C++实验报告三:类的继承

实验三:类的继承

【实验目的】

  1. 掌握类继承与派生关系以及实现方法,理解类的层次结构;

  2. 掌握派生类构造函数初始化基类成员和对象成员的方法;

  3. 掌握类型兼容规则,掌握派生类的复制构造函数的定义;

  4. 掌握多重继承和虚基类。

【实验内容】

  1. 设计一个用于人事管理的“People(人员类)”基类。考虑到通用性,仅抽象出各类人员都具有的属性:编号、姓名、性别、出生日期、身份证号等;

  2. 从People(人员类)派生出Student(学生类),并添加属性:班号class NO;

  3. 从People类派生出Teacher(教师类),并添加属性:职务principalship、部门department;

  4. 从Student类中派生出Graduate(研究生)类,并添加属性:专业subject、导师adviser(该属性是Teacher类对象);

  5. 从Graduate类和Teacher类派生出助教生类TA,无新的属性。设计该类时注意虚基类的使用,注意重载相应的成员函数。

  6. 编写main函数测试这些类。在main函数中设计测试用例时,注意考虑如何体现成员函数的覆盖。

【程序清单】

//People.h
#pragma once
#include <string>
using namespace std;

class Date
{
public:
	Date(int y = 0, int m = 0, int n = 0);
	Date(const Date& copy);
	~Date() {}
	void Show();

private:
	int m_year, m_month, m_day;
};

class People
{
public:
	People();
	People(int number, string id, string name, string sex, int y, int m, int d);
	People(const People& copy);
	~People() {}
	void Show();

protected:
	int m_number;
	string m_sex, m_name, m_id;
	Date m_birth;
};
//People.cpp
#include "People.h"
#include <iostream>
using namespace std;

Date::Date(int y, int m, int d)
{
	m_year = y;
	m_month = m;
	m_day = d;
}

Date::Date(const Date& copy)
{
	m_year = copy.m_year;
	m_month = copy.m_month;
	m_day = copy.m_day;
}

void Date::Show()
{
	cout << "出生年月日为:";
	cout << m_year << '.' << m_month << '.' << m_day << endl;
}

People::People()
{
	m_number = 0;
	m_id = "";
	m_name = "";
	m_sex = "";
	m_birth = { 0, 0, 0 };
}

People::People(int number, string id, string name, string sex, int y, int m, int d) 
	: m_birth(y, m, d)
{
	m_number = number;
	m_id = id;
	m_name = name;
	m_sex = sex;
}

People::People(const People& copy) : m_birth(copy.m_birth)
{
	m_number = copy.m_number;
	m_id = copy.m_id;
	m_name = copy.m_name;
	m_sex = copy.m_sex;
}

void People::Show()
{
	cout << "姓名:" << m_name << endl;
	cout << "性别:" << m_sex << endl;
	cout << "编号:" << m_number << endl;
	cout << "身份证号:" << m_id << endl;
	m_birth.Show();
}
//Student.h
#pragma once
#include "People.h"

class Student
	: virtual public People
{
public:
	Student() {}
	Student(People p, int classNO);
	Student(const Student& copy);
	~Student() {}
	void Show();

protected:
	int m_classNO;
};
//Student.cpp
#include "Student.h"
#include <iostream>
using namespace std;

Student::Student(People p, int classNO)
	: People(p)
{
	m_classNO = classNO;
}

Student::Student(const Student& copy) 
	: People(copy)
{
	m_classNO = copy.m_classNO;
}

void Student::Show()
{
	People::Show();
	cout << "班号:" << m_classNO << endl;
}
//Teacher.h
#pragma once
#include "People.h"

class Teacher 
	: virtual public People
{
public:
	Teacher() {}
	Teacher(People p, string principalship, string department);
	Teacher(const Teacher& copy);
	~Teacher() {}
	void Show();
	void Show(string i);

protected:
	string m_principalship, m_department;
};
//Teacher.cpp
#include "Teacher.h"
#include <iostream>
using namespace std;

Teacher::Teacher(People p, string principalship, string department)
	: People(p)
{
	m_principalship = principalship;
	m_department = department;
}

Teacher::Teacher(const Teacher& copy)
	: People(copy)
{
	m_principalship = copy.m_principalship;
	m_department = copy.m_department;
}

void Teacher::Show()
{
	People::Show();
	cout << "职务:" << m_principalship << endl;
	cout << "部门:" << m_department << endl;
}

void Teacher::Show(string i)
{
	cout << "助理职务:" << m_principalship << endl;
	cout << "助理部门:" << m_department << endl;
}
//Graduate.h
#pragma once
#include "Student.h"
#include "Teacher.h"

class Graduate 
	: public Student
{
public:
	Graduate() {}
	Graduate(Student stu, string subject, Teacher adviser);
	Graduate(const Graduate& copy);
	~Graduate() {}
	void Show();

protected:
	string m_subject;
	Teacher m_adviser;
};
//Graduate.cpp
#include "Graduate.h"
#include <iostream>
using namespace std;

Graduate::Graduate(Student stu, string subject, Teacher adviser)
	: People(stu), Student(stu), m_adviser(adviser)
{
	m_subject = subject;
}

Graduate::Graduate(const Graduate& copy)
	: People(copy), Student(copy), m_adviser(copy.m_adviser)
{
	m_subject = copy.m_subject;
}

void Graduate::Show()
{
	Student::Show();
	cout << "专业:" << m_subject << endl;
	cout << endl << "导师信息如下:" << endl;
	m_adviser.Show();
}
//TA.h
#pragma once
#include "Teacher.h"
#include "Graduate.h"

class TA
	: public Teacher, public Graduate
{
public:
	TA() {}
	TA(Teacher t, Graduate g);
	TA(const TA& copy);
	~TA() {}
	void Show();
};
//TA.cpp
#include "TA.h"
#include <iostream>
using namespace std;

TA::TA(Teacher t, Graduate g)
	: People(t), Teacher(t), Graduate(g)
{

}

TA::TA(const TA& copy)
	: People(copy), Teacher(copy), Graduate(copy)
{

}

void TA::Show()
{
	Teacher::Show("new");
	Graduate::Show();
}
//main.cpp
#include "Graduate.h"
#include "People.h"
#include "Student.h"
#include "TA.h"
#include "Teacher.h"
#include <iostream>
using namespace std;

int main()
{
	People p1(1, "111", "A", "男", 2000, 1, 1);
	People p2(2, "222", "B", "女", 1970, 2, 2);
	People p3(3, "333", "C", "男", 1997, 3, 3);

	Student s1(p1, 1);
	Student s2(p3, 2);
	Teacher t1(p2, "教授", "计算机科学与技术");
	Teacher t2(p3, "讲师", "网络工程");
	Graduate g1(s2, "软件工程", t1);
	TA T1(t2, g1);


	cout << "基类:" << endl;
	s1.People::Show();
	cout << "===========================" << endl;

	cout << "学生类:" << endl;
	s1.Show();
	cout << "===========================" << endl;

	cout << "教师类:" << endl;
	t1.Show();
	cout << "===========================" << endl;

	cout << "研究生类:" << endl;
	g1.Show();
	cout << "===========================" << endl;

	cout << "助教类:" << endl;
	T1.Show();
	cout << "===========================" << endl;

	system("pause");
	return 0;
}

【运行结果】

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Luther_w

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

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

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

打赏作者

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

抵扣说明:

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

余额充值