2 回答
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
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;
}
- 2 回答
- 0 关注
- 314 浏览
添加回答
举报