prog10.cpp

来自「C++语言程序设计题典」· C++ 代码 · 共 55 行

CPP
55
字号
#include <iostream.h>
#include <string.h>
class father
{
protected:
	char fname[5];     //姓
	char sname[10];    //名
public:
	father() {}
	father(char *fn,char *sn) { strcpy(fname,fn);strcpy(sname,sn); }
    char *getfname() { return fname; }
	void show() { cout << fname << sname; }
};
class mother
{
protected:
	char fname[5];     //姓
	char sname[10];    //名
public:
	mother() {}
	mother(char *fn,char *sn) { strcpy(fname,fn);strcpy(sname,sn); }
    char *getfname() { return fname; }
	void show() { cout << fname << sname; }
};
class child:public father,public mother
{
private:
	father *myfather;
	mother *mymother;
public:
	child(father &fa,mother &mo,char *na):myfather(&fa),mymother(&mo)
	{
		strcpy(father::fname,fa.getfname());  //这里的father::fname是指从
		strcpy(father::sname,na);             //father继承的fname
	}
	void show()
	{
		cout << "  姓名:";father::show();
		cout << endl;
		cout << "    父亲:";myfather->show();
		cout << endl;
		cout << "    母亲:";mymother->show();
		cout << endl;
	}
};
void main()
{
	father fa1("陈","国强"),fa2("王","伟");
	mother mo1("李","丽"),mo2("许","英");
	child ch1(fa1,mo1,"文化"),ch2(fa2,mo2,"萍");
	cout << "输出结果:" << endl;
	ch1.show();
	ch2.show();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?