计算机程序设计c++ 11-3: 三种派生类继承方式

三种派生类继承方式是

  • 公有继承public
  • 私有继承private
  • 保护继承protected

公有继承方式(public)

采用公有继承方式创建的派生类,对基类各种成员访问权限如下:

  • 基类公有成员相当于派生类的公有成员,即派生类可以象访问自身公有成员一样访问从基类继承的公有成员
  • 基类保护成员相当于派生类的保护成员,即派生类可以像访问自身的保护成员一样,访问基类的保护成员
  • 派生类内部成员无法直接访问基类的私有成员

例子

// 基类
class Person //人员类定义
{
    
    
	protected:
		char Name[10]; //姓名
		int Age; //年龄
		char Sex; //性别

	public:
		void Register(char *name, int age, char sex) //设置数据成员
		{
    
    
			strcpy(Name, name);
			Age = age;
			Sex = (sex == 'm'? 'm':'f');
		}

		void ShowMe() //输出数据成员
		{
    
    cout << Name << "\t" << Sex << "\t" << Age << "\t"; }
};


// 派生类
class Employee: public Person //雇员类定义
{
    
     
	char Dept[20]; //工作部门
	float Salary; //月薪
	
	public:
		Employee()
		{
    
    EmployeeRegister("XXX", 0, 'm', "XXX", 0); }
		
		void EmployeeRegister(char *name, int age, char sex, char *dept, float salary);
		void ShowEmp(); //显示雇员信息
};

派生类外定义成员函数

void Employee::EmployeeRegister(char *name, int age, char sex, char *dept, float
salary)
{
    
    
	Register(name, age, sex);  //使用基类的成员函数操作基类数据成员
	strcpy(Dept, dept);
	Salary = salary;
}

void Employee::ShowEmp ()
{
    
    
	cout << Name << "\t" << Sex << "\t" << Age << "\t";
	cout << Dept << "\t" << Salary;
}

主函数测试

int main()//主函数
{
    
    
	Employee emp;
	emp.EmployeeRegister("张弓长", 40, 'f', "图书馆", 2000);
	emp.ShowEmp();
	cout<<endl;

	emp.ShowMe();
	cout<<endl;
	return 0;
}

在这里插入图片描述

私有继承方式(private)

派生类对基类各种成员访问权限如下:

  • 基类公有成员和保护成员都相当于派生类的私有成员,派生类只能通过自身的函数成员访问他们
  • 对于基类的私有成员,无论派生类内部成员或派生类使用者都无法直接访问

私有继承方式举例

class Employee: private Person //雇员类定义
{
    
     
	char Dept[20]; //工作部门
	float Salary; //月薪
	
	public:
		Employee()
		{
    
    EmployeeRegister("XXX", 0, 'm', "XXX", 0); }
		
		void EmployeeRegister(char *name, int age, char sex, char *dept, float salary);
		void ShowEmp(); //显示雇员信息
};

主函数测试

int main()//主函数
{
    
    
	Employee emp;
	emp.EmployeeRegister("张弓长", 40, 'f', "图书馆",2000);
	emp.ShowEmp();
	emp.ShowMe();  // 错误,因为是私有继承,不能直接访问
	return 0;
}
  • 前三句正常通过
  • emp.ShowMe()违反继承规则,Showme()是派生类的私有成员,只能成员函数访问,对象不能访问

可以在派生类中添加访问成员函数解决上述问题:

#include<iostream.h>
#include<string.h>

class Employee: private Person //雇员类定义
{
    
    
	char Dept[20]; //工作部门
	float Salary; //月薪

	public:
		Employee()
		{
    
    EmployeeRegister("XXX",0,'m',"XXX",0); }

		void EmployeeRegister(char *name, int age, char sex, char *dept, float salary);
		void ShowEmp(); //显示雇员信息
		char* GetEmployeeName() {
    
     return Name; } //取姓名
		char GetEmployeeSex() {
    
     return Sex; } //取性别
		int GetEmployeeAge() {
    
     return Age; } //取年龄
};

类外定义成员函数

Employee::void EmployeeRegister(char *name, int age, char sex, char *dept, float salary)
{
    
    
	Register(name, age, sex);
	strcpy(Dept, dept);
	Salary = salary;
}

void Employee::ShowEmp()
{
    
    
	cout << Name << "\t"; //调用私有成员Name
	cout << Age << "\t"; //调用私有成员Age
	cout << Sex << "\t"; //调用私有成员Sex
	cout << Dept << "\t" << Salary << endl;
}

主函数测试

int main()
{
    
    
	Employee emp;
	emp.EmployeeRegister("张弓长", 40, 'm', "图书馆", 2000);
	emp.ShowEmp();
	cout << "调用GetEmployeeName()返回值为:" << emp.GetEmployeeName() << endl;
	cout << "调用GetEmployeeSex()返回值为: " << emp.GetEmployeeSex() << endl;
	cout << "调用GetEmployeeAge()返回值为: " << emp.GetEmployeeAge() << endl;
	return 0;
}

在这里插入图片描述

保护继承方式(protected)

采用保护继承方式创建的派生类对基类各种成员访问权限如下 :

  • 基类的公有成员和保护成员都相当于派生类的保护成员,派生类可以通过自身的成员函数或其子类的成员函数访问他们
  • 对于基类的私有成员,无论派生类内部成员或派生类使用者都无法直接访问

保护继承方式例子

class Student : protected Person
{
    
    
	int Number;
	char ClassName[10];
	
	public:
		void Register(char *classname, int number, char *name, int age, char sex)
		{
    
    
			strcpy(ClassName, classname);
			Number = number;
			strcpy(Name, name); //正确,引用基类的保护成员
			Age = age; //正确,引用基类的保护成员
			Sex = (sex == 'm'?'m':'f'); //正确,引用基类的保护成员
		}
		
		void ShowStu()
		{
    
    
			cout << Number << '\t' << ClassName << '\t';
			ShowMe();
		}
};

主函数测试

int main()
{
    
    
	Student stu;
	stu.Register("计算机51",85071011,"张弓长",18,'m');
	stu.ShowStu();
	// stu.ShowMe(); //错误,对象不能直接访问保护成员
	return 0;
}

总结

在这里插入图片描述

Guess you like

Origin blog.csdn.net/uncle_ll/article/details/121881782