先不多说,来看看一个继承体系的完整书写方法:
People头文件:
#ifndef People1
#define People1
#include <string>
#include <iostream>
using namespace std;
class People
{
public:
//构造函数
People();
People(const string& name, int age);
//拷贝构造函数
People(const People& people);
//赋值运算符重载
People& operator = (const People& people);
virtual ~People();
virtual void jieshao();
private:
string name;
int age;
};
#endif
#include "People.h"
People::People() {
name = " ";
age = 0;
};
People::People(const string& name, int age) {
this->name = name;
this->age = age;
};
People::People(const People& people) {
this->name = people.name;
this->age = people.age;
};
People& People::operator = (const People& people) {
this->name = people.name;
this->age = people.age;
return *this;
};
People::~People() {
};
void People::jieshao() {
cout << "name" << this->name.c_str() << endl;
cout << "age" << this->age << endl;
};
Student.h及cpp
#ifndef Student1
#define Student1
#include <string>
#include "People.h"
using namespace std;
class Student :public People
{
public:
Student();
Student(string name, int age, int numb);
//拷贝构造函数
Student(const Student& student);
//赋值运算符重载
Student& operator = (Student student);
//虚函数
void jieshao();
~Student();
private:
int numb;
};
#endif
#include "Student.h"
Student::Student() :People() {
};
Student::Student(string name, int age, int numb) :People(name, age) {
this->numb = numb;
};
Student::Student(const Student& student) :People(student) {
this->numb = student.numb;
};
Student& Student::operator = (Student student) {
this->numb = student.numb;
People::operator=(student);
return *this;
};
Student::~Student(){
};
void Student::jieshao() {
People::jieshao();
cout << "numb" << this->numb << endl;
};
关于类继承后的生命周期:
然后就是多态了,关于他我不想过多的说了,都是老的东西了,一搜一大堆!