帮我写段程序哈(c++)…谢谢。

来源:百度知道 编辑:UC知道 时间:2024/06/06 04:23:09
有如下一个类层次,每一个国家的人都会用自己的母语说你好,用相应的函数描述就是

SayHello()
{
Cout<<”Say Hello in”<<motherlanguage<<endl;
}

请将上述函数作为类的成员函数,编写一个程序,其中基类名为CMAN,并用如下的代码测试你的程序:

CMAN *p;
P=&(你写的继承类的对象);
p->SayHello();

要求给出程序的执行结果(每一个类都要用上述代码测试)。

#include <iostream>
using namespace std;

class CMAN
{
public:
char motherlanguage[100];
virtual void SayHello()
{
cout<<"Say Hello in "<<motherlanguage<<endl;
};
};

class Chinese:CMAN
{
public:
Chinese(){strcpy(motherlanguage, "Chinese");};
};

class French:CMAN
{
public:
French(){strcpy(motherlanguage, "French");};
};

class American:CMAN
{
public:
American(){strcpy(motherlanguage, "American");};
};

int main()
{
CMAN *a;
Chinese b;
French c;
American d;
a = (CMAN*)&b;
a->SayHello();
a = (CMAN*)&c;
a->SayHello();
a = (CMAN*)&d;
a->SayHello();
return 0;
}