继承学习第一天 共有派生

如何共有派生一个类

class MAN :public HUMAN
基类 HUMAN

class HUMAN
{
private:
	int Age;
	int Height;
	string Name;

public:
	HUMAN(int age, int height, string & name);//构造函数
	HUMAN(const HUMAN & Temp);//复制构造函数
	~HUMAN(void);//析构函数
	void SetAge(const int age);
	void SetHeight(const int height);
	void SetName(const string &Temp);

	int GetAge(void) const;
	int GetHeight(void) const;
	string & GetName(void);

};

派生类MAN

class MAN : public HUMAN
{
private:
	bool Tall;
	bool Strong;
	bool Wealth;

public:
	
	MAN(bool tall, bool strong, bool wealth,
	int age, int height, string & name);//构造函数1
	MAN(bool tall, bool strong, bool wealth, const HUMAN &Temp);
	//构造函数2
	~MAN(void);//析构函数

	void SetTall(bool tall);
	void SetStrong(bool strong);
	void SetWealth(bool wealth);

	bool GetTall(void) const;
	bool GetStrong(void) const;
	bool GetWealth(void) const;
};

class MAN :public HUMAN

派生类MAN 继承了基类HUMAN 的 成员 和 方法。
公有派生: 基类的公有 成员 将成为 派生类的公有成员,基类的私有成员也成为派生类的一部分,但是基类的私有成员,还是只能通过基类的公有和保护方法访问

需要在派生类中添加什么

①添加派生类自己的构造函数
②添加 派生类自己需要的 成员和方法

派生类 不能直接访问 基类的私有成员 必须通过基类的公有方法去访问

派生类的构造函数问题

创建派生类对象的时候,程序首先会创建 基类对象,这就代表 程序进入派生类的构造函数之前,基类对象就已经被创建
基类的复制构造函数和 构造函数

HUMAN::HUMAN(int age, int height, string & name)
{
	cout<<"基类三个参数的构造函数"<<endl;
	Age = age; Height = height; Name = name;
}
HUMAN::HUMAN(const HUMAN & Temp)
{
	cout<<"基类复制构造函数"<<endl;
	Age = Temp.Age; Height = Temp.Height; Name = Temp.Name;
}

派生类的构造函数

MAN::MAN(bool tall, bool strong, bool wealth,
int age, int height, string & name):HUMAN(age, height, name)
{
	cout<<"派生类MAN六个参数的构造函数"<<endl;
	Tall = tall; Strong = strong; Wealth = wealth;
}
MAN::MAN(bool tall, bool strong, bool wealth, 
const HUMAN &Temp):HUMAN(Temp)
{
	cout<<"派生类MAN四个参数的构造函数"<<endl;
	Tall = tall; Strong = strong; Wealth = wealth;
}

基类指针可以在不强制转化的情况下,指向派生类对象,引用也可以,但是基类指针只能调用基类方法

派生类构造函数要点

①首先创建基类对象
②派生类的构造函数 通过成员初始化列表 将信息告诉 基类的构造函数
③ 派生类的构造函数 初始化自己的 成员

释放派生类对象

先调用派生类的析构函数,在调用基类的析构函数

全部代码

implement.h

#ifndef __IMPLEMENT_H__
#define __IMPLEMENT_H__

#include <string>

class HUMAN
{
private:
	int Age;
	int Height;
	string Name;

public:
	HUMAN(int age, int height, string & name);
	HUMAN(const HUMAN & Temp);
	~HUMAN(void);
	void SetAge(const int age);
	void SetHeight(const int height);
	void SetName(const string &Temp);

	int GetAge(void) const;
	int GetHeight(void) const;
	string & GetName(void);

};

class MAN : public HUMAN
{
private:
	bool Tall;
	bool Strong;
	bool Wealth;

public:
	
	MAN(bool tall, bool strong, bool wealth,
	int age, int height, string & name);
	MAN(bool tall, bool strong, bool wealth, const HUMAN &Temp);
	~MAN(void);

	void SetTall(bool tall);
	void SetStrong(bool strong);
	void SetWealth(bool wealth);

	bool GetTall(void) const;
	bool GetStrong(void) const;
	bool GetWealth(void) const;
};

#endif

implement.cpp

#include "implement.h"


HUMAN::HUMAN(int age, int height, string & name)
{
	cout<<"基类三个参数的构造函数"<<endl;
	Age = age; Height = height; Name = name;
}
HUMAN::HUMAN(const HUMAN & Temp)
{
	cout<<"基类复制构造函数"<<endl;
	Age = Temp.Age; Height = Temp.Height; Name = Temp.Name;
}
HUMAN::~HUMAN(void)
{
	cout<<"基类析构函数"<<endl;
}


void HUMAN::SetAge(const int age)
{
	Age = age;
}
void HUMAN::SetHeight(const int height)
{
	Height = height;
}
void HUMAN::SetName(const string &Temp)
{
	Name = Temp;
}

int HUMAN::GetAge(void) const
{
	return Age;
}
int HUMAN::GetHeight(void) const
{
	return Height;
}
string & HUMAN::GetName(void) 
{
	return Name;
}






MAN::MAN(bool tall, bool strong, bool wealth,
int age, int height, string & name):HUMAN(age, height, name)
{
	cout<<"派生类MAN六个参数的构造函数"<<endl;
	Tall = tall; Strong = strong; Wealth = wealth;
}
MAN::MAN(bool tall, bool strong, bool wealth, 
const HUMAN &Temp):HUMAN(Temp)
{
	cout<<"派生类MAN四个参数的构造函数"<<endl;
	Tall = tall; Strong = strong; Wealth = wealth;
}

MAN::~MAN(void)
{
	cout<<"派生类MAN析构函数"<<endl;
}
void MAN::SetTall(bool tall)
{
	Tall = tall;
}
void MAN::SetStrong(bool strong)
{
	Strong = strong;
}
void MAN::SetWealth(bool wealth)
{
	Wealth = wealth;
}

bool MAN::GetTall(void) const
{
	return Tall;
}
bool MAN::GetStrong(void) const
{
	return Strong;
}
bool MAN::GetWealth(void) const
{
	return Wealth;
}

main.cpp
ShowHUMAN这个函数的参数是基类

#include "implement.h"
#include <string>

void ShowHUMAN( HUMAN &Temp) 
{
	
	cout<<Temp.GetHeight()<<" "<<Temp.GetAge()<<" "
	<<Temp.GetName()<<endl;
}

int main(int argc, char* argv[])
{
	string Input;
	cin>>Input;

	HUMAN A(18, 180 , Input);//调用HUMAN的构造函数
	ShowHUMAN(A);

	cin>>Input;
	HUMAN B(22,176, Input);
	ShowHUMAN(B);

	HUMAN C(A);//HUMAN的复制构造函数
	ShowHUMAN(C);

	cin>>Input;
	MAN D(true, true, true, 18, 180, Input);
	//MAN的构造函数1
	ShowHUMAN(D);


	cin>>Input;
	MAN E(true, true, true, A);
	//MAN的构造函数2
	ShowHUMAN(E);
	
	cout<<E.GetName()<<" "<<E.GetTall()<<endl;
	return 0;
}

运行结果
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值