想用C++写一个程序,求问有了解的吗?

设计一个father类,一个mother类和一个child类。期中child类继承father类和mother类。father类和mother类都包含姓和名两个数据成员,child类仅包含名数据成员。要求一个child类的对象能够输出其父,其母和自己的姓名。

猛跑小猪
浏览 267回答 2
2回答

慕的地8271018

#include <string>#include <iostream>using namespace std;//class father;//class mother;//class child;class father{public:father()//构造函数{firstname="f";lastname="F";}~father()//析构函数{firstname.~string();lastname.~string();}father(const string first,const string last)//带参数的构造函数{}string getFirst()//取出father的名{return firstname;//="f";}string getLast()//取出father的姓{return lastname;//="F";}private:string firstname;//名string lastname;//姓};//此处一定要有分号class mother{public:mother()//mother的构造函数{firstname="m";lastname="M";}~mother()//析构{firstname.~string();lastname.~string();}string getFirst()//取出mother的名{return firstname;//="m";}string getLast()//取出mother的姓{return lastname;//="M";}private:string firstname;string lastname;};class child:public father,public mother{public:child(){ firstname="c"; }~child(){ firstname.~string(); }void showFa(){cout<<"Father's name is:";cout<<father::getFirst()<<father::getLast()<<endl;}void showMo(){cout<<"Mother's name is:";cout<<mother::getFirst()<<mother::getLast()<<endl;}void showCh(){cout<<"Child's name is:";cout<<firstname<<father::getLast()<<endl;//子随父姓}private:string firstname;};int main(int argc, char* argv[]){child ch;ch.showFa();ch.showMo();ch.showCh();return 0;}出以下错误的话在首行加#include "stdafx.h"e:\workspace\vc\baidu2\baidu2.cpp(102) : fatal error C1010: unexpected end of file while looking for precompiled header directive

三国纷争

#include <iostream.h>#include<string.h>class father{char firstname[10];char lastname[10];public:void print(){cout<<"父亲的姓是:"<<firstname<<endl;cout<<"父亲的姓是:"<<lastname<<endl;}father(){strcpy(firstname,"佐");strcpy(lastname,"助");}};class mather{char firstname[10];char lastname[10];public:void print(){cout<<"母亲的姓是:"<<firstname<<endl;cout<<"母亲的姓是:"<<lastname<<endl;}mather(){strcpy(firstname,"小");strcpy(lastname,"樱");}};class child:public father,mather{char lastname[10];public:void print(){father::print();mather::print();cout<<"小孩的名是:"<<lastname<<endl;}child(){strcpy(lastname,"宇智波");}};int main(){child Child;Child.print();return 0;}
打开App,查看更多内容
随时随地看视频慕课网APP