为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

猛跑小猪 2021-11-24 21:11:29
设计一个father类,一个mother类和一个child类。期中child类继承father类和mother类。father类和mother类都包含姓和名两个数据成员,child类仅包含名数据成员。要求一个child类的对象能够输出其父,其母和自己的姓名。
查看完整描述

2 回答

?
慕的地8271018

TA贡献1796条经验 获得超4个赞

#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



查看完整回答
反对 回复 2021-11-28
?
三国纷争

TA贡献1804条经验 获得超7个赞

#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;
}



查看完整回答
反对 回复 2021-11-28
  • 2 回答
  • 0 关注
  • 314 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信