C++期末考试复习题库
选择题
- C++源程序文件的缺省扩展名为( A )。
`A. cpp B. exe C. obj D. lik`
- 用new运算符创建一个含10个元素的一维整型数组的正确语句是( C )。
A. int *p=new a[10]; B. int *p=new float[10]; C. int *p=new int[10]; D. int *p=new int[10]={1,2,3,4,5}
- 下列的符号常量定义中,错误的定义是(A)。
A、 const M=10; B、 const char ch; C、 const int M=20; D、 const bool mark=true;
- 关于封装,下列说法中不正确的是( D )。
A. 通过封装,对象的全部属性和操作结合在一起,形成一个整体 B. 通过封装,一个对象的实现细节被尽可能地隐藏起来(不可见) C. 通过封装,每个对象都成为相对独立的实体 D. 通过封装,对象的属性都是不可见的
- 函数重载是指( A )。
A. 两个或两个以上的函数取相同的函数名,但形参的个数或类型不同 B. 两个以上的函数取相同的名字和具有相同的参数个数,但形参的类型可以不同 C. 两个以上的函数名字不同,但形参的个数或类型相同 D. 两个以上的函数取相同的函数名,并且函数的返回类型相同
- 为了提高程序的运行速度,可将不太复杂的功能用函数实现,此函数应选择( B )。
A. 重载函数 B. 内联函数 C.递归函数 D.函数模板
- 假定AA为一个类,a为该类公有的数据成员,x为该类的一个对象,则访问x对象中数据成员a的格式为( D )。
A. x(a) B. x[a] C. x->a D. x.a
- 一个类的构造函数通常被定义为该类的( A )成员。
A. 公有 B. 保护 C. 私有 D. 友元
- 类的析构函数可以带有( A )个参数。
A. 0 B. 1 C. 2 D. 任意
- 引入友元的主要目的是为了( C )。
A. 增强数据安全性 B. 提高程序的可靠性 C. 提高程序的效率和灵活性 D. 保证类的封装性
- 派生类的成员函数可以直接访问基类的( B )成员。
A. 所有 B. 公有和保护 C. 保护和私有 D. 私有
- 在定义一个派生类时,若不使用保留字显式地规定采用何种继承方式,则默认为( A)方式。
A. 私有继承 B. 非私有继承 C. 保护继承 D. 公有继承
在定义一个派生类时,,若是struct,则若不使用保留字显式地规定采用何种继承方式,则默认为公有继承方式。公有继承是最常用的继承方式,它将基类的公有成员和保护成员都继承到派生类中,并且基类的私有成员不能被派生类直接访问。如果需要使用其他继承方式,如私有继承或保护继承,需要使用相应的保留字来显式地指定。class默认是私有。
- C++中的虚基类机制可以保证:(D )。
A. 限定基类只通过一条路径派生出派生类 B. 允许基类通过多条路径派生出派生类,派生类也就能多次继承该基类 C. 当一个类多次间接从基类派生以后,派生类对象能保留多份间接基类的成员 D. 当一个类多次间接从基类派生以后,其基类只被一次继承
C++中的虚基类机制可以保证限定基类只通过一条路径派生出派生类。虚基类是指在多重继承中,被派生类的多个基类中,有一个或多个是共享同一个基类的子对象。为了避免虚基类在派生类中出现多次,C++引入了虚基类的概念。虚基类是通过在基类声明中使用关键字virtual来定义的。当一个类从虚基类派生时,虚基类的子对象只会在派生类中出现一次,从而避免了多次继承的问题。虚基类机制可以保证派生类只通过一条路径派生出基类,从而避免了多次继承的问题。
- 面向对象方法的多态性是指( C )。
A. 一个类可以派生出多个特殊类 B. 一个对象在不同的运行环境中可以有不同的变体 C. 针对一消息,不同的对象可以以适合自身的方式加以响应 D. 一个对象可以是由多个其他对象组合而成的
- 如果表达式a+b中的“+”是作为成员函数重载的运算符,若采用运算符函数调用格式,则可表示为( A )。
A. a.operator+(b) B. b.operator+(a) C. operator+(a,b) D. operator(a+b)
- 以下设置默认值的函数原型声明中错误的是( C )。
A.int add(int x, int y, int z=5); B.int add(int x, int y=4, int z=5); C.int add(int x, int y=4, int z); D.int add(int x=3, int y=4, int z=5);
- 下列运算符中,( C )运算符在C++语言中不能重载。
A.+= B.[ ] C.:: D.new
- 系统在调用重载函数时往往根据一些条件确定哪个重载函数被调用,在下列选项中,不能作为依据的是( D )。
A.参数的个数 B.参数的类型 C.参数的顺序 D.函数的返回类型
系统在调用重载函数时往往根据参数的个数、参数的类型、参数的顺序和函数的返回类型等条件来确定哪个重载函数被调用。在下列选项中,不能作为依据的是函数的返回类型。函数的返回类型不会影响系统调用重载函数的选择,因为函数的返回类型只是函数的一个属性,不会影响函数的参数列表。而参数的个数、参数的类型和参数的顺序都会影响函数的参数列表,从而影响系统调用重载函数的选择。因此,系统在调用重载函数时往往根据参数的个数、参数的类型和参数的顺序来确定哪个重载函数被调用。
- 友元的作用(A )
A.提高程序的运行效率 B.加强类的封装性 C.实现数据的隐藏性 D.增加成员函数的种类
- 模板的作用是( D )。
A.提高程序的运行效率 B.加强类的封装性 C.实现数据的隐藏性 D.提高代码可重用性
- 基类中的公有成员经过保护派生后,它在派生类中的访问属性是( B )。
A.public B.protected C.private D.不可以被继承
- 下列哪个不是构造函数的特征( D )。
A.构造函数名与类名同名 B.构造函数可以重载 C.构造函数可以设置默认参数 D.构造函数必须指定类型说明
- 下列关于静态数据成员的特性中,( D )是错误的。
A.说明静态数据成员时前面要加修饰符static B.静态数据成员要在类体外进行初始化 C.可以使用类名加作用域运算符访问静态数据成员 D.静态数据成员不是该类所有对象共享的
- 关于参数缺省值的设置,正确的是( C)。
A.不允许设置参数的默认值 B.设置参数默认值只能在定义函数时设置 C.设置参数默认值时,应该先设置右边的再设置左边的 D.设置参数默认值时,应该全部参数都设置
- 通过函数实现一种简单功能,并且需要加快执行速度,选用( A )。
A.内联函数 B.重载函数 C.递归调用 D.嵌套调用
- 函数的整型参数使用引用方式,为了不改变参数的内容,参数引用的格式应该是(C )。
A.const int func(int & ) B.int func(int & ) const C.int func(const int & ) D.int func(const int * ) const
- 关于抽象类,下列说明正确的是( C )。
A.抽象类可以创建对象 B.抽象类中只能有一个纯虚函数 C.抽象类中可以有非纯虚函数 D.抽象类不可派生出抽象类
- 下列说明中,
const char *ptr=”Nanjing”;
ptr应该是( C )。
A.指向字符常量的指针 B.指向字符的常量指针 C.指向字符串常量的指针 D.指向字符串的常量指针
- 多继承派生类构造函数构造对象时,最先被调用的是( B )。
A.派生类构造函数 B.虚基类的构造函数 C.非虚基类的构造函数 D.派生类子对象的构造函数
- 下列关于对象数组的描述中,(D )是错误的。
A.对象数组的下标是从0开始的 B.对象数组的数组名是一个常量指针 C.对象数组的每个元素是同一个类的对象 D.对象数组只能赋初值,而不能在定义后赋值
填空题
读程序
- 下面程序的输出结果是?
#include using namespace std; int main() { char *str="abbcabdced"; int c1=0,c2=0,c3=0,c4=0; for(int i=0;str[i];i++) switch(str[i]) { case 'a':c1++;break; case 'b':c2++;break; case 'c':c3++;break; default:c4++; } cout int x, y; public: B() { x = y = 0; cout x = i; y = 0; cout x = i; y = j; cout cout B *ptr; ptr = new B[3]; ptr[0] = B(); ptr[1] = B(1); ptr[2] = B(2,3); delete[] ptr; return 0; } const double PI; double r; public: Circle(double rr):PI(3.14) { r=rr; } double Area()const { return PI*r*r; } }; int main() { Circle c1(1); const Circle c2(2); cout public: B() { cout cout public: D() { cout cout D *p = new D; cout public: virtual void fun() { cout public: virtual void fun() { cout public: void fun() { cout C c; A &p1=c; B &p2=c; C &p3=c; p1.fun(); p2.fun(); p3.fun(); return 0; } x+=y; y+=x; } int main() { int x=3,y=1; fun(x,y); fun(y,x); cout public: C(){coutcout C c; cout int i; static int k; public: S(); void disp(); }; S::S() { i=0;k++; } void S::disp() { cout S a,b; a.disp(); b.disp(); return 0; } public: void f1( ); A(int i, int j ) { i1=i; j1=j;} protected: int j1; private: int i1; }; class B : protected A { public: void f2( ); B(int i,int j, int k ,int l):A(i, j) { i2=k; j2=l;} protected: int j2; private: int i2; }; private: int Hour; int Minute; int Second; public: Time(); Time(int hour,int minute,int second); ~Time(); void SetTime(int hour,int minute,int second); int GetHour(); int GetMinute(); int GetSecond(); void Print(); }; Time::Time(){ }; Time::~Time(){ }; Time::Time(int hour,int minute,int second){ Hour=hour; Minute=minute; Second=second; } void Time::SetTime(int hour,int minute,int second){ Hour=hour; Minute=minute; Second=second; } int Time::GetHour(){ return Hour; } int Time::GetMinute(){ return Minute; } int Time::GetSecond(){ return Second; } void Time::Print(){ cout Time a(5,40,30); cout double x,y; public: Point(double a=0,double b=0);//构造函数 friend bool operator(Point &A,Point &B);//若A点到原点距离大于B点到原点 //距离,则为真;否则为假 friend double GetLength(Point &A,Point &B);//计算点与点之间的距离 }; 试编程实现类中带注释语句的函数,每个5分。 private: double x; //x轴坐标 double y; //y轴坐标 public: Point(); Point(double a=0,double b=0);//构造函数 //友元函数的特点:函数可以直接访问这个类的对象的私有成员 friend bool operator(Point &A,Point &B);//若A点到原点距离大于B点到原点 //距离,则为真;否则为假 friend double GetLength(Point &A,Point &B);//计算点与点之间的距离 }; Point::Point(){} Point::Point(double a,double b){ x=a; y=b; } double GetLength(Point &A,Point &B){ double len=0; int d1,d2; d1=A.x*A.x+A.y*A.y; d2=B.x*B.x+B.y*B.y; if(d1d2){ len=sqrt((A.x-B.x)*(A.x-B.x)+(A.y*-B.y)*(A.y*-B.y)); }else{ len=sqrt((B.x-A.x)*(B.x-A.x)+(B.y-A.y)*(B.y-A.y)); } return len; } bool operator (Point &A,Point &B){ int d1,d2; d1=A.x*A.x+A.y*A.y; d2=B.x*B.x+B.y*B.y; return d1-d2 0 ? true :false; } int main(){ Point A(1,1); Point B(2,2); bool c=AB; cout cout cout int a,b,c; char ch; cinachbc; cout int i=5; { int i=7; cout x+=y; y+=x; } int main(){ int x=5,y=10; fun(x,y); fun(y,x); cout private: int age; string name; public: Student(); Student(int m,string n); void SetName(int m,string n); int Getage(); string Getname(); }; Student::Student(){} Student::Student(int m,string n){ age=m; name=n; } void Student::SetName(int m,string n){ age=m; name=n; } int Student::Getage(){ return age; } string Student::Getname(){ return name; } int main(){ Student stu[3]={Student(15,"小红")}; stu[2].SetName(16,"小明"); cout private: string brand; string type; int year; double price; public: Car(); Car(string b,string t,int y,int p); string GetBrand(); string GetType(); int GetYear(); double GetPrice(); ~Car(); }; Car::Car(){} Car::Car(string b,string t,int y,int p){ brand=b; type=t; year=y; price=p; } string Car::GetBrand(){ return brand; } string Car::GetType(){ return type; } int Car::GetYear(){ return year; } double Car::GetPrice(){ return price; } Car::~Car(){} int main(){ Car c("五菱战神","究极战机",2077,10000000); cout private: double X,Y; public: Point(double a,double b); Point(Point &p); double GetX(); double GetY(); }; class Line{ private: Point A,B; double length; public: Line(Point p1,Point P2); double GetLength(); }; Point::Point(double a,double b){ X=a; Y=b; } Point::Point(Point &p){ X=p.X; Y=p.Y; } double Point::GetX(){ return X; } double Point::GetY(){ return Y; } Line::Line(Point p1,Point p2):A(p1),B(p2) { length=sqrt((p1.GetX()-p2.GetX())*(p1.GetX()-p2.GetX())+(p1.GetY()-p2.GetY())*(p1.GetY()-p2.GetY()));//因为x,y是私有成员变量,所以不能直接进行访问,要通过成员函数进行间接访问。 } double Line::GetLength(){ return length; } int main(){ Point A(1,1),B(2,2); Line line(A,B); cout private: int age; string name; public: static int count; Student(int m,string n); Student(); ~Student(); void Print() const; }; int Student::count=0; Student::Student(){ age=0; name="NoName"; count++; } Student::Student(int m,string n){ age=m; name=n; count++; } Student::~Student(){ count--; } void Student::Print()const{ cout cout private: int x,y,z; public: Point(int a,int b,int c); friend void operator (Point &p1,Point &p2); friend ostream & operator x=a; y=b; z=c; } void operator (Point &p1,Point &p2) { int d1,d2; d1=(p1.x)*(p1.x)+(p1.y)*(p1.y); d2=(p2.x)*(p2.x)+(p2.y)*(p2.y); if(d1d2){ cout cout out Point A(1,1,1); Point B(2,2,2); cout public: int row,col; int *m; Matrix(); Matrix(int r,int c,int *ma); Matrix(const Matrix &); ~Matrix(); friend Matrix operator + (Matrix &a,Matrix &b); friend Matrix operator - (Matrix &a,Matrix &b); friend Matrix operator * (Matrix &a,Matrix &b); Matrix & operator = (const Matrix &mat);//理解为返回一个矩阵类的引用数据类型 Matrix & operator += (Matrix &mat); void show(); }; Matrix::Matrix(){ row=0; col=0; m=NULL; } Matrix::Matrix(int r,int c,int *ma){ row=r; col=c; m=new int [row*col]; for(int i=0;i *(m+i)=*(ma+i); } } Matrix::Matrix(const Matrix &mat){ m=new int [mat.row*mat.col]; row=mat.row; col=mat.col; for(int i=0;i for(int j=0;j *(m+i*col+j)=*(mat.m+i*col+j); } } } Matrix::~Matrix(){ delete [] m; } Matrix operator + (Matrix &a,Matrix &b){ Matrix mat(a); if(a.row !=b.row || a.col!=b.col){ return mat; } for(int i=0;i for(int j=0;j *(mat.m+i*mat.col+j)+=*(b.m+i*b.col+j); } } return mat; } Matrix operator - (Matrix &a,Matrix &b){ Matrix mat(a); if(a.row !=b.row || a.col!=b.col){ return mat; } for(int i=0;i for(int j=0;j *(mat.m+i*mat.col+j)-=*(b.m+i*b.col+j); } } return mat; } Matrix operator * (Matrix &a,Matrix &b){ Matrix temp; if(a.col!=b.row) return a; temp.m=new int[a.row*b.col]; temp.row=a.row; temp.col=b.col; for(int i=0;i for(int j=0;j *(temp.m+i*temp.col+j)=0;//进行初始化,防止系统自动生成随机数。 for(int k=0;k *(temp.m+i*temp.col+j)=*(temp.m+i*temp.col+j)+ *(a.m+i*a.col+k)**(b.m+k*b.col+j); } } } return temp; } Matrix& Matrix::operator= (const Matrix &mat){ delete [] m ; m=new int[mat.row*mat.col]; row=mat.row; col=mat.col; for(int i=0;i for(int j=0;j *(m+i*col+j)=*(mat.m+i*mat.col+j); } } return *this; } Matrix& Matrix::operator+= (Matrix &mat){ if(col!=mat.col || row!=mat.row) return *this; for(int i=0;i for(int j=0;j *(m+i*col+j)=*(m+i*col+j)+*(mat.m+i*mat.col+j); } } return *this; } void Matrix::show(){ for(int i=0;i cout cout int a[3*3]={1,2,3,4,5,6,7,8,9}; int b[3*3]={9,8,7,6,5,4,3,2,1}; Matrix am(3,3,a),bm(3,3,b),cm; cout double real,imag; public: comp(double r=0,double i=0); friend comp operator+(const comp &s,const comp &t); friend ostream &operator private: double real,imag; public: comp(double r=0,double i=0); friend comp operator+(const comp &s,const comp &t); friend ostream &operator real=i; imag=i; } comp operator +(const comp &s,const comp &t){ comp tem; tem.real=s.real+t.real; tem.imag=s.imag+t.imag; return tem; } ostream & operator out comp a(1,1),b(2,2),c; c=a+b; cout double real,imag; public: comp(double r=0,double i=0); //friend bool operator(const comp &s,const comp &t);//比较到原点的距离,该处题目有问题。 //换成 friend bool operator (const comp &t,const comp &i); friend ostream &operator private: double real,imag; public: comp(double r=0,double i=0); friend bool operator(const comp &t,const comp &i);//比较到原点的距离 friend ostream &operator real=i; imag=i; } bool operator(const comp &t,const comp &i){ double d1,d2; d1=t.real*t.real+t.imag*t.imag; d2=i.real*i.real+i.imag*i.imag; return d1d2 ? true : false; } ostream & operator out comp a(1,1),b(2,2); cout cout cout private: int age; string name; public: stu(); stu(int a,string n); ~stu(); void changeAge(); void show(); }; stu::stu(){} stu::stu(int a,string n){ age=a; name=n; } stu::~stu(){} void stu::changeAge(){ age++; } void stu::show(){ cout stu s(18,"张三"); cout private: int y; int m; int d; public: Date(); Date(int a,int b,int c): y(a), m(b), d(c){}; void changeDay(); void show(); }; void Date::changeDay(){ d++; if(y%4==0 && y%100!=0){ if(m==2){ d 29 ? d=1,++m : ++d; } else if(m==4||m==6||m==9||m==11){ d 30 ? d=1,++m : ++d; }else{ d 31 ? d=1,++m : ++d; } }else{ if(m==2){ d 28 ? d=1,++m : ++d; } else if(m==4||m==6||m==9||m==11){ d 30 ? d=1,++m : ++d; }else{ d 31 ? d=1,++m : ++d; } } if(m12) m=1,y=y+1; } void Date::show(){ cout Date d(2024,2,29); d.show(); d.changeDay(); d.show(); return 0; } private: int y; int m; int d; public: Date(); Date(int a,int b,int c): y(a), m(b), d(c){}; Date(const Date &D);//深层拷贝 void show(); int gety(); int getm(); int getd(); void change(); }; int Date::gety(){ return y; }; int Date::getm(){ return m; }; int Date::getd(){ return d; }; Date::Date(const Date &D):y(D.y),m(D.m),d(D.d){ change(); } void Date::change(){ d++; if(y%4==0 && y%100!=0){ if(m==2){ d 29 ? d=1,++m : ++d; } else if(m==4||m==6||m==9||m==11){ d 30 ? d=1,++m : ++d; }else{ d 31 ? d=1,++m : ++d; } }else{ if(m==2){ d 28 ? d=1,++m : ++d; } else if(m==4||m==6||m==9||m==11){ d 30 ? d=1,++m : ++d; }else{ d 31 ? d=1,++m : ++d; } } if(m12) m=1,y=y+1; } void Date::show(){ cout Date d1(2024,2,29); d1.show(); Date d2=d1; d2.show(); return 0; } private: int x; int y; public: Point(){}; Point(int a,int b):x(a),y(b){}; friend Point& operator ++(Point &p); Point operator -(Point &p); void show(); }; Point& operator ++(Point &p){ ++p.x; ++p.y; return p; } //成员函数 Point& Point::operator ++(){ ++x; ++y; return *this; } //后置 Point operator++(Point& p, int) // 后置++重载的友元函数定义 { Point temp(p.x, p.y); p.x++; p.y++; return temp; } Point Point::operator -(Point &p){ return Point(x-p.x,y-p.y); } void Point::show(){ cout Point a(0,0),b(2,2),c; ++a; c=a-b; c.show(); return 0; }
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。