写在前面:

推荐直接将此文链接转发给好友!

如果发现我的代码有误或者我的理解有偏差请联系我修改:QQ573315002,如果需要补充点什么也请联系我。


2013~2014第2学期 计算机学院 计算机类专业 C++面向对象程序设计(甲)期末考试要求

总体要求:

1. 掌握C++语言的基本语法规则和基础知识。

2. 能够阅读和分析C++程序。

3. 理解面向对象理论,能够采用面向对象的方法编写应用程序。

4. 学习现代C++程序设计方法 ,利用现有STL容器和算法

重点要求 :

1. 学会动态分配,理解单链表处理,正确运用函数重载和默认参数、内联函数。

/*
 * Author: Haipz
 * School: HDU
 * File Name: 1.cpp
 */
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cctype>
#include <cfloat>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

int main() {
    //动态分配
    int len = 100; //定义数组大小
    int *array = new int[len]; //申请指定大小内存
    delete[] array; //释放内存

    //单链表处理
    struct node {
        int value; //当前节点储存的值
        node *next; //下一个节点的地址
    };
    node *root = new node(); //建立链表头
    len = 10; //定义链表长度
    node *temp = root; //申请一个暂用指针
    for (int i = 0; i < len; ++i) {
        temp->next = new node();
        temp = temp->next;
        temp->value = i;
    } //建立长度为len的链表,其值为其下标
    int index = 5; //指定删除节点下标
    temp = root; //令指针指向根节点
    for (int i = 0; i < index; ++i) temp = temp->next; //将指针转移到删除节点前
    temp->next = temp->next->next; //另下一个节点的地址跳过删除节点
    while (root->next) {
        root = root->next;
        cout << root->value << ' ';
    }
    cout << endl;

    //函数重载
    class Overload {
        public:
            int value;
            void SetValue() {
                value = 0;
            } //定义函数

            void SetValue(int Value) {
                value = Value;
            } //函数重载

            void ChangeValue(int Value = 0) {
                value = Value;
            } //默认参数
    };
    Overload *Root = new Overload();
    Root->SetValue(); //调用定义函数
    cout << Root->value << endl;
    Root->SetValue(100); //调用函数重载
    cout << Root->value << endl;
    Root->ChangeValue(); //调用默认参数
    cout << Root->value << endl;
    
    return 0;
}
2. 学习面向对象程序设计基本理论, 掌握类与对象的基本知识 :类与对象的定义、 成员访问控制、常成员函数对象作用域和生成期、对象构造和析构时机 (初始化列表, 基类和子对象构造)。
/*
 * Author: Haipz
 * School: HDU
 * File Name: 2.cpp
 */
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cctype>
#include <cfloat>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

int main() {
    //类和对象的定义
    class Person {
        private: //成员访问控制
            string name;
            int age;
        public:
            Person(string Name = "unknow", int Age = 99) //带初始化列表的构造函数
                :name(Name), age(Age) {
                    cout << "I am " << name << " and my age is " << age << '.' << endl;
                }

            ~Person() { //析构函数
                cout << "Goodbye man." << endl;
            }
    };
    Person *haipz = new Person(); //构造函数默认值
    delete(haipz);
    haipz = new Person("haipz", 20); //构造函数自定义

    return 0;
}
3.掌握传统复制构造、复制赋值(深层复制和浅层复制),理解C++11移动构造、移动赋值机制,掌握对象如何在函数间传递。
/*
 * Author: Haipz
 * School: HDU
 * File Name: 3.cpp
 */
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cctype>
#include <cfloat>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

class iString { //自定义字符串类
    private:
        char *buffer; //指针指向字符串首地址
    public:
        iString(const char *Buffer) {
            cout << "Creating new iString." << endl;
            if (Buffer == NULL) buffer = NULL;
            else {
                buffer = new char[strlen(Buffer) + 1]; //申请字符串长度内存
                strcpy(buffer, Buffer); //拷贝字符串
                cout << "Buffer points to: " << (unsigned int*)buffer << endl;
            }
        } //构造函数

        iString(const iString &Source) {
            cout << "Copying from iString." << endl;
            if (Source.buffer == NULL) buffer = NULL;
            else {
                buffer = new char[strlen(Source.buffer) + 1]; //申请字符串长度内存
                strcpy(buffer, Source.buffer); //拷贝字符串
                cout << "Buffer points to: " << (unsigned int*)buffer << endl;
            }
        } //复制构造函数确保深复制

        ~iString() {
            cout << "Cleaning up!" << endl;
            if (buffer != NULL) delete[] buffer; //释放内存
        } //析构函数

        int GetLength() {
            return strlen(buffer); //返回字符串长度
        }

        const char *GetString() { //const能保证字符串不被修改
            return buffer; //返回字符串
        }
};

void SayHello(iString Input) {
    cout << "Buffer length: " << Input.GetLength() << endl; //查询字符串长度
    cout << "Buffer contains: " << Input.GetString() << endl; //查询字符串
    return;
}

int main() {
    iString hello("Hello, I am haipz."); //新建一个字符串
    SayHello(hello); //调用函数执行

    return 0;
}
4.掌握对象和指针间关系基本知识;理解特殊机制 :静态成员、 友元函数。
/*
 * Author: Haipz
 * School: HDU
 * File Name: 4.cpp
 */
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cctype>
#include <cfloat>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

class Student { //定义一个学生类
    private:
        string name; //成员变量
        friend void Output(Student Input); //声明友元
    public:
        static int count; //静态成员变量

        Student(string Name) //构造函数初始化
            :name(Name) {
                ++count; //统计
            }

        ~Student() { //析构函数
            cout << "Goodbye " << name << '.' << endl; //输出学生名
        }
};

void Output(Student Input) {
    cout << "Student Name: " << Input.name << endl; //访问私有变量
    return;
}

int Student::count = 0; //初始化学生类静态成员变量

int main() {
    //不能通过类名来调用类的非静态成员函数
    //类的对象可以使用静态成员函数和非静态成员函数
    //静态成员函数中不能引用非静态成员
    //类的非静态成员函数可以调用用静态成员函数
    //类的静态成员变量必须先初始化再使用
    Student student0("haipz0"); //新建一个学生
    Output(student0); //输出学生
    cout << Student::count << endl; //输出当前学生数量
    Student student1("haipz1"); //新建一个学生
    Output(student1); //输出学生
    cout << Student::count << endl; //输出当前学生数量
    Student student2("haipz2"); //新建一个学生
    Output(student2); //输出学生
    cout << Student::count << endl; //输出当前学生数量

    return 0;
}
5. 掌握成员运算符和友元运算符两种重载方法;掌握运算符重载规则,掌握不同类型对象间转换。掌握+, -, *, /, [],=, <<, ++, --等运算符重载。
/*
 * Author: Haipz
 * School: HDU
 * File Name: 5.cpp
 */
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cctype>
#include <cfloat>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

template <typename T>
class iPointer { //自定义指针类
    private:
        T* pointer; //指针地址
    public:
        iPointer(T* Pointer)
            :pointer(Pointer) {
            }

        ~iPointer() {
            delete pointer;
        }

        T& operator* () const { //解除引用运算符
            return *pointer;
        }

        T* operator-> () const { //解除成员选择运算符
            return pointer;
        }
};

class iInt {
    private:
        int value;
    public:
        iInt(int Value)
            :value(Value) {
            }

        ~iInt() {
            cout << "Goodbye iInt(" << value << ')' << endl;
        }

        int Get() {
            return value;
        }

        //单目运算符
        iInt& operator++ () { //前缀递增运算符
            ++value;
            return *this;
        }

        iInt operator++ (int) { //后缀递增运算符
            value++;
            return *this;
        }
        //前缀递减运算符、后缀递减运算符与上类似

        operator const char*() { //转换运算符
            string sValue = "";
            int tValue = value;
            while (tValue) sValue += '0' + tValue % 10, tValue /= 10;
            for (int i = 0; i < (int)sValue.length() / 2; ++i) swap(sValue[i], sValue[sValue.length() - 1]);
            return sValue.c_str();
        }

        //双目运算符
        iInt operator+ (iInt IInt) { //加法运算符
            return iInt(value + IInt.value);
        }

        void operator+= (iInt IInt) { //加并赋值运算符
            value += IInt.value;
        }
        //-、*、/、-=、*=、/=、%=、<<=、>>=、^=、|=、&=与上类似

        bool operator== (const iInt& IInt) { //等于运算符
            return value == IInt.value;
        }

        bool operator!= (const iInt& IInt) { //不等于运算符
            return !(value == IInt.value);
        }

        bool operator< (const iInt& IInt) { //小于运算符
            return value < IInt.value;
        }

        bool operator<= (const iInt& IInt) { //小于等于运算符
            return value <= IInt.value;
        }
        //>、>=与上类似
        
        iInt& operator= (const iInt& IInt) { //复制赋值运算符
            value = IInt.value;
            return *this;
        }
};

int main() {
    iPointer<iInt> tPointer(new iInt(13)); //自定义指针类
    cout << *tPointer << endl; //转换运算符
    iInt *tInt = new iInt(1355);
    cout << tInt->Get() << endl;
    delete(tInt);
    tInt = new iInt(13055700);
    cout << tInt->Get() << endl;
    delete(tInt);
    iInt int0(0), int1(13055700), int2(10), int3(2);
    cout << int0.Get() << endl;
    int0 = int1 + int2; //加法运算符
    cout << int0.Get() << endl;
    int0 += int3; //加并赋值运算符
    cout << int0.Get() << endl;
    ++int3; //前缀递增运算符
    cout << int3.Get() << endl;
    int3++; //后缀递增运算符
    cout << int3.Get() << endl;
    if (int0 < int1) { //小于运算符
        cout << int0.Get() << " < " << int1.Get() << endl;
    } else {
        cout << int0.Get() << " > " << int1.Get() << endl;
    }
    if (int0 == int1) { //等于运算符
        cout << int0.Get() << " == " << int1.Get() << endl;
    } else {
        cout << int0.Get() << " != " << int1.Get() << endl;
    }
    if (int0 <= int1) { //小于等于运算符
        cout << int0.Get() << " <= " << int1.Get() << endl;
    } else {
        cout << int0.Get() << " => " << int1.Get() << endl;
    }
    return 0;
}
7. 掌握异常抛出和捕获。
/*
 * Author: Haipz
 * School: HDU
 * File Name: 7.cpp
 */
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cctype>
#include <cfloat>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <exception>
#include <algorithm>
using namespace std;

int Divide(int a, int b) {
    if (b == 0)
        throw "Dividing by 0!"; //抛出异常
    return a/b;
}

int main() {
    int a = 1, b = 0; //初始化值
    try { //尝试执行
        cout << Divide(a, b) << endl;
    }
    catch(const char* exp) { //捕获异常
        cout << exp << endl; //输出异常
    }
    return 0;
}

8. 掌握函数模板和类模板基本知识。

/*
 * Author: Haipz
 * School: HDU
 * File Name: 8.cpp
 */
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cctype>
#include <cfloat>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

template <typename iType> //声明类型名
class iTemplate {
    private:
        iType* pointer; //指针储存
    public:
        iTemplate() {
        }

        iTemplate(iType* Pointer) //构造函数
            :pointer(Pointer) {
            }

        ~iTemplate() { //析构函数
            delete pointer;
        }

        void Set(iType* Pointer) { //修改变量
            pointer = Pointer;
            return;
        }

        iType Get() { //获取变量
            return *pointer;
        }
};

int main() {
    iTemplate<int> T(new int(13055712));
    cout << T.Get() << endl;
    T.Set(new int(71255031));
    cout << T.Get() << endl;

    return 0;
}

9. 基本了解STL容器、算法、迭代器机制,掌握4个STL容器或容器适配器 (stack、 queue、vector、string)的基本使用和实现原理。 具体要求掌握如下接口 (理解程序意义):

stack: 构造、push、pop、top、empty

queue: 构造、push、pop、front 、back、empty

vector: 构造、empty、size 、push_back、[]、at 、通过迭代器遍历

string: 构造、empty、size 、[]、at 、+、+=、substr、== 、!=、<、>、<=、>=

/*
 * Author: Haipz
 * School: HDU
 * File Name: 9.cpp
 */
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cctype>
#include <cfloat>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

int main() {
    //stack
    cout << "-----> Stack <-----" << endl;
    stack<int> Stack;
    Stack.push(0);
    Stack.push(1);
    Stack.pop();
    Stack.push(2);
    Stack.push(3);
    Stack.pop();
    Stack.push(4);
    Stack.push(5);
    while (!Stack.empty()) {
        cout << Stack.top() << endl;
        Stack.pop();
    }

    //queue
    cout << "-----> Queue <-----" << endl;
    queue<int> Queue;
    Queue.push(0);
    Queue.push(1);
    Queue.pop();
    Queue.push(2);
    Queue.push(3);
    Queue.pop();
    Queue.push(4);
    Queue.push(5);
    while (!Queue.empty()) {
        cout << Queue.front() << " - " << Queue.back() << endl;
        Queue.pop();
    }

    //vector
    cout << "-----> Vector <-----" << endl;
    vector<int> Vector;
    if (!Vector.empty()) {
        vector<int>::iterator it;
        for (it = Vector.begin(); it != Vector.end(); ++it) cout << *it << ' ';
        cout << endl;
        for (unsigned int i = 0; i < Vector.size(); ++i) cout << Vector[i] << ' ';
        cout << endl;
        for (unsigned int i = 0; i < Vector.size(); ++i) cout << Vector.at(i) << ' ';
        cout << endl;
    }
    Vector.push_back(0);
    Vector.push_back(1);
    Vector.push_back(2);
    Vector.push_back(3);
    Vector.push_back(4);
    Vector.push_back(5);
    if (!Vector.empty()) {
        vector<int>::iterator it;
        for (it = Vector.begin(); it != Vector.end(); ++it) cout << *it << ' ';
        cout << endl;
        for (unsigned int i = 0; i < Vector.size(); ++i) cout << Vector[i] << ' ';
        cout << endl;
        for (unsigned int i = 0; i < Vector.size(); ++i) cout << Vector.at(i) << ' ';
        cout << endl;
    }

    //string
    cout << "-----> String <-----" << endl;
    string String0 = "haipz0";
    string String1 = "";
    if (String1.empty()) cout << "Empty!" << endl;
    else cout << "No Empty!" << endl;
    String1 = "haipz1";
    if (String1.empty()) cout << "Empty!" << endl;
    else cout << "No Empty!" << endl;
    if (String0 > String1) cout << String0 << " larger than " << String1 << endl;
    else cout << String0 << " smaller than " << String1 << endl;
    //==、!=、<、<=、>=与上类似
    string String2 = String0 + String1;
    cout << String2 << endl;
    string String3 = String2.substr(0, 5);
    for (unsigned int i = 0; i < String3.size(); ++i) cout << String3[i];
    cout << endl;
    for (unsigned int i = 0; i < String3.size(); ++i) cout << String3.at(i);
    cout << endl;

    return 0;
}

10. 学习C++11标准的Lambda表达式,掌握STL算法sort、find、 find_if、count、count_if的基本使用方法(理解程序)。

/*
 * Author: Haipz
 * School: HDU
 * File Name: 10.cpp
 */
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cctype>
#include <cfloat>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

int mincmp(int a, int b) {
    return a < b; //从小到大排序
}

int maxcmp(int a, int b) {
    return a > b; //从大到小排序
}

bool cmp(int element) {
    if (element % 2 != 0) return true; //判断奇数
    return false;
}

int main() {
    //sort
    cout << "-----> Sort <-----" << endl;
    int f[5] = {5, 1, 3, 1, 4};
    for (int i = 0; i < 5; ++i) cout << f[i] << ' ';
    cout << endl;
    sort(f, f + 5, mincmp);
    for (int i = 0; i < 5; ++i) cout << f[i] << ' ';
    cout << endl;
    sort(f, f + 5, maxcmp);
    for (int i = 0; i < 5; ++i) cout << f[i] << ' ';
    cout << endl;

    //find
    cout << "-----> Find <-----" << endl;
    int toFind = 4;
    int *Index = find(&f[0], &f[5], toFind);
    if (Index != &f[5]) cout << "Found!" << endl;
    else cout << "No Find!" << endl;

    //find_if
    cout << "-----> Find_if <-----" << endl;
    Index = find_if(&f[0], &f[5], [](int element) { return element == 4; } ); //lambda表达式
    cout << *Index << endl;

    //count
    cout << "-----> Count <-----" << endl;
    int toCount = 1;
    int Count = count(&f[0], &f[5], toCount);
    cout << Count << endl;

    //count_if
    cout << "-----> Count_if <-----" << endl;
    Count = count_if(&f[0], &f[5], cmp); //自定义判断函数
    cout << Count << endl;

    return 0;
}

11. 学会输入输出流的基本使用方法。

/*
 * Author: Haipz
 * School: HDU
 * File Name: 11.cpp
 */
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cctype>
#include <cfloat>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

int main() {
    int iNumber = 13055712;
    cout.setf(ios::dec); //十进制
    cout << iNumber << endl;
    cout.unsetf(ios::dec);
    cout.setf(ios::oct); //八进制
    cout << iNumber << endl;
    cout.unsetf(ios::oct);
    cout.setf(ios::hex); //十六进制
    cout << iNumber << endl;
    cout.unsetf(ios::hex);
    double tNumber = 13055.712;
    cout.unsetf(ios::scientific); //终止科学记数法状态
    cout.setf(ios::fixed); //指定用定点形式输出
    cout.precision(6); //保留六位小数
    cout << tNumber << endl;
    cout.width(20); //指定域宽为20
    cout << tNumber << endl;
    cout.width(15); //指定域宽为15
    cout.fill('x'); //指定空白处以'x'填充
    cout << tNumber << endl;
    cout.width(25); //指定域宽为25
    cout.setf(ios::right); //在域宽内靠右
    cout << tNumber << endl;
    cout.setf(ios::showpos); //输出正数时,给出“+”号
    cout << tNumber << endl;

    return 0;
}

具体题型 :

一、判断题(每小题2分,共10分)

二、单项选择题(每小题2分,共20分)

三、基础知识填空题(本题10分)

四、程序分析题(每小题8分,共32分

重点 : 类和对象,异常,虚函数,STL容器,迭代遍历,算法)给出下面各程序的输出结果。

五、程序设计题(每小题14分,共28分)

重点 : 类设计、类成员设置与获取、动态分配、复制构造、运算符重载) 考试时间 :6月9日 18:30~20:30

所有代码下载,右键下面下载链接选择链接另存为:

http://haipz.qiniudn.com/cpp@haipz.com.zip

转载保留版权:http://haipz.com/blog/i/3328 - 海胖博客