word完美格式
}
(1)输出结果为: Instrument::Print Instrument::Print (2)输出结果为: Guitar::Print Piano::Print 10、 对虚函数使用对象指针或引用调用,系统使用__动态__联编:使用对象调用时,系统使用__静态__联编。
11、 将下列程序补充完整。 #include
public:
convert(double I){va11=I;}
____ virtual void compute()=0; ____ protected: double val1; double va12: };
//liters to gallons
class l_to_g:public convert {
public:
__ l_to_g(double I):convert(I){} ____ void compute() {
va12=va11/3.7854;
cout< //Fahrenheit to Celsius class f_to_c:public convert { public: f_to_c(double I):convert(I){} void& compute() { va12=(va11-32)*5/9; cout< 精心整理 学习帮手 word完美格式 void fun(__convert& f____) { f.compute(); } void main() { l_to_g lgobj(4); f_to_c fcobj(70); fun(lgobj); fun(fcobj); } 12、 下列程序的运行结果如下: Derive1's Print() called. Derive2's Print() caIIed. 根据结果将程序补充完整。 #include public: Base(int I){b=I;} __ virtual Print()=0; ____ protected: int b; } class Derive1:public Base { public: __ Derive1(int& I):Base(I){} ___ void Print() { cout<<\ } }; class Derive2:public Base { public: Derive2(int I):Base(I){} void Print(){cout<<“Derive2’s Print() called.”< void fun(__Base *obj ____) { obj->Print(); }; 精心整理 学习帮手 word完美格式 void main() { Derive1 *d1=new Derive1(1); Derive2 *d2=new Derive2(2); fun(d1); fun(d2); } 13、 在一个成员函数内调用一个虚函数时,对该虚函数的调用进行_动态__联编。 14、 带有__纯虚函数__的类称为抽象类,它只能作为__基类__来使用。 4.3简答题 A、成员函数重载与虚函数的区别 B、静态联编和动态联编的含义 C、纯虚函数和抽象类的定义及其关系 D、抽象类的特点 4.4读程序题 1、写出下列程序的运行结果。 #include public: Base(int i){x=i;cout<<\ virtual ~Base(){cout<<\private: int x; }; class Derived :public Base { public: Derived(int i,int j):Base(i){y=j;cout<<\ ~Derived(){cout<<\private: int y; }; void main() { Base *b1=new Base(15); delete b1; Base *b2=new Derived(20,25); delete b2; 精心整理 学习帮手 word完美格式 } 答案 Base:15 ~Base:15 Base:20 Derived:25 ~Derived:25 ~Base:20 2、 阅读下列程序,回答后面提出的问题。 #include public: Base():x(0),y(0){} Base(int I,int j):x(I),y(j){} int GetX() const{return x;} int GetY() const{return y;} virtual void Print() const{cout<<\ virtual ~Base(){Print();} private: int x,y; }; class Derived :public Base { public: Derived():z(0){} Derived(int I,int j,int k):Base(I,j),z(k){} void Print() const {cout<<\ ~Derived(){Print();} private: int z; }; void main() { Base *d1=new Derived; d1->Print(); Base *d2=new Derived(10,20,30); d2->Print(); delete d2; delete d1; } 精心整理 学习帮手