类的继承

先不多说,来看看一个继承体系的完整书写方法:

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

关于类继承后的生命周期:

然后就是多态了,关于他我不想过多的说了,都是老的东西了,一搜一大堆!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值