1、第1章 类和对象
一、 选择题
1.C 2.B 3.C 4.A 5.C
6.A 7.C 8 C 9A 10 C
二、阅读题
1.x=2,y=3
2.x=2,y=3
x!=y
3.
Cstatic::va1=0
cs1.vaI=1
cs2.val=2
cs1.val=4
cs2.vaI=4
四、改错题
#include
2、nam,char s)
{
num=n;
strcpy(name,nam);
sex=s;
cout<<"Constructor called."< 3、y( )
{
cout<<"num: "< 4、
person s2(10011,"Zhang_fun",'m');
s2.display( );
return 0;
}
五、编程题
5.1
#include 5、 double lengh;
double width;
double high;
};
CBox::CBox(double l,double w,double h)
{
lengh=l;
width=w;
high=h;
}
double CBox::area()
{
return 2*(lengh*width+lengh*high+width*high);
}
double CBox::volume ()
{
return lengh*width*high;
}
void main()
{
CBox box1(4,5,6);
6、cout< 7、e x,y;
};
class CLine
{
public:
CLine(double x1=0,double y1=0,double x2=0,double y2=0):p1(x1,y1),p2(x2,y2)
{
}
CLine(CPoint x,CPoint y):p1(x),p2(y)
{
}
CLine(CLine &lin)
{
p1=lin.p1;
p2=lin.p2;
}
void DrawLine()
{
cout<<"Line form";
p1.print();
cout<<"to";
8、
p2.print();
cout< 9、40,50);
CLine line1(point1,point2);
CLine line2(2,3,8,12);
line1.DrawLine ();
line2.DrawLine ();
line2.move(point3,point4);
line2.DrawLine ();
line2=line1;
line2.DrawLine ();
line1.Linedel ();
}
5.3
#include 10、ic:
CComplex(double, double);
CComplex(CComplex &c); //复数类的拷贝构造函数声明
double GetReal();
double GetImag();
void Print();
private:
double real;
double imag;
};
CComplex::CComplex (double r=0.0, double i=0.0)
{
real = r;
imag = i;
cout<<"调用两个参数的构造函数"< 11、lex &c) //复数类的拷贝构造函数定义
{
real = c.real;
imag = c.imag;
cout<<"调用拷贝构造函数"< 12、) //普通函数完成两个数的加法,对象作为函数参数,
{
return CComplex(x.GetReal() +y.GetReal() ,x.GetImag ()+y.GetImag ());
}
void main(void)
{
CComplex a(3.0,4.0), b(5.6,7.9);
CComplex c(a); //调用复数类的拷贝构造函数
cout << "a = ";
a.Print();
cout << "b = ";
b.Print();
cout << "c = ";
c.Prin 13、t();
cout<<"c=a+b"< 14、
~CStudent();
void display();
friend float avg(CStudent &s);
private:
char *name;
float grad[3];
};
CStudent::CStudent(char *na,float a,float b,float c)
{
name=new char[strlen(na)+1];
strcpy(name,na);
grad[0]=a;
grad[1]=b;
grad[2]=c;
}
CStudent::CSt 15、udent(CStudent &s)
{
name=new char[strlen(s.name)+1];
strcpy(name,s.name);
grad[0]=s.grad[0];
grad[1]=s.grad [1];
grad[2]=s.grad [2];
}
CStudent::~CStudent()
{
delete []name;
}
void CStudent::display( )
{
int i;
cout<<"name:"< 16、t<<"grad["<
17、using namespace std;
class CString
{
public :
CString(); //缺省构造函数,初始化为空串
CString(char ch,int nRepeat);//用一个字符重复n次,初始化字符串
CString(const char*psz); //用一个字符串初始化
CString(CString &stringser); //拷贝构造函数
~CString();
int GetLength() const;
bool IsEmpty() const;
char GetAt(int nindex) 18、const;
void Setat(int nindex,char ch);
void Print();
int compare(CString& string);
int compare(const char* psz)const;
void Vacate();
private :
char *s;
};
CString::CString()
{
s=NULL;
}
CString::CString(CString& stringser)
{
s=new char[strlen(stringser.s)+1];
if(s!=0)
19、strcpy(s,stringser.s);
}
CString::CString(char ch,int nRepeat)
{
s=new char[nRepeat+1];
for(int i=0;i 20、
while (s[i]!='\0')
i++;
return i;
}
bool CString::IsEmpty() const
{
if(s==NULL)
return 1;
else
return 0;
}
void CString::Vacate()
{
s[0]='\0';
cout<<"Now have vacated string."< 21、 return s[ nindex-1];
else
return '0';
}
void CString::Setat(int nindex,char ch)
{
if(nindex>1&&nindex<=GetLength()+1)
s[ nindex-1]=ch;
}
void CString::Print()
{
cout< 22、
else if(strcmp(s,string.s)<0)
return -1;
else
return 0;
}
int CString::compare(const char* psz)const
{
if(strcmp(s,psz)>0)
return 1;
else if(strcmp(s,psz)<0)
return -1;
else return 0;
}
CString::~CString()
{
//cout< 23、lete[]s;
}
void main()
{
char a[4]="y";char b[4];
CString c1("Hellow"),c2(c1);
cout<<"Stringc1 is:"<<" "; c1.Print();
cout< 24、
cout<<"*******************以下是对串的基本操作****************"< 25、
c1.Print();
//////////////////////
cout< 26、t<<"请按任意键继续"< 27、c3);
cout<<"以下比较c2与c3"< 28、dl;
else if(pare("Goodboy!")<0)
cout<<"Stringc2<'Goodboy!"< 29、}
Circle (double d){r=d;}
double area(){return(PAI*r*r);}
void display1(){cout<<"桌子的面积:"< 30、ublic Circle,public Table
{
public:
Roundtable(){strcpy(color,"白色");}
Roundtable(double a,double b,char* c):Circle(a),Table(b){strcpy(color,c);}
void display ()
{ display1();
display2();
cout<<"桌子的颜色:"< 31、黑色");
s.display();
}
4.2
如果Age在基类中被定义为私有的,SetAge(int n)不能直接给Age赋值,如果Age是基类的公有成员变量,则可以直接赋值。
class Animal
{
public:
Animal(){};
int Age;
};
class Dog:public Animal
{
public:
Dog(){};
Void SetAge(int n)
{
Age=n;
}
};
4.3
#include 32、{
public:
Rectangle (double l,double w){length=l,width=w}
double area(){return length*width;}
void display1();
private:
double length;
double width;
};
void Rectangle::display1()
{cout<<"长:"< 33、le L,double w,double h):Rectangle(L,w){high=h,volume=L*w*high };
double vol(){return area()*high;}
void show ();
private:
double high;
double volume;
};
void Cuboid::show()
{
display1();
cout<<"高:"< 34、);
}
4.4
#include 35、rtual public Vehicle
{
public:
Bicycle (double m,double w,double h):Vehicle(m,w),height(h){}
private:
double height;
};
class Motorcar : virtual public Vehicle
{
public:
Motorcar (double m,double w,double s):Vehicle(m,w),setnum(s){}
private:
double setnum;
};
class Motorcycle:public Bi 36、cycle,public Motorcar
{
public:
Motorcycle(double m,double w,double h,double s):Bicycle(m,w,h),Motorcar(m,w,s), Vehicle(m,w){}
};
void main()
{
Motorcycle s1(300,10,6,30);
s1.run();
s1.stop();
}
如果不把Vehicle设置为虚基类,会产生二义性问题。
第3章 多态性
3.1 简答题
3.1.1 哪些运算符可以重载?
几乎所有的运算符都可以重载,除了以下的几个运算符: 37、
., :: ,?:
3.1.2 运算符重载后,优先级和结合性如何?
用户重新定义运算符,不改变原运算符的的优先级和结合性。同时运算符重载后,也不改变运算符的语法结构,即单目运算符只能重载为单目运算符,双目运算符只能重载为双目运算符。
3.1.3 编译程序如何选用运算符函数?
在每一种编译系统中,运算符实际上都对应一个函数,只是这种运算对用户具有透明性,使用者并不知道函数的存在。运算符重载实际上是运算符函数的重载,所以运算符的重载实际上是函数的重载。
编译程序对运算符重载的选择,遵循着函数重载的选择原则。当遇到不很明显的运算符时,编译程序将去寻找参数相匹配的运算符函数。
3 38、1.4 重载运算符有哪些限制?
(1) 不可臆造新的运算符
(2) 坚持四个不能改变。
l 不能改变运算符原有的优先级
l 不能改变运算符原有的结合性
l 不能改变运算符原有的语法结构
l 不能改变运算符原有的操作数个数
但允许改变
运算符的返回类型
(3)C++规定,运算符中,参数类型都是内部类型时,不允许重载。 “::、*、->、?:”这五个运算符不能重载。
3.1.5 运算符重载必须遵循哪些原则?
运算符重载可以使程序更加简洁,使表达式更加直观,增强可读性。但是,运算符重载使用不宜过多,否则会带来一定的麻烦。
运算符重载必须遵循以下原则:
(1) 重载运算符 39、含义必须清楚。
(2) 重载运算符不能有二义性。
3.2 填空题
3.2.1 C++中多态性包括两种多态性: (1) 和 (2) 。前者是通过 (3) 实现的,而后者是通过 (4) 和 (5) 来实现的。
答案:(1)编译时的
(2)运行时的
(3)函数和运算符的重载
(4)类继承关系
(5)虚函数
3.2.2 纯虚函数定义时在函数参数表后加 (1) ,它表明程序员对函数 (2) ,其本质是将指向函数体的指针定为 (3) 。
答案:(1)=0
(2)不定义
(3)NULL
3.2.3在基类中将一个成员函数说明成虚函数后,在其派生类中只要 (1) 40、 (2) 和 (3) 完全一样就认为是虚函数,而不必再加关键字 (4) 。如有任何不同,则认为是 (5) 而不是虚函数。除了非成员函数不能作为虚函数外, (6) 、 (7) 和 (8) 也不能作为虚函数。
答案:(1)同虚函数名
(2)同参数表
(3)同返回类型。如基类中返回基类指针,而派生类中返回派生类指针是允许的
(4)virtual
(5)重载
(6)静态成员函数
(7)内联函数
(8)构造函数
第三大题答案:
3.1答案:
#include 41、dent
{
char name[20];
char nativeplace[20];
char code[30];
int age;
float score;
public :
CStudent(char *, char*,char*,int ,float);
CStudent(CStudent &s);
void display();
float operator+(CStudent s1);
};
CStudent::CStudent(char *name, char*native,char*code,int age, 42、float score)
{
strcpy(this->name,name);
strcpy(this->nativeplace,native);
strcpy(this->code,code);
this->age=age;
this->score=score;
}
CStudent::CStudent(CStudent &s)
{
strcpy(this->name,s.name);
strcpy(this->nativeplace,s.nativeplace);
strcpy(this->code,s.code);
this->age=s.ag 43、e;
this->score=s.score;
}
void CStudent::display()
{
cout< 44、
CStudent c("ctm","zhengzhou","13783580182",30,90);
c.display();
cout< 45、t GetArea()
{
float t=(a+b+c)/2;
return sqrt(t*(t-a)*(t-b)*(t-c));
}
float operator +(CTriangle t)
{
return this->GetArea()+t.GetArea();
}
};
void main()
{
CTriangle tr1(3,4,5),tr2(6,8,10);
cout< 46、public:
virtual void f1()
{cout<<"BaseClass::f1()"< 47、
BaseClass* Bptr;
DerivedClass* Dptr;
Bptr=&d1;
Bptr->f1();
Bptr->f2();
Dptr=&d1;
Dptr->f1();
Dptr->f2();
}
习题四
一、选择题
1.D 2.A 3.B 4.C 5. C 6.C 7. A
二、简答题
1.什么叫做流?流的提取和插入是指什么?I/O流在C++中起着怎样的作用?
答:流是一种抽象,它负责在数据的生产者(程序/文件)和数据的消费者(文件/程序)之间建立联系,并管 48、理数据的流动。
一般意义下的读操作在流数据抽象中被称为(从流中)提取,写操作被称为(向流中)插入。
完成数据从动态(内存)到静态(外存)的转换,或着是从静态(外存)到动态(内存)的转换。
2.什么是字节流、字符流和二进制流?
答:根据对字节内容的解释方式,字节流分为字符流(也称文本流)和二进制流。
字符流将字节流的每个字节按ASCⅡ字符解释,它在数据传输时需作转换,效率较低。例如源程序文件和文本文件都是字符流。由于ASCⅡ字符是标准的,所以字符流可以直接编辑,显示或打印,字符流产生的文件通行于各类计算机。
二进制流将字节流的每个字节以二进制方式解释,它在数据传输时不作任何转换,故效 49、率高。但各类计算机对数据的二进制存放格式各有差异,且无法人工阅读,故二进制流产生的文件可移植性较差。
3.cerr和clog作用是什么?有何区别?
答:对于输入提示信息或输出结果而言, cerr和clog的用法相同,但作用不同。cerr的作用是向标准错误设备(standard error device)输出有关出错信息。cerr流中的信息只能在显示器输出。当调试程序时,往往不希望程序运行时的出错信息被送到其他文件,而要求在显示器上及时输出,这时应该用cerr,cerr流中的信息是用户根据需要指定的。clog流对象也是标准错误流,它是console log的缩写。它的作用和cerr相同,都是在终端显示器上显示出错信息。不同的是cerr不经过缓冲区,直接向显示器上输出有关信息,而clog中的信息存放在缓冲区中,缓冲区满后或遇endl时向显示器输出。
4.用什么方法来控制输入输出流中出现的错误?
答:为提高程序的可靠性,应在程序中检测I/O流的操作是否正常。当检测到流操作出现错误时,可以通过异常处理来解决问题。
5.比较读写文本文件与二进制文件的异同。
答:从文件编码的方式来看,文件可分为ASCII码文件和二进制码文件两种。
ASCII文件也称为文本文件,这种文件在磁盘中存放时每个字符对应一个字节,用于存放对应的ASCII码。例如,数0)
return 1;






