题目内容

有如下程序:#include using namespace std;class Animal{public:virtual char* getType()const { return "Animal";}virtual char* getVoice()const { return "Voice";}};class Dog: public Animal {public:char* getType ( ) const {return "Dog";}char* getVoice ( ) const {return "Woof";}};void type(Animal &a) {cout<

查看答案
更多问题

如下程序运行时的输出结果是 。#includeusing namespace std;class Base{public:Base(){ }virtual void f(){cout<<"A";}};class D1: public Base {public:D1(){ }virtual void f() const{cout<<"B";}};class D2: public D1 {public:virtual void f(){cout<<"C";}};void fun(Base &b){ b.f(); }int main(){Base b; D1 d1; D2 d2;fun(b);fun(d1);fun(d2);b.f();d1.f();d2.f();return 0;}

完善程序,使其运行时的输出结果如下:f1 function of derivef2 function of basef4 function of base#include using namespace std;class base{public:void f1(){cout<<"f1 function of base"<< endl; }virtual void f2(){cout<<"f2 function of base "<< endl; }void f4(){cout<<"f4 function of base "<f1();p->f2();p->f4();return 0;}

请将如下程序补充完整,使程序的输出结果如下:Derived::Print: 5Derived destructorBase destructor源程序清单:#include using namespace std;class BaseClass{public:~BaseClass();virtual void Print();};BaseClass::~BaseClass(){cout<< "Base destructor" << endl; }void BaseClass::Print(){cout << "BaseClass::Print" << endl;}class Derived:BaseClass{public:Derived();~Derived();void Print();private:int *p;};Derived::Derived(){p =;}Derived::~Derived() {cout << "Derived destructor" << endl;delete p;}void Derived::Print(){cout << "Derived::Print: " <<*p<< endl;}void fun(BaseClass* b) {b->Print();delete b;}int main() {BaseClass *ptr = ;fun(ptr);return 0;}

请改正程序中的错误(在对应空处写出错误所在行的完整一行,分号不写),使程序的输出结果如下:BaseClassDerived1Derived2#include using namespace std;class BaseClass {public:/********** Error found【1】**********/void Print() const;};void BaseClass::Print() const {cout << "BaseClass" << endl;}/********** Error found【2】**********/class Derived1::BaseClass{ public:void Print() const;};void Derived1::Print() const {cout << "Derived1" << endl;}class Derived2: public Derived1 {public:void Print() const;};void Derived2::Print() const {cout << "Derived2" << endl;}void Print(BaseClass &p) {/********** Error found【3】**********/p->Print();}int main() {BaseClass b; Derived1 d1; Derived2 d2;Print(b); Print(d1); Print(d2);return 0;}

答案查题题库