继承——读写器

公有继承,私有继承,读写器

分别有三个类,通过读写器修改每个类的信息。
代码比较繁琐,仅供学习使用,基础知识
依然是三个文件

头文件:

这里用的编译器是VS2017

#include<string>
using namespace std;
class stud
{
	string name;
	string no;
	float chi;
	float math;
	float eng;
	float aver;
public:
	stud();
	stud(string n1, string n2, float c1, float m1, float e1);
	~stud();
	void set_name(string t);
	string get_name();
	void set_no(string t);
	string get_no();
	void set_chi(float t);
	float get_chi();
	void set_math(float t);
	float get_math();
	void set_eng(float t);
	float get_eng();
	float get_aver();

};
class pupil :public  stud {
public:
	pupil();
	pupil(string n1, string n2, float c1, float m1, float e1, string g1);
	~pupil();
	void set_grade(string t);
	string get_grade();

private:

	string grade;
};
class middle_school_student :public stud {
public:
	middle_school_student();
	middle_school_student(string n1, string n2, float c1, float m1, float e1, string as1);
	~middle_school_student();
	void set_as(string t);
	string get_as();

private:
	string Arts_or_Sciences;

};
class college_student :private stud {
public:
	college_student();
	college_student(string n1, string n2, float c1, float m1, float e1, string ma1);
	~college_student();
	void set_college_name(string t);   //私有继承要通过自己的函数去访问基类的成员,间接访问
	string get_college_name();
	void set_college_number(string t);
	string  get_college_number();
	void set_college_chi(float t);
	float get_college_chi();
	void set_college_math(float t);
	float get_college_math();
	void set_college_eng(float t);
	float get_college_eng();
	float get_college_aver();
	string get_major();
	void set_major(string t);

private:
	string major;

};

.cpp文件

代码很长,但是我分的很清楚,对应的继承下面是对应的函数

#include "pch.h"
#include <iostream>
using namespace std;
stud::stud()
{
	name ="  ";
	no = "  ";
	chi = 0;
	math = 0;
	eng = 0;
	aver=0;
}
stud::~stud()
{
	cout <<name<<"is destructed!"<<endl;
}

stud::stud(string n1, string n2, float c1, float m1, float e1)
{
	name = n1;
	no = n2;
	chi = c1;
	math = m1;
	eng = e1;
	aver=(chi + math + eng) / 3;
	
}
pupil::pupil()
{
	grade = " ";
}
pupil::pupil(string n1, string n2, float c1, float m1, float e1, string g1) :stud(n1, n2, c1, m1, e1)
{
	grade = g1;

}
pupil::~pupil()
{
	cout<<"is destructed!" << endl;
}
middle_school_student::middle_school_student(string n1, string n2, float c1, float m1, float e1, string as1) : stud(n1, n2, c1, m1, e1)
{
	Arts_or_Sciences = as1;
}
middle_school_student::middle_school_student()
{
	Arts_or_Sciences = "  ";
}
middle_school_student::~middle_school_student()
{
	cout << "is destructed!" << endl;
}
void stud::set_name(string t)
{
	name = t;
}
string  stud::get_name()
{
	return name;
}
void stud::set_no(string t)
{
	no = t;
}
string stud::get_no()
{
	return no;
}
void stud::set_chi(float t)
{
	chi = t;
}
float stud::get_chi()
{
	return chi;
}
void stud::set_math(float t)
{
	math = t;
}
float stud::get_math()
{
	return math;
}
void stud::set_eng(float t)
{
	eng = t;
}
float stud::get_eng()
{
	return eng;
}
float stud::get_aver()
{
	return (chi + math + eng) / 3;
}
void pupil::set_grade(string t)
{
	grade = t;
}
string pupil::get_grade()
{

	return grade;
}
void middle_school_student::set_as(string t)
{
	Arts_or_Sciences = t;
}
string middle_school_student::get_as()
{
	return Arts_or_Sciences;
}
//从这里开始是私有继承
college_student::college_student(string n1, string n2, float c1, float m1, float e1, string ma1) :stud(n1, n2, c1, m1, e1)
{
	major = ma1;

}
college_student::college_student()
{
	major = "  ";
}
college_student::~college_student()
{
	cout << "is destructed!" << endl;
}
void college_student::set_college_name(string t)
{
	set_name(t);
}
string  college_student::get_college_name()
{
	return get_name();
}
void college_student::set_college_number(string t)
{
	set_no(t);
}
string college_student::get_college_number()
{
	return get_no();
}
void college_student::set_college_chi(float t)
{
	set_chi(t);
}
float college_student::get_college_chi()
{
	return get_chi();
}
void college_student::set_college_math(float t)
{
	set_math(t);
}
float college_student::get_college_math()
{
	return get_math();
}
void college_student::set_college_eng(float t)
{
	set_eng(t);
}
float college_student::get_college_eng()
{
	return get_eng();
}
float college_student::get_college_aver()
{
	return get_aver();
}
void college_student::set_major(string t)
{
	major = t;
}
string college_student::get_major()
{
	return major;
}

最后是main函数

#include "pch.h"
#include <iostream>
using namespace std;


int main()
{

	pupil C;
	string t_name;
	string t_number;
	string t_grade;
	float t_chi;
	float t_math;
	float t_eng;
	cout << "请输入C的姓名:" ;
	cin >> t_name;
	cout << "请输入C的学号:"  ;
	cin >> t_number;
	cout << "请输入C的语文:" ;
	cin >> t_chi;
	cout << "请输入C的数学:" ;
	cin >> t_math;
	cout << "请输入C的英语:" ;
	cin >> t_eng;
	cout << "请输入C的年级:"<< endl;
	cin >> t_grade;
	C.set_name(t_name);
	C.set_no(t_number);
	C.set_grade(t_grade);
	C.set_chi(t_chi);
	C.set_math(t_math);
	C.set_eng(t_eng);

	cout << "C的姓名:" << C.get_name() << endl;
	cout << "C的学号:" << C.get_no() << endl;
	cout << "C的语文:" << C.get_chi() << endl;
	cout << "C的数学:" << C.get_math() << endl;
	cout << "C的英语:" << C.get_eng() << endl;
	cout << "C的平均:" << C.get_aver() << endl;
	cout << "C的年级:" <<C.get_grade() << endl << endl;


	middle_school_student A;
	string x_name;
	string x_number;
	string t_as;
	float x_chi;
	float x_math;
	float x_eng;
	cout << "请输入A的姓名:";
	cin >> x_name;
	cout << "请输入A的学号:";
	cin >> x_number;
	cout << "请输入A的语文:";
	cin >> x_chi;
	cout << "请输入A的数学:";
	cin >> x_math;
	cout << "请输入A的英语:";
	cin >> x_eng;
	cout << "请输入A的文理科:" << endl;
	cin >> t_as;
	A.set_name(x_name);
	A.set_no(x_number);
	A.set_as(t_as);
	A.set_chi(x_chi);
	A.set_math(x_math);
	A.set_eng(x_eng);

	cout << "A的姓名:" << A.get_name() << endl;
	cout << "A的学号:" << A.get_no() << endl;
	cout << "A的语文:" << A.get_chi() << endl;
	cout << "A的数学:" << A.get_math() << endl;
	cout << "A的英语:" << A.get_eng() << endl;
	cout << "A的平均:" << A.get_aver() << endl;
	cout << "A的文理科:" << A.get_as() << endl << endl;

	college_student B;
	string e_name;
	string e_number;
	string t_major;
	float e_chi;
	float e_math;
	float e_eng;
	cout << "请输入B的姓名:";
	cin >> e_name;
	cout << "请输入B的学号:";
	cin >> e_number;
	cout << "请输入B的语文:";
	cin >> e_chi;
	cout << "请输入B的数学:";
	cin >> e_math;
	cout << "请输入B的英语:";
	cin >> e_eng;
	cout << "请输入B专业:" << endl;
	cin >> t_major;
	B.set_college_name(e_name);
	B.set_college_number(e_number);
	B.set_major(t_major);
	B.set_college_chi(e_chi);
	B.set_college_math(e_math);
	B.set_college_eng(e_eng);

	cout << "B的姓名:" << B.get_college_name() << endl;
	cout << "B的学号:" << B.get_college_number() << endl;
	cout << "B的语文:" << B.get_college_chi() << endl;
	cout << "B的数学:" << B.get_college_math() << endl;
	cout << "B的英语:" << B.get_college_eng() << endl;
	cout << "B的平均:" << B.get_college_aver() << endl;
	cout << "B的专业:" << B.get_major() << endl << endl;
	return 0;
}

类似于:

void stud::set_name(string t)
{
	name = t;
}
string  stud::get_name()
{
	return name;
}

是读写器,各位博客老爷一定都知道,容许我班门弄斧!
在这里发表一下个人观点,关于读写器的好处:
1.便于修改信息,因为模块化很清楚
2.做项目的时候应该用得着,不过没有这么简单
3.留给博客老爷们

最后展示一下运行的结果

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
第三张图片可以看到构造函数和析构函数的顺序!

好啦 ,以上就是这些啦!祝大家2019端午节快乐,不过我是真的吃不惯肉粽子和咸鸭蛋!!!!好咸!!!!是真的咸!!!!
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值