面向对象程序设计(C++)复习题 联系客服

发布时间 : 星期四 文章面向对象程序设计(C++)复习题更新完毕开始阅读c03e8d7eb90d6c85ec3ac67a

delete []p; cout<

19. 19.

#include class A {

int a[10]; int n; public:

A(int aa[], int nn): n(nn) {for(int i=0; i

int SumA(int n) { int s=0; for(int j=0; j

void main() {

int a[]={1,1,2,3,5,8}; A x(a,3), y(a,4); int d=1;

for(int i=0; i<3; i++) d*=x.Get(i); int f=y.SumA(4); cout<<\ cout<<\}

20. 20.

#include class BB { int a,b; public:

BB(int i=0,int j=1) { a=i; b=j;}

BB operator +(BB c) { BB d; d.a=a + c.a; d.b=b+c.b; return d;} BB operator -(BB c) { BB d; d.a=a - c.a; d.b=b-c.b; return d;} void show()

{cout<<'('<

void main()

{ BB x(10,15) , y(5,7),z;

z=x+y; z.show(); z=x-y; z.show(); } 21. 21.

#include #include class Point { int x,y; public:

Point(int x1=0, int y1=0) :x(x1), y(y1) {cout<<\ ~Point() { cout<<\

};

class Text {

char text[100]; public:

Text(char * str) {strcpy(text,str);cout<<\ ~Text() {cout<<\};

class CircleWithText : public Point,public Text { public:

CircleWithText(int cx,int cy, char *msg):

Point(cx,cy),Text(msg) {cout<<\~CircleWithText() {cout<<\};

void main()

{ CircleWithText cm(3,4,\22. 22.

#include class Croot

{ public: int x;

Croot( ) {x=2;cout<<\

Croot(int n) { x=n; cout<<\ void showsmall() { cout<<\};

class Cder1: public Croot

{ public: Cder1(int m) :Croot(m){} }; class Cder2: public Croot { public: int x;

Cder2(int n=0) { x=n; } };

void main()

{ Croot A; Cder1 bb(4); Cder2 cc;

A.showsmall(); bb.showsmall(); cc.showsmall(); }

23. 23.

#include class Croot { public: int small;

Croot( ) { small=2;cout<<”Called Constructor1.\\n”;} Croot(int n) { small=n; cout<<”Called Constructor2.\\n”;} void showsmall() { cout<<\};

class Cder1: public Croot

{ public:

Cder1(int m) :Croot(m){} };

class Cder2: public Croot { public: int small;

Cder2(int n=0) { small=n; } };

void main() { Croot A; Cder1 bb(4); Cder2 cc;

A.showsmall(); bb.showsmall(); cc.showsmall(); }

四 程序

1. 定义一个第3题的结构体类型的结构体数组stud,输入学生三门课成绩,然后按总分成绩排序后输出

学生成绩。

#include struct stud { int no;

char name[20]; int math; int eng; int c; int sum; } st[10];

int n=-1; //表示数组元素当前下标 void main() { int x=1,i,j; stud t;

cout<<\请输入学生记录,按0 结束\ while(x) { cin>>x;

if(x){ n++; //读入学号

st[n].no=x; //读入姓名

cin>>st[n].name>>st[n].math>>st[n].eng>>st[n].c; //读入三门课成绩 st[n].sum=st[n].math+st[n].eng+st[n].c; //计算三门课总分 }

else break; }

for(i=0;i

if(st[j].sum>st[j+1].sum)

{t=st[j];st[j]=st[j+1];st[j+1]=t;}

for(i=0;i<=n;i++) // 输出排序后学生信息 cout<

<<\

}

2. 声明一个哺乳动物Mammal类,再由此派生出狗Dog类,声明一个Dog类的对象,观察基类与派生类的

构造函数与析构函数的调用顺序。

#include class Mammal { public:

Mammal() { cout<<\ }

~Mammal() {cout<<\ } };

class Dog :public Mammal { public:

Dog() {cout<<\

~Dog() {cout<<\};

void main() { Dog b; }

3. 定义一个基类有姓名、性别、年龄,再由基类派生出教师类和学生类,教师类增加工号、职称和工资,

学生类增加学号、班级、专业和入学成绩。

#include #include #include

class base //定义一个基类 {protected:

char name[20]; //姓名 char sex[3]; //性别 int age; //年龄 ?? ?? };

class teacher:public base //基类派生出教师类 { int sno; //工号 char zc[20]; //职称 double wages; //工资 ?? ?? };

class student :public base //基类派生出学生类 { int sno; //学号 char bj[10]; //班级 char zy[10]; //专业 double score; //入学成绩 ?? ??

};

4. 建立一个基类Building ,用来存储一座楼房的层数、房间数以及它的总平方英尺数。建立派生类

Housing,继承Building,并存储下面的内容:卧室和浴室的数量,另外,建立派生类Office,继承Building,并存储灭火器和电话的数目。然后,编制应用程序,建立住宅楼对象和办公楼对象,并输