C++继承与派生(谭浩强11.1-11.7)

例11.3在派生类中引用保护成员

#include <iostream>
using namespace std;
#include<string>
class Student//声明基类
{
public://基类无公用成员
protected://基类保护成员
	int num;
	string name;
	char sex;
};
class Student1 :protected Student//用protected方式声明派生类Student1
{
public:
	void get_value1();
	void display1();
private:
	int age;
	string addr;
};
void Student1::get_value1()//定义派生类公用成员函数
{
	cin >> num >> name >> sex;//输入保护基类数据成员
	cin >> age >> addr;//输入派生类数据成员
}
void Student1::display1()//定义派生类公用成员函数
{
	cout << "num:" << num << endl;//引用基类的保护成员
	cout << "name:" <<name << endl;//引用基类的保护成员
	cout << "sex:" << sex << endl;//引用基类的保护成员
	cout << "age:" << age << endl;//引用派生类的私有成员
	cout << "sddress:" <<addr << endl;//引用派生类的私有成员
}
int main()
{
	Student1 stud1;//stud1是派生类Student1类对象
	stud1.get_value1();//派生类公用成员函数
	stud1.display1();
	return 0;
}

程序执行效果如图:
在这里插入图片描述
11.5定义简单派生类的构造函数;

#include <iostream>
using namespace std;
#include<string>
class Student//声明基类
{
public://基类公用成员
	Student(int n,string nam,char s)//基类构造函数
	{
		num = n;
		name = nam;
		sex = s;
	}
	~Student(){}//基类析构函数
protected://基类保护成员
	int num;
	string name;
	char sex;
};
class Student1 :public Student//用public方式声明派生类Student1
{
public:
	Student1(int n, string nam, char s, int a, string ad) :Student(n, name, s)

		//定义派生类构造函数
	{
		age = a;
		addr = ad;
	}
	void show()
	{
		cout << "num:" << num << endl;
		cout << "name:" << name << endl;
		cout << "sex:" << sex << endl;
		cout << "age:" << age << endl;
		cout << "sddress:" << addr << endl << endl;
	}
	~Student1(){}//派生类析构函数
private:
	int age;
	string addr;
};
int main()
{
	Student1 stud1(10010, "Wang-li", 'f',19,"115 Beijing Road,Shanghai");
	Student1 stud2(10011, "Zhang-fang", 'm', 21, "213 Shanghai Road,Beijing");
	stud1.show();
	stud2.show();
	return 0;
}

程序执行结果如图:
在这里插入图片描述
11.6包含子对象的派生类的构造函数

#include <iostream>
using namespace std;
#include<string>
class Student//声明基类
{
public://基类公用成员
	Student(int n,string nam)//基类构造函数
	{
		num = n;
		name = nam;
	}
	void display()
	{
		cout << "num:" << num << endl << "name:" << name << endl;
	}
protected://基类保护成员
	int num;
	string name;
};
class Student1 :public Student//用public方式声明派生类Student1
{
public:
	Student1(int n, string nam,int n1,string nam1, int a, string ad) 
		:Student(n, nam), monitor(n1,nam1)

		//定义派生类构造函数
	{
		age = a;
		addr = ad;
	}
	void show()
	{
		cout << "This student is:" << endl;
		display();
		cout << "age:" << age << endl;
		cout << "address:" << addr << endl << endl;
	}
	void show_monitor()
	{
		cout << endl<< "Class monitor is:" << endl;
		monitor.display();
	}

private:
	Student monitor;
	int age;
	string addr;
};
int main()
{
	Student1 stud1(10010, "Wang-li",10001 ,"Li-sun",19,"115 Beijing Road,Shanghai");
	stud1.show();
	stud1.show_monitor();
	return 0;
}

程序执行结果如图:
在这里插入图片描述
例11.7多级派生情况下的构造函数

#include <iostream>
using namespace std;
#include<string>
class Student//声明基类
{
public://基类公用成员
	Student(int n,string nam)//基类构造函数
	{
		num = n;
		name = nam;
	}
	void display()
	{
		cout << "num:" << num << endl << "name:" << name << endl;
	}
protected://基类保护成员
	int num;
	string name;
};
class Student1 :public Student//用public方式声明派生类Student1
{
public:
	Student1(int n, string nam, int a)
		:Student(n, nam)

		//定义直接派生类构造函数
	{
		age = a;
	}
	void show()
	{
		display();
		cout << "age:" << age << endl;
	}
private:
	int age;
};
class Student2 :public Student1//用public方式声明派生类Student2
{
public:
	Student2(int n,string nam,  int a,int s)
		:Student1(n, nam,a)

		//定义间接派生类构造函数
	{
		score=s;
	}
	void show_all()
	{
		show();
		cout << "score:" << score << endl;
	}
private:
	int score;
};
int main()
{
	Student2 stud1(10010, "Wang-li",17, 89);
	stud1.show_all();
	return 0;
}

程序执行效果如图:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值