64,642
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <cstdlib>
using namespace std;
class Grand
{
private:
int hold;
public:
Grand(int h = 0) : hold(h) {}
virtual void Speak() const { cout << "I am a grand class!\n";}
virtual int Value() const { return hold; }
};
class Superb : public Grand
{
public:
Superb(int h = 0) : Grand(h) {}
void Speak() const {cout << "I am a superb class!!\n"; }//这里没用virtual修饰
virtual void Say() const//这里使用了,和上面的函数有什么区别呢
{ cout << "I hold the superb value of " << Value() << "!\n";}
};
class Magnificent : public Superb
{
private:
char ch;
public:
Magnificent(int h = 0, char cv = 'A') : Superb(h), ch(cv) {}
void Speak() const {cout << "I am a magnificent class!!!\n";}
void Say() const {cout << "I hold the character " << ch <<
" and the integer " << Value() << "!\n"; }
};
int main()
{
Grand * pg;
Superb * ps;
pg=new Magnificent();
ps=new Magnificent();
pg->Speak();
ps->Speak();
return 0;
}