请教一个继承类的问题

来源:百度知道 编辑:UC知道 时间:2024/06/21 06:24:01
题目是这样的:设计一个father类,一个mother类和一个child类。其中child类继承father类和mother类。father类和mother类都包含姓和名两个数据成员,child类仅包含各数据成员。要求一个child类的对象能够输出父母和自己的姓名。
我的程序:# include<iostream.h>
class father{
char a[100];
public:
father(){cin>>a;}
void print(){cout<<"父亲姓名:"<<a<<endl;}
};

class mother{
char b[100];
public:
mother(){cin>>b;}
void print(){cout<<"母亲姓名:"<<b<<endl;}
};
class child:public father,public mother{
char c[100];
public:
child(){cin>>c;cout<<"孩子姓名";}
};

void main(){
child d1;
d1.print();
}

刚学这个,编的好烂,希望大家能帮一下忙~~,如果能走成功,还有加分哦~~小女子拜谢~~^_^

这样比较好:

#include<iostream.h>
class father{
char a[100];
public:
father(){cout<<"输入父亲姓名:"<<endl;cin>>a;}
void outputName(){cout<<"父亲姓名:"<<a<<endl;}
};

class mother{
char b[100];
public:
mother(){cout<<"输入母亲姓名:"<<endl;cin>>b;}
void outputName(){cout<<"母亲姓名:"<<b<<endl;}
};

class child:public father,public mother{
char c[100];
public:
child(){cout<<"输入孩子姓名:"<<endl;cin>>c;}
void outputName(){ father::outputName(); mother::outputName();cout<<"孩子姓名:"<<c<<endl;}
};

void main(){
child d1;
d1.outputName();
}