一、选择题
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
#include
class person
{
public:
person(int n,char* nam,char s)
{
num=n;
strcpy(name,nam);
sex=s;
cout<<"Constructor called."<
}
~person( )
{
cout<<"Destructor called."<
}
void display( )
{
cout<<"num: "<
cout<<"name: "<
cout<<"sex: "<
}
private:
int num;
char name[10];
char sex;
};
int main( )
{
person s1(10010,"'Wang_li",'f');
s1.display( );
person s2(10011,"Zhang_fun",'m');
s2.display( );
return 0;
}
五、编程题
5.1
#include
using namespace std;
class CBox
{
public :
CBox(double l=0,double w=0,double h=0);
double area();
double volume ();
private :
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);
cout<
cout<
}
5.2
#include
using namespace std;
class CPoint
{
public :
CPoint(double a=0,double b=0)
{
x=a;
y=b;
}
CPoint(CPoint & p)
{
x=p.x;
y=p.y;
}
void print()
{
cout<<"("<
}
private :
double x,y;
};
class CLine
{
public:
CLine(double x1=0,double y1=0,double x2=0,double
{
}
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";
p2.print();
cout<
}
void Linedel()
{
cout<<"delete line"<
}
void move(CPoint &x,CPoint &y)
{
cout<<"move line"<
p1=x;
p2=y;
}
private :
CPoint p1,p2;
};
void main()
{
CPoint point1(1,5),point2(5,8),point3(20,30),poin
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
using namespace std;
class CComplex
{
public:
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<<"调用两个参数的构造函数"<
}
CComplex::CComplex (CComplex &c) //复数类的拷贝构造函数定义
{
real = c.real;
imag = c.imag;
cout<<"调用拷贝构造函数"<
}
double CComplex::GetReal(){return real;}
double CComplex::GetImag(){return imag;}
void CComplex::Print()// 显示复数值
{
cout << "(" << real << "," << imag << ")" << endl;
}
CComplex add(CComplex &x,CComplex &y) //普通函数完
{
return CComplex(x.GetReal() +y.GetReal() ,x.GetI
}
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.Print();
cout<<"c=a+b"<
c=add(a,b);
cout << "c = ";
c.Print ();
}
5.4
#include
#include
using namespace std;
class CStudent //类声明
{
public:
CStudent(char *,float,float,float);
CStudent(CStudent &s);
~CStudent();
void display();
friend float avg(CStudent &s);
private:
char *name;
float grad[3];
};
CStudent::CStudent(char *na,float a,float b,float
{
name=new char[strlen(na)+1];
strcpy(name,na);
grad[0]=a;
grad[1]=b;
grad[2]=c;
}
CStudent::CStudent(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:"<
for(i=0;i<3;i++)
cout<<"grad["<
}
float avg(CStudent &s) //普通函数,需要引用私有成员,声明为学生类的友元函
{
return (s.grad[0]+s.grad[1] +s.grad[2])/3;
}
int main( )
{
CStudent stud1("张三",79,98,82);//定义对象
stud1.display();
return 0;
}
5.5
#include
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) 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)
strcpy(s,stringser.s);
}
CString::CString(char ch,int nRepeat)
{
s=new char[nRepeat+1];
for(int i=0;i
s[i]=ch;
s[nRepeat]='\0';
}
CString::CString(const char*psz)
{
s=new char[strlen(psz)+1];
if(s!=0)
strcpy(s,psz);
}
int CString::GetLength() const
{ int i=0;
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."<
}
char CString::GetAt(int nindex) const
{
if(nindex>1&&nindex<=GetLength()+1)
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<
}
int CString::compare(CString& string)
{
if(strcmp(s,string.s)>0)
return 1;
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<
if(s!=NULL)
delete[]s;
}
void main()
{
char a[4]="y";char b[4];
CString c1("Hellow"),c2(c1);
cout<<"Stringc1 is:"<<" "; c1.Print();
cout<
cout<<"Stringc2 is:"<<" "; c2.Print();
cout<
CString c3('b',3);
cout<<"Stringc3 is:"<<" "; c3.Print();
cout<
cout<<"*******************以下是对串的基本操作************
int num=c1.GetLength();
char ch;
cout<<"c1的长度是:"<<" "<
ch=c1.GetAt(5);
cout<<"获得字符串c1中第"<<5<<"个字符是:"<
cout<<"下面是插入字符串c1中一个特殊字符'd'"<
c1.Setat(5,'d');
cout<<"插入后字符串c1变为:"<<" ";
c1.Print();
//////////////////////
cout<
if(c1.IsEmpty()==1)
cout<<"String is empty."<
else
cout<<"String isn't empty."<
cout<<"下面是判断字符串c1清空"<
c1.Vacate();
cout<<"清空后输出字符串c1:"<<" \n";
c1.Print();
cout<<"字符串已被清空"<
cout<<"请按任意键继续"<
cin>>b;
///////////////////////////
cout<<"****************以下是对串的赋值*****************
CString c5=c2;
cout<<"String c5=c2 is:"<<" ";c5.Print();
cout<
cout<<"****************以下是对串的比较*****************
int temp=c2.compare(c3);
cout<<"以下比较c2与c3"<
if(temp>0)
cout<<"Stringc2>Stringc3"<
else if(temp<0)
cout<<"Stringc2
else
cout<<"Stringc9==Stringc4"<
cout<
cout<<"以下比较c2与任意字符串Goodboy!"<
if(c2.compare("Goodboy!")>0)
cout<<"Stringc2>'Goodboy!'"<
else if(c2.compare("Goodboy!")<0)
cout<<"Stringc2<'Goodboy!"<
else
cout<<"Stringc2 =='Goodboy!'"<
}
第二章 继承和派生
一、选择题
1.D 2.B 3. D
一、阅读程序题
四、编程题
第3章 多态性
3.1 简答题
3.2 填空题
习题四