一、单项选择题 (在每小题列出的四个备选项中只有一个是符合题目要求的。) 二、填空题 1. 假设int a=1,b=2;,则表达式(++a/b)*b--的值为______。
3. 执行下列程序
double a=3.1415926,b=3.14;
cout<<setprecision(5)<<a<<","<<setpreeision(5)<<b<<endl;
程序的输出结果是______。
4. 在程序中执行int*a=new int之后,若a为空指针,则表明______。
5. 若要使用string类,需要引入的头文件是______。
6. 在C++中,编译指令都是以______(符号)开始。
7. 假定用户为类A定义了一个构造函数"A(int i=0):x(i){}",则与该语句等价的定义构造函数的格式为______。
8. 为了在对象生存期结束时释放其指针成员所指向的动态存储空间,通常为该类定义______。
10. 在已经定义了整型指针ip后,为了得到一个包括10个整数的数组并由ip所指向,应使用语句______。
11. C++函数分为库函数(标准函数)和______两类。
12. 设类A有成员函数void f(void);,若要定义一个指向类成员函数的指针变量pf来指向f,该指针变量的声明语句是______。
void(A::*pf)(void)=&A::f;
13. 在单一继承和多重继承方式中,面向对象程序设计应尽量使用______继承。
14. 继承的方式有公有继承、私有继承和______。
15. C++函数的返回值类型可以是除______与函数以外的任何类型。
16. vector类中用于删除向量中的所有对象的方法是______。
17. 一个抽象类的派生类可以实例化的必要条件是实现了所有的______。
18. 在C++中,虚函数帮助实现了类的______。
19. 如果在派生类中要访问被重定义了的基类同名函数,那么需使用______才可调用。
20. 如果在类模板的定义中有一个静态数据成员,则在程序运行中会产生______静态变量。
三、改错题 1. #include<iostream.h>
class A
{public:
A(int i=8):y(i){}
static int x:
int y;
};
int x=20;//对类成员初始化
void main()
{A a;
cout<<a.x<<","<<a.y<<endl;
}
int x;静态数据成员初始化,需要在类外进行,格式错误。应改为int A::x=20;。
2. #include<iostream.h>
class A
{ int a,b;
const int c;
public:
A():c(0),a(0),b(0){}
A(int aa,int bb):c(aa+bb)
{a=aa;b=bb;}
void show()
{cout<<"const c:"<<c<<endl;
cout<<"a,b:"<<a<<b<<endl;
}
};
void main()
{A a,b(1,2);
A*x=&a;
x.show();
b.show();
}
x.show();错误,x是指针引用成员有两种方法:一是利用指向运算符"->",二是使用成员运算符"."。应改为x->show();或者(*x).show();
3. 运行程序,从键盘上输入“This is a C++prog!”后输出到一个文件中,并显示在屏幕上,显示结果:
This is a C++prog!。
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void main()
{char str[20];
ofstream outf("D:\tem.dat",ios::trunc);
cin>>str;
outf<<str;
outf.close();
int i=0;
while(str[i]!='\0')
{cout<<str[i];
i++;
}
cout<<endl;
}
cin>>str;使用cin进行输入时空格也是作为结束符,所以str数组不能接收完整的字符串。应改为cin.getline(str,20);。
4. #include<iostream>
#include<fstream>
#include<string>
using namespace std;
class A
{public:
A(const char*na){strcpy(name,na);}
private:
char name[80];
};
class B:public A
{public:
B(const char*nm):A(nm){}
void show();
};
void B::show()
{cout<<"name:"<<name<<endl;
}
void main()
{B b1("B");
b1.show();
}
private:因为name如果是私有的,在派生类中无法访问,而基类没有提供成员函数来访问name,所以更改name访问权限为公有或保护,这样对于派生类来说是透明的。应改为public:或protected:。
5. #include<iostream.h>
class A
{private:
int x;
public:
A(int i){x=i;}
A(){x=0;}
friend int min(A&,A&);
}:
int min(A&a,A&b)
{return(a.x<b.x)? a.x:b.x;
}
void main()
{A a(3),b(5);
cout<<a.min(a,b)<<endl;
}
cout<<a.min(a,b)<<endl;友元函数不是类成员,所以对象a不能使用a.min(a,b)这种方法。min就是一个普通的友元函数,应改为cout<<min(a,b)<<endl;。
四、完成程序题 1. 完成下面类中成员函数的定义。
#include<iostream.h>
class vehicle
{protected:
int size;
int speed;
public:
void set(int s){speed=s;}
______get(){return speed/10;}
};
class car:public vehicle
{public:
int get(){return speed;}
};
class truck:public vehicle
{public:
int get(){return speed/2;}
};
int max(______)
{if(v1.get()>v2.get())
return 1;
else
return 2;
}
void main()
{ truck t;
car c;
t.set(160);
c.set(80);
cout<<max(t,c)<<endl;//此结果输出为2
}
virtual int vehicle&v1,vehicle&v2
2. 在下划线处填上缺少的部分。
#include<iostream.h>
class A
{ int a,b;
public:
______//定义构造函数,使参数i和j的默认值为0
{a=i;b=j;}//在函数体中用i初始化a,用j初始化b
};
main()
{ A*p;
______;//调用带参构造函数生成由p指向的动态对象
//使a和b成员分别被初始化为4和5
}
A(int i=0,int j=0) p=new A(4.5)
3. 先为数组a输满数据,再为x输入一个数据,在数组a中找出第一个与x相等的元素并将其下标输出,若不存在这样的元素,则输出"Not found!"标志。
#include<iostream.h>
void main()
{int i,x,a[10],m;
cout<<"输入数组中的10个数据:";
for(i=0;i<10;i++)
cin>>a[i];
cout<<endl<<"输入要查找的一个数据:";
cin>>x;
cout<<x<<endl;
for(i=0;i<10;i++)
if(______){m=i;break;}
if(______)
cout<<x<<"在数组中的位置是:"<<m<<endl;
else
cout<<"Not found!\n"<<endl;
}
4. 完成下面类中成员函数的定义。
#include<iostream>
#include<string>
using namespace std;
class str
{private:
char*st;
public:
str(char*a)
{set(a);}
str&operator=(______)
{delete st;
set(a.st);
return*this;
}
void show(){cout<<st<<endl;}
~str(){delete st;}
void set(char*s)//初始化st
{______
strcpy(st,s);
}
};
void main()
{ str s1("he"),s2("she");
s1.show(),s2.show();
s2=s1;
s1.show(),s2.show();
}
str &a st=new char[strlen(s)+1];
5. 下面程序段用来求三角形的面积,首先进行判断,三边不符合组成三角形时,返回-1;符合时输出三角形面积。
#include<iostream.h>
#include<math.h>
double area(double a,double b,double c)
{if(______)
return -1;
else
{
double ar,1;
1=(a+b+c)/2;
ar=sqrt(1*(1-a)*(1-b)*(1-c));
return ar;
}
}
void main()
{double i=0,j=0,k=0;
cout<<"输入三角形三边:";
cin>>i>>j>>k;
double s=area(i,j,k);
if(s<0)
cout<<"不是三角形"<<endl;
else
______
}
a+b<=c||a+c<=b||b+c<=a cout<<s<<endl;
五、程序分析题 1. 给出下面程序的输出结果。
#include<iostream.h>
template<class T>
class Complex
{T real,image;
public:
Complex(T a,T b):real(a){image=b;}
Complex(T a){image=0,real=a;}
void pr()
{char c;
c=(image>0? '+':'-');
if(image!=0)
cout<<real<<c<<(image>0? image:-image)<<"i"<<endl;
else
cout<<real<<endl:
}
};
void main()
{Complex<double>a(16.5,-7.8);
a.pr();
Complex<int>b(6);
b.pr();
}
2. 给出下面程序的输出结果。
#include<iostream.h>
class example
{int a;
public:
example(int b=5){a=b++;}
void print(){a=a+1;cout<<a<<" ";}
void print()const
{cout<<a<<endl;}
};
void main()
{example x;
const exahaple y(2);
x.print();
y.print();
}
六、程序设计题 1. 已有复数类Complex和主函数main(),编写一个类Root来实现求解一元二次方程的程序。
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
class Complex
{public:
Complex(){}
void setC(double i,double j)
{real=i;image=j;}
void show();
protected:
double real,image;
};
void Complex::show()
{cout<<"("<<real<<","<<image<<"i)"<<endl;}
void main()
{double a,b,c;
cout<<"\n请输入一元二次方程的系数a,b,c:";
cin>>a>>b>>c;
Root root(a,b,c);
root.solve();
}
完整的程序如下: class Root {public: Root(double i,double j,double k) {a=i,b=j,c=k;} void show(); void solve(); protected: double a,b,c; }; void Root::show() {cout<<"方程"; cout<<a; cout<<setiosflags(ios::showpos); cout<<"*x*x": cout<<b<<"*x"; cout<<c<<"=0\n"; cout<<resetiosflags(ios::showpos); } void Root::solve() {show(); double x=-b/2/a; cout<<"的解是:\n"; double dt=b*b-4*a*c; Complex c1,c2; if(fabs(dt)<1e-5) {c1.setC(x,0),c2.setC(x,0); cout<<"\nx1=x2=": c1.show(); } else if(dt>0) {c1.setC(x+sqrt(dt)/2/a,0),c2.setC(x-sqrt(dt)/2/a,0); c1.show();c2.show(); } else {c1.setC(x,sqrt(-dt)/2/a); c2.setC(x,-sqrt(-dt)/2/a); c1.show();c2.show(); } }