题目内容

请将如下程序补充完整,使程序的输出结果如下: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;}

虚函数声明一个Shape基类,在此基础上公有派生出矩形Rectangle类和圆Circle类,二者都有函数GetArea()计算对象的面积,利用矩形类公有派生一个正方形Square类。具体要求如下:(1) Shape基类有以下公有成员:构造函数,函数体为空float GetArea():虚函数,其返回值为-1void Print():虚函数,函数体为空(2) Rectangle类的公有成员为:构造函数float GetArea(): 计算矩形的面积void Print(): 输出面积和长、宽私有成员为:float width: 矩形的宽float length: 矩形的长(3) Circle类的公有成员为:构造函数float GetArea(): 计算圆的面积void Print(): 输出面积和半径私有成员为:float radius: 圆的半径(4) Square类的公有成员为:构造函数(5)主函数中通过Shape指针sp来实现对其他派生类对象的操作。

对Point类重载++(自增)、--(自减)运算符

定义一个基类BaseClass,从它派生出类DerivedClass,BaseClass有成员函数fn1()、fn2(),fn1()是虚函数,DerivedClass也有成员函数fn1()、fn2(),在主程序中定义一个DerivedClass的对象,分别用BaseClass和DerivedClass的指针来调用fn1()、fn2(),观察运行结果。

答案查题题库