南京信息工程大学滨江学院C++面向对象程序设计期末复习资料
+++ 面向对象程序设计复习资料
一、概念、填空题
1.面向对象的四大特性是:抽象、封装、继承、多态
2.构造函数的作用是创建对象时为对象赋初值,析构函数的作用是释放对象占用的资源
3.根据继承的基类的多少,C++将继承分为:单继承和对继承
4.C++进行动态内存分配的一对运算符是new和delete
5.抽象类、纯虚函数:…..P153
纯虚函数是一个在基类中说明的虚函数,它在该类基类中没有定义,但要求在它的派生类中必须定义自己的版本,或重新说明为纯虚函数。
如果一个类至少有一个纯虚函数,那么就称该类为抽象类。
6.对象拷贝有两种方式:浅拷贝和深拷贝
7.变量的生存期分为:动态生存期和静态生存期
8.C++里的逻辑值:非0表示真,0表示假
9.C++里函数参数传递方式有:值传递、地址传递和引用传递(P35例2-13程序1程序2程
序3)
10.静态成员变量属于类,所有的对象共享。........静态成员变量的使用
11.根据访问控制,C++将继承分为:公有继承、私有继承和保护继承
12.用const修饰的对象,其意思是:对象的状态不能被修改
13.拷贝构造函数被调用的场合有3种,分别是:用对象作参数构造对象时、为函数实参对
象赋值时、函数返回对象时
14.C++里的模版包括:函数模版和类模版
15.命名空间及其作用
名字空间就是一个由程序设计者命名的内存区域,名字空间的作用类似于操作系统中的目录和文件的关系。名字空间建立了一些相互分隔的作用域,把全局标识符分隔开,避免产生名字冲突。(P48)
16.类的访问属性包括:公有、私有、继承
17.虚基类的作用 P124
如果一个派生类有多个直接基类,而这些基类又有一个共同的基类,则在最终的派生类中会保留该间接共同基类成员的多份同名成员。但由于保留共同基类的多份成员,不仅占用较多的存储空间,还增加了访问这些成天元时的困难,容易出错。C++提供了
虚基类的方法,使得在继承间接共同基类时只保留其一份成员。
18.多态及其分类P145
多态性是指用一个名字定义不同的函数,这些函数执行不同但又类似的操作,从而可以使用相同的调用方式来调用这些具有不同功能的同名函数。
参数多态,包含多态,重载多态,强制多态,前两种称为通用多态,后两种称为专用多态
19.虚函数与重载函数的比较 ....P153
(1)函数重载处理的是同一层次上的同名函数问题,而虚函数处理的是同一类族中不同派生层次上的同名函数问题,前者是横向重载,后者可以理解为纵向重载。但与重载不同的是:同一类族的虚函数的首部是相同的,而函数重载时函数的首部是不同的。(2)重载函数可以是成员函数或普通函数,而虚函数只能是成员函数。(3)重载函数调用是以所传递参数序列的差别作为调用不同函数的依据;虚函数是根据对象的不同去调用不同类的虚函数。(4)虚函数在运行时表现出多态功能,这是C++精髓,而重载函数则在编译时表现出多态性。
20,一个类的函数是否是虚函数的条件是:.....P151
判断该函数与基类的虚函数有相同的名称;该函数与基类的虚函数有相同的参数个数及相同的对应参数类型;该函数与基类的虚函数有相同的返回类型或者满足赋值兼
容规则的指针、引用型的返回函数。
21,c in cout >> <<
22,根据文件中数据的组织,文件分为ascII文件和二进制文件
23,强制类型转换:(int)3.5 int(3.5)
24,内联函数inline P62
该方法的思想是在编译时将调用函数的代码直接嵌入到调用函数处。INLINE函数的机制兼顾了函数和效率两个方面的有点。
二、看程序结果
1、以下程序的结果是_____5,7 5,10 5,7_________________(全局变量与局部变量问题)
viod myfunction( )
{int y=10; cout< int x=5 ,y=7 ; int main( ) { cout< myfunction( ); cout< 2、以下程序的结果是___2,12 2,14_____________________________(静态变量应用) int ret(int a) { static int c=10; a=a+c ; c=a ; return c; } int main() { int a=2; for(int i=0; i<2; i++) cout< 3、以下程序的结果是________________3________________(指针) int main( ) { int a=2 ; Int *b=&a ; a=3 ; cout<<*b ; } 4、以下程序的结果是________________3________________(引用) int main( ) { int a=2 ; int &b=a ; a=3 ; cout< 5、以下程序的结果是________________________________________(参数传递) int main( ) { int x(3) , y(8) ; Swap(x , y) ; cout<< “x=“< void Swap(int &a ,int &b) { int t ; t=a ;a=b ; b=t ; } 6、以下程序的结果是________________________________(参数传递) int main( ) { int x(3) , y(8) ; Swap(&x , & y) ; cout<< “x=“< void Swap(int * a ,int * b) { int t ; t=*a ;*a=*b ; *b=t ; } 7、以下程序的结果是________________4________________(引用) int main( ) { int a=2 ; int &b=a ; a=a+b ; cout< 8、以下程序的结果是_________________5_______________(数组) int main( ) { int a[5]={2,3,4,5,6} ; Int *b=a ; cout<<*(b+3); } 三、程序填空 1、输入一个整数,将各位数字反转后输出(利用运算符/ 和 %) int main( ){ int n ,rd ,newnum ; cin>>n ; do{ rd=____n%10_______; cout< cout< 2、输入一系列整数,统计出正整数个数和负整数个数,读入0结束 int main( ){ int i=0 ,j=0 ,n ; cin>>n ; while(n!=0){if(n>0)___i++______; if(n<0)__j++______ cin>>n ;} cout<<”正整数个数:“< 3、用户输入考试分数(0~100),输出考试等级。 int main( ){ int m ; _cin>>m>>endl_____________ ; switch(____m/10_____) { case 10: case 9: cout<<”优秀” ; break ; case 8: cout<<”良好” ; break ; case 7: cout<<”中等” ; break ; case 6: cout<<”及格” ; break ; default: cout<<”不及格”; } 4、交换两个整数 int main( ) { int x(5) , y(10) ; cout<< “x=“< Swap(x , y) ; cout<< “x=“< void Swap(int _&a______ ,int ____&b_____) { int t ; t=a ;a=b ; b=t ; } 5、查找数组中元素值为5的元素,返回其序号 int main( ){ int n ,a[10] , i=0 ; for(i=0 ; i<10 ; i++ ) cin>>a[i] ; for(i=0 ; i<10 ; i++ ) if(____a[i]==5______) __count< if(i==10)cout<<”no find”; else cout< } 6、查找数组中元素值为’b’的元素,将其替换为‘B’ int main( ){ char a[10] ; int i=0 ,j=0 ,n ; cin>>a ; for(i=0 ; i<10 ; i++ ) if(______a[i]=='b'_______________) {______a[i]='B'_____; cout<<”ok” ;} 7、交换两个整数 int main( ) { int x(5) , y(10) ; cout<< “x=“< Swap(_int &x______ , _int &y__________) ; cout<< “x=“< void Swap(int *a ,int *b) { int t ; ___t=*a_________ ; ____*a=*b ; _____*b=t________ } 四、编程题 2、定义一个复数类,实现两个复数类相加和相减。P206 要求:(1)将运算符函数重载为成员函数;(2)将运算符函数重载为友元函数#include using namespace std; class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} Complex operator + (Complex &c2); Complex operator - (Complex &c2); void display(); private: double real; double imag; }; void Complex::display() {cout<<"("< Complex Complex::operator + (Complex &c2) { Complex c; c.real=real+c2.real; c.imag=imag+c2.imag; return c; } Complex Complex::operator - (Complex &c2) { Complex c; c.real=real-c2.real; c.imag=imag-c2.imag; return c; } int main() { Complex c1(3,4),c2(5,-10),c3,c4; c1.display(); c2.display(); c3=c1+c2; cout<<"c3=c1+c2="; c3.display(); c4=c1-c2; cout<<"c4=c1-c2="; c4.display(); return 0; }#include class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} friend Complex operator + (Complex &c1, Complex &c2); friend Complex operator - (Complex &c1, Complex &c2); void display(); private: double real; double imag; }; void Complex::display() {cout<<"("< Complex operator + (Complex &c1, Complex &c2) { Complex c; c.real=c1.real+c2.real; c.imag=c1.imag+c2.imag; return c; } Complex operator - (Complex &c1, Complex &c2) { Complex c; c.real=c1.real-c2.real; c.imag=c1.imag-c2.imag; return c; } int main() { Complex c1(3,4),c2(5,-10),c3,c4; c1.display(); c2.display(); c3=c1+c2; cout<<"c3=c1+c2="; c3.display(); c4=c1-c2; cout<<"c4=c1-c2="; c4.display(); return 0; } 5-7 #include using namespace std; class Cat { public: Cat(int age){numOfCats++; } virtual ~Cat() { numOfCats--; } static int getnumOfCats() { return numOfCats; } private: static int numOfCats; }; int Cat::numOfCats = 0; void telepathicFunction() { cout << "There are " << Cat::getnumOfCats() << " cats alive!\n"; } int main() { const int maxCats = 5; Cat *catHouse[maxCats]; int i; for (i = 0; i < maxCats; i++) { catHouse[i] = new Cat(i); tele pathicFunction(); } for (i = 0; i < maxCats; i++) { delete catHouse[i]; telepathi cFunction(); } return 0; } 4-11 #include class Rectangle { public: Rectangle(float len, float width) { Length = len; Width = width; } ~Rectangle(){}; float GetArea() { return Length * Width; } float GetLength() { return Length; } float GetWidth() { return Width; } private: float Length; float Width; }; void main() { float length, width; cout << "请输入矩形的长度:"; cin >> length; cout << "请输入矩形的宽度:"; cin >> width; Rectangle r(length, width); cout << "长为" << length << "宽为" << width << "的矩形的面积为:" << r.GetArea () << endl; } 3、编写一个类,其成员函数包括:1)接受键盘输入的英文句子;2)实现一条英文句子中字母的个数统计并输出;3)输出英文句子。主函数里进行测试。 #include using namespace std; class Char { public: Char(){count=0;} void Input(); void show(); void GetCount() {