c++实验三:继承与派生

一、实验目的
1、 掌握类继承与派生关系以及实现方法,理解类的层次结构;
2、 掌握派生类构造函数初始化基类成员和对象成员的方法;
3、 掌握类型兼容规则,掌握派生类的复制构造函数的定义。
4、 掌握多重继承和虚基类。
二.一个用于人事管理的类族。
⑴设计一个用于人事管理的“People(人员类)”基类。考虑到通用性,仅抽象出各类人员都具有的属性:编号、姓名·、性别、出生日期、身份证号等;
⑵从People(人员类)派生出Student(学生类),并添加属性:班号classNO;
⑶从People类派生出Teacher(教师类),并添加属性:职务principalship、部门department;
⑷从Student类中派生出Graduate(研究生)类,并添加属性:专业subject、导师adviser(该属性是Teacher类对象);
⑸从Graduate类和Teacher类派生出助教生类TA,无新的属性。设计该类时注意虚基类的使用,注意重载相应的成员函数。
⑹编写main函数测试这些类。在main函数中设计测试用例时,注意考虑如何体现成员函数的覆盖。
编程要求:
⑴本程序必须建立工程。
⑵将上述5个类的定义部分分别作为5个头文件(.h文件),对5个类的实现部分分别作为5个源文件(.cpp文件),在main函数中用#include命令把它们包含进来,形成一个完整的程序。
这是我第一次编工程呢,过程可是艰辛,废话不多说,我用的软件是Devc++,下面把在其中如何做工程的方法罗列出来
首先:
在这里插入图片描述
再就是选第二个
然后就会出现这个
出现了main.cpp
再直接新建单元并且改名就行,c++的项目就完成了
这是总的结果
在这里插入图片描述

//main.cpp
#include<iostream>
#include"Date.h"
#include"Person.h"
#include"Student.h"
#include"Teacher.h"
#include"Graduate.h"
#include"TA.h"
#include<string>
using namespace std;
int main()
{
	Date d1(2001,4,2),d2(1985,4,9); 
	Person p1(201907152052,"小明","man","4209222012342626",d1);
	Person p2(2019071520523,"李华","man","420922220000427362",d2);
	Student s(p1,"1902");
	Teacher t(p2,"英语老师","英语部");
	Graduate G(s,"计算机科学与技术",t);
	TA T(G,t);
	cout<<"………………………………………………………………………………………………";
	cout<<"基类信息:"<<endl;
	p1.show();
	cout<<"………………………………………………………………………………………………" ;
	cout<<"学生类:"<<endl;
	s.show();
	cout<<"………………………………………………………………………………………………"<<endl;
	s.Person::show();//表示同名的覆盖!
	cout<<"………………………………………………………………………………………………" ;
	cout<<"老师类:"<<endl;
	t.show() ;
	cout<<"………………………………………………………………………………………………" ;
	cout<<"研究生类:"<<endl;
	G.show() ;
	cout<<"………………………………………………………………………………………………" ;
	cout<<"TA类:"<<endl;
	T.show();
	cout<<"………………………………………………………………………………………………" ;
	return 0;
}

//Date.h
#ifndef _DATE_H_
#define _DATE_H_
class Date//Date类 
{
	public:
		Date(int y=2001,int m=4,int d=2);//有参构造与默认构造和一 
		Date( const Date &D);//复制构造 
		void getin();//设置时间 
		void show();//展示时间 
		~Date(){} //析构函数 
	private:
		int year,month,day;
};
#endif

//Date.cpp 
#include"Date.h"
#include<iostream>
using namespace std;
Date::Date(int y,int m,int d):year(y),month(m),day(d)//初始化区域 
{
}
inline void Date:: getin()
{
cin>>year>>month>>day;
}
void Date::show()
{
	cout<<"出生日期:"<<year<<"-"<<month<<"-"<<day<<endl; 
}
Date::Date(const Date &D):year(D.year),month(D.month),day(D.day)//只要是构造函数都可以,但是普通得成员函数不可以 
{
}

//Person.h
#ifndef _PERSON_H_
#define _PERSON_H_
#include"Date.h"//编号、姓名、性别、出生日期、身份证号等;
#include<string>
using namespace std;
class Person
{
	public:
		Person(long long number, const string &name, const  string& sex, const string &idcard,const Date& date);//默认构造最好用cosnt 
		Person(const Person&P);
		void show();
		~Person(){}//析构函数的大括号千万不能忘 
	protected:
		long long  p_number;//编号 
		string p_name;//姓名 
		string p_sex;//性别 
		string p_idcard;//身份证号 
		Date p_date;	//内嵌对象	 
		
};
#endif
//Person.cpp
#include "Person.h"//要包含要用到的.h文件 
#include<iostream>
#include"Date.h"
#include<string>
using namespace std;
Person::Person(long long number, const string &name, const string &sex,const string& idcard,const Date &date):p_number(number),p_name(name),p_sex(sex),p_idcard(idcard),p_date(date)
{
}
Person::Person(const Person&P):	p_number(P.p_number),p_name(P.p_name),p_sex(P.p_sex),p_idcard(P.p_idcard),p_date(P.p_date) 
{
}
void Person::show()
{
	cout<<"编号:"<<p_number<<endl;
	cout<<"姓名:"<<p_name<<endl;
	cout<<"性别:"<<p_sex<<endl;
	cout<<"身份证号:"<<p_idcard<<endl;
	p_date.show(); 
}

//Student.h
#ifndef _STUDENT_H_
#define _STUDENT_H_
#include"Person.h"
class Student: virtual public Person//虚基类继承 
{
	public:
		Student(const Person &P, const string &classNO);//用引用来做参数 构造函数
		Student(const Student&S);
		void show(); 
		~Student(){}
	protected:
	string s_classNO;
};
#endif
//Student.cpp
#include<cstring>
#include"Person.h"
#include"student.h"
#include<iostream>
using namespace std;
Student::Student(const Person &P,  const string &classNO):Person(P),s_classNO(classNO)
{
}
Student::Student(const Student &S1):Person(S1),s_classNO(S1.s_classNO)
{
}
void Student::show()
{
	Person::show();
	cout<<"班号:"<<s_classNO<<endl;
}
//Teacher.h
#include<string>
#ifndef _TEACHER_H_
#define _TEACHER_H_
#include"Person.h"//职务principalship、部门department;
class Teacher: virtual public Person
{
	public:
		Teacher( const Person&p,const string &principalship, const string &department);
		Teacher(const Teacher&T);
		void show();
		~Teacher(){}
	protected:
		string T_principalship;
		string T_department;
};
#endif
//Teacher.cpp
#include<string>
#include<iostream>
#include"Person.h"
#include"Teacher.h"
using namespace std;
Teacher::Teacher(const Person&p, const  string &principalship,  const string &department):Person(p),T_principalship(principalship),T_department(department)
{
}
Teacher::Teacher(const Teacher&T):Person(T),T_principalship(T.T_principalship),T_department(T.T_department)
{
}
void Teacher::show()
{
	Person::show();
	cout<<"职务:"<<T_principalship<<endl;
	cout<<"部门:"<<T_department<<endl;
}
//Graduate.h
#ifndef _GRADUATE_H_
#define _GRADUATE_H_
#include"Student.h"
#include"Teacher.h"
#include<string>
using namespace std;
// //专业subject、导师adviser
class Graduate:public Student
{
	public:
		Graduate( const Student &S1, const string &subject,const Teacher &T);
		Graduate(const Graduate&G);
		void show();
		~Graduate(){}
	protected:
	string G_subject;
	Teacher G_adviser;	
};
#endif
//Graduate.cpp
#include<string>
#include<iostream>
#include"Student.h"
#include"Graduate.h"
using namespace std;
Graduate::Graduate(const Student &S1,const string &subject,  const Teacher &T):Person(S1),Student(S1),G_adviser(T),G_subject(subject)
{
}
Graduate::Graduate(const Graduate&G):Person(G),Student(G),G_adviser(G.G_adviser),G_subject(G.G_subject)
{
}
void Graduate::show()
{
	Student::show();
	cout<<"项目:"<<G_subject<<endl;
	cout<<"导师:"<<endl;
	G_adviser.show();
}
//TA.h
#ifndef _TA_H_
#define _TA_H_
#include"Graduate.h"
#include"Teacher.h"
class TA:  public Graduate, public Teacher
{
	public:
	TA(const Graduate&g, const Teacher&t);
	TA(const TA&T);
	void show();
	~TA(){}	
};
#endif
//TA.cpp
#include<iostream>
#include"Graduate.h"
#include"Teacher.h"
#include"TA.h"
using namespace std;
TA::TA(const Graduate&g, const Teacher&t):Person(g),Graduate(g),Teacher(t){}
TA::TA(const TA&T):Person(T),Graduate(T),Teacher(T){} 
void TA::show()
{
	Graduate::show();
} 
  • 40
    点赞
  • 92
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值