第八章 类和对象 复习题

{

obj.Print() ; }

void main(){

Guitar g; Tone(g); Piano p; Tone(p); }

输出结果为: Guitar::Print Piano::Print

33. #include

cout<<\ Instrument &obj

38.写出下列程序的运行结果。

#include class B1{ public: int Nv;

void fun(){cout<<“Member of B1.”<

void fun(){cout<<“Member of B2.”<

class D1:public B1, public B2{ public: int Nv;

void fun(){cout<<“Member of D1.”<

void main(){ D1 d1;

d1.Nv=1;d1.fun(); d1.B1::Nv=2;d1.B1::fun();

d1.B2::Nv=3;d1.B2::fun(); }

运行结果:

38. Member of D1. (2分)

Member of B1. (2分) Member of B2. (1分)

39 在划线处填上适当的语句,完成下列程序。

#include class Point{ public:

Point(double a,double b,double c){ X=a;Y=b;Z=c }//构造函数 double Getx(){return X;} double Gety(){return Y;} double Getz(){return Z;} private:

};

class Line : Point{ public:

Line(double a ,double b,double c,double d) { K=d; } void show(){

cout<

void main() {

Line obj(1.2,3.4,5.6,7.8);

obj.show();

}//运行结果是1.2 3.4 5.6 7.8

34. double X,Y,Z;

pubic :Point(a,b,c)

40.建立一个对象数组,内放5个学生的数据(学号,成绩),设立一个函数

max,用指向对象的指针作函数参数,在max函数找出5个学生中成绩最高者,并输出其学号。 41.

#include

using namespace std; (1分) class Student {public:

Student(int n,float s):num(n),score(s){} (1分) int num; float score; }; (1分) void main() {Student stud[5]={

Student(101,78.5),Student(102,85.5),Student(103,98.5), (1分) Student(104,100.0),Student(105,95.5)}; (1分) void max(Student* );

Student *p=&stud[0]; (1分) max(p); } (1分) void max(Student *arr)

{float max_score=arr[0].score; (1分) int k=0;

for(int i=1;i<5;i++)

if(arr[i].score>max_score) {max_score=arr[i].score;k=i;} (1分) cout<

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4