Father.h------------------
#pragma once
#include <iostream>
using namespace std;
class CFather
{
public:
CFather(string strXing,string strProvince,int iAge);
virtual ~CFather();
string GetXing();
string GetProvince();
int GetAge();
private:
string m_strXing;
string m_strProvince;
int m_iAge;
};
Father.CPP---------------
#include "Father.h"
CFather::CFather(string strXing, string strProvince, int iAge)
{
this->m_strXing = strXing;
this->m_strProvince = strProvince;
this->m_iAge = iAge;
}
CFather::~CFather()
{
}
std::string CFather::GetXing()
{
return m_strXing;
}
std::string CFather::GetProvince()
{
return m_strProvince;
}
int CFather::GetAge()
{
return m_iAge;
}
Son.h---------------------
#pragma once
#include <iostream>
using namespace std;
#include "Father.h"
class CSon :
public CFather
{
public:
CSon(string strXingTmp, string strProTmp, int iAgeTmp,string SonName);
virtual ~CSon();
string m_strSonName;
string GetSonName();
};
Son.cpp---------------------
#include "Son.h"
CSon::CSon(string strXingTmp, string strProTmp, int iAgeTmp, string SonName) :
CFather(strXingTmp, strProTmp, iAgeTmp) //通过子类构造函数给父类构造函数传递参数同时也传递给
//子类自己其他成员初始值
{
this->m_strSonName = SonName;
}
CSon::~CSon()
{
}
std::string CSon::GetSonName()
{
return this->m_strSonName;
}
main.cpp--------------------
#include "Son.h"
#include <iostream>
#include <string>
using namespace std;
void test()
{
CSon son("李", "江苏", 40, "lele");
cout << "sonname:" << son.GetSonName() <<"姓:"<<son.GetXing()<<"省份:"<<son.GetProvince()<<"父亲年龄:"<<son.GetAge()<< endl;
}
int main()
{
test();
system("pause");
return EXIT_SUCCESS;
}