继承与多态 用法

博客主要介绍了C++开发语言中继承与多态的用法,涉及信息技术领域的程序设计知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

继承与多态 用法

#include <iostream>
#include <string>
using namespace std;

class Person
{
public:
	//默认成员函数
	Person(string name, string sex, int age)
		:_name(name)
		,_sex(sex)
		,_age(age)
	{}
	//虚函数
	virtual void SelfIntroduce()
	{
		cout << "个人信息如下:";
		cout << _name << " " << _sex << " " << _age << endl;
	}
	virtual void GetAdmissionFee()
	{
		cout << "票价为100元" << endl;
	}
	virtual void IsHappy() //加上 final 试试?直接封口
	{
		cout << "我很幸福!" << endl;
	}
	//纯虚函数

protected:
	string _name;
	string _sex;
	int _age;
};

class Student : public Person
{
public:
	//默认成员函数
	Student(string name, string sex, int age, int stuid, string goal)
		:Person(name, sex, age)
		,_stuid(stuid)
		,_goal(goal)
	{}
	~Student()
	{
		_stuid = 0;
	}
	Student(const Student& other)
		:Person(other)
		,_stuid(other._stuid)
		,_goal(other._goal)
	{}
	Student& operator=(const Student& other)
	{
		if (&other != this)
		{
			Person::operator=(other);
			_stuid = other._stuid;
			_goal = other._goal;
		}
		return *this;
	}
	//虚函数重写
	virtual void SelfIntroduce() override
	{
		cout << "个人信息如下:";
		cout << _name << " " << _sex << " " << _age << " " << _stuid << " " << _goal << endl;
	}
	void GetAdmissionFee()
	{
		cout << "票价为50元" << endl;
	}
	virtual void IsHappy()
	{
		cout << "我不幸福!" << endl;
	}
protected:
	int _stuid;
	string _goal;
};

class Teather : public Person
{
public:
	//默认成员函数
	Teather(string name, string sex, int age, int jobid, bool ismarry)
		:Person(name, sex, age)
		, _jobid(jobid)
		, _ismarry(ismarry)
	{}
	~Teather()
	{
		_jobid = 0;
	}
	Teather(const Teather& other)
		:Person(other)
		, _jobid(other._jobid)
		, _ismarry(other._ismarry)
	{}
	Teather& operator=(const Teather& other)
	{
		if (&other != this)
		{
			Person::operator=(other);
			_jobid = other._jobid;
			_ismarry = other._ismarry;
		}
		return *this;
	}
	//虚函数重写
	virtual void SelfIntroduce() override
	{
		cout << "个人信息如下:";
		cout << _name << " " << _sex << " " << _age << " " << _jobid << " " << _ismarry << endl;
	}
	void GetAdmissionFee()
	{
		cout << "票价为110元" << endl;
	}
	virtual void IsHappy()
	{
		cout << "我不幸福!" << endl;
	}
protected:
	int _jobid;
	bool _ismarry;
};

void CheckAdmissionFee(Person* person)
{
	person->GetAdmissionFee();
}

void InformationDisplay(Person& person)
{
	person.SelfIntroduce();
}

void Asking(Person* person)
{
	person->IsHappy();
}

int main()
{
	Person person1("张三", "男", 20);
	person1.SelfIntroduce();
	person1.GetAdmissionFee();
	cout << endl;

	Student person2("张三的弟弟", "男", 15, 1254, "好好学习技能");
	person2.SelfIntroduce();
	person2.GetAdmissionFee();
	cout << endl;

	Teather person3("张三的妈妈", "女", 40, 907, true);
	person3.SelfIntroduce();
	person3.GetAdmissionFee();
	cout << endl;

	CheckAdmissionFee(&person1);
	CheckAdmissionFee(&person2);
	CheckAdmissionFee(&person3);
	cout << endl;

	InformationDisplay(person1);
	InformationDisplay(person2);
	InformationDisplay(person3);
	cout << endl;

	Asking(&person1);
	Asking(&person2);
	Asking(&person3);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

立志成为软件工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值