一步一步往上爬:
类的继承。
首先建立第一个类People。
1)People.h。
#ifndef PEOPLE_H
#define PEOPLE_H
#include<iostream>
using namespace std;
class People
{
public: //共有成员
People(); //People的默认的构造方法
void get_date(); //输入数据
void get_age(); //用来获取age
void get_sex(); //用来获取sex
void say_hello();
private: //私有成员
int age;
int sex; //我们约定1为男,0为女。
};
#endif // PEOPLE_H
2)People.cpp
#include "People.h"
People::People(){
this->age = 18;
this->sex = 1;
}//当我们不用那个构造方法时,
//我用的就是这个默认的构造方法。
void People::get_date(){
cin >> this->age;
cin >> this->sex;
}
void People::get_age(){
cout << this->age << endl;
}
void People::get_sex(){
cout << this->sex << endl;
}
void People::say_hello(){
cout << "hello world !" << endl;
}
1)tt.h。
<span style="font-size:18px;">
#ifndef TT_H
#define TT_H
#include "People.h"
class tt:public People //继承People类
{
public:
};
#endif // TT_H
</span>
主程序。
<span style="font-size:18px;">
#include "tt.h"
int main()
{
tt *p = new tt();
p -> say_hello();
p -> get_date();
p -> get_age();
p -> get_sex();
delete p;
return 0;
}
</span>
当我们不用get_date()时。既用了People的默认构造方法。
#include "tt.h"
int main()
{
tt *p = new tt();
p -> say_hello();
//p -> get_date();
p:People();
p -> get_age();
p -> get_sex();
delete p;
return 0;
}
感觉有点姿势不大对。。求大神们给具体讲讲类的继承吧。。
~~~~(>_<)~~~~
PS:滴水可穿石。