C++继承和派生练习(一)--关于从people(人员)类派生出student(学生)类等
. 从people(人员)类派生出student(学生)类
添加属性:班号char classNO[];从people类派生出teacher(教师)类,
添加属性:职务char principalship[]、部门char department[]。
从student类中派生graduate(研究生)类,添加属性:专业char subject[]、
导师char teacher_adviser[];从graduate类和teacher类派生出TA(助教生)类,
注意虚基类的使用。重载相应的成员函数,测试这些类。
. 代码如下:
```
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
class Data {
public:
Data() {}
Data(int yy, int mm, int dd);
Data(Data &ap);
~Data();
int get_year();
int get_month();
int get_day();
void set_year(int y);
void set_month(int m);
void set_day(int d);
private:
int year;
int month;
int day;
};
Data::Data(int yy, int mm, int dd) {
year = yy;
month = mm;
day = dd;
}
Data::Data(Data &ap) {
year = ap.year;
month = ap.month;
day = ap.day;
}
Data::~Data() { }
int Data::get_day() {
return day;
}
int Data::get_month() {
return month;
}
int Data::get_year() {
return year;
}
void Data::set_day(int d) {
day = d;
}
void Data::set_month(int m) {
month = m;
}
void Data::set_year(int y) {
year = y;
}
class People {
public:
People(int num, string se, Data birthd, string iid);
People(People &tp);
People() {}
People get_People();
~People() { }
void set_number(int num) {
number = num;
}
void set_sex(string se) {
sex = se;
}
void set_birthday(Data birth) {
birthday = birth;
}
void set_id(string iidd) {
id = iidd;
}
int get_number();
string get_sex();
Data get_birthday();
string get_id();
void details();
private:
int number;
string sex;
Data birthday;
string id;
};
inline int People::get_number() {
return number;
}
inline string People::get_sex() {
return sex;
}
inline string People::get_id() {
return id;
}
Data People::get_birthday() {
return birthday;
}
void People::details() {
cout << "Number:" << number << endl;
cout << "Sex:" << sex << endl;
cout << "Birhtday:" << birthday.get_year() << "/" << birthday.get_month() << "/" << birthday.get_day() << endl;
cout << "ID:" << id << endl;
}
People::People(int num, string se, Data birth, string iid) :birthday(birth) {
number = num;
sex = se;
id = iid;
}
People People::get_People() {
int num, yy, mm, dd;
string ID, se;
cout << "Please enter the number of the people:";
cin >> num;
cout << "Please enter the sex of the people:(male or female)";
cin >> se;
cout << "Please enter the birthday of the people:" << endl
<< "(Warnning:The format is similar to 1998 8 3)" << endl;
cin >> yy >> mm >> dd;
cout << "Please enter the id of the people:";
cin >> ID;
Data birth(yy, mm, dd);
id = ID;
number = num;
sex = se;
birthday = birth;
return *this;
}
People::People(People &tp) {
number = tp.get_number();
sex = tp.get_sex();
id = tp.get_id();
birthday = tp.get_birthday();
}
class Student :virtual public People {
public:
char classNo[];
Student(int num, string se, Data birthd, string iid, char a[]) :People(num, se, birthd, iid) {
strcpy(classNo, a);
}
~Student() { };
void Show_Student() {
cout << "This is student:" << endl;
cout << "ClassNo :" << classNo << endl;
}
};
class Teacher :virtual public People {
public:
char principalship[];
char department[];
Teacher(int num, string se, Data birthd, string iid, char a[],char b[]) :People(num, se, birthd, iid) {
strcpy(principalship, a);
strcpy(department, b);
}
Teacher() { }
void Show_Teacher() {
cout << "This is teacher:" << endl;
cout << "Principalship :" << principalship << endl;
cout << "Department :" << department << endl;
}
};
class Graduate :virtual public Student {
public:
char subject[];
Teacher adviser;
Graduate(int num, string se, Data birthd, string iid, char a[], char c[],Teacher vt) :People(num, se, birthd, iid), Student(num, se, birthd, iid, a) {
strcpy(subject, c);
adviser = vt;
}
~Graduate() { }
void Show_Graduate() {
cout << "This is Graduate:" << endl;
cout << "The subject:" << subject << endl;
cout << "The adviser teacher:" << endl;
cout << "Principalship:" << adviser.principalship<< endl;
cout << "Department:" << adviser.department << endl;
cout << endl;
}
};
class TA :public Graduate, public Teacher {
TA(int num, string se, Data birthd, string iid, char a[], char c[], Teacher vt) :People(num, se, birthd, iid), Student(num, se, birthd, iid, a), Graduate(num, se, birthd, iid, a, c, vt) { }
~TA() { }
void Show_TA() {
cout << "This is TA:" << endl;
cout << "The classNo:" << classNo << endl;
}
};
int main()
{
People asp;
asp.get_People();
asp.details();
Data a(, , );
Student b(,"male",a,"","");
b.Show_Student();
Data a1(, , );
Teacher c(, "female", a1,"","Advanced", "Promotion");
c.Show_Teacher();
Data a2(, , );
Graduate d(, "female", a2, "", "", "CS", c);
d.Show_Graduate();
return ;
}
```
C++继承和派生练习(一)--关于从people(人员)类派生出student(学生)类等的更多相关文章
- 继承的综合运用《Point类派生出Circle类而且进行各种操作》
类的组合与继承 (1)先建立一个Point(点)类.包括数据成员x,y(坐标点). (2)以Point为基类.派生出一个Circle(圆)类,添加数据成员(半径),基类的成员表示圆心: (3)编写上述 ...
- Python基础(16)_面向对象程序设计(类、继承、派生、组合、接口)
一.面向过程程序设计与面向对象程序设计 面向过程的程序设计:核心是过程,过程就解决问题的步骤,基于该思想设计程序就像是在设计一条流水线,是一种机械式的思维方式 优点:复杂的问题的简单化,流程化 缺点: ...
- (74)c++再回顾一继承和派生
一:继承和派生 0.默认构造函数即不带参数的构造函数或者是系统自动生成的构造函数.每一个类的构造函数可以有多个,但是析构函数只能有一个. 1.采用公用public继承方式,则基类的公有成员变量和成员函 ...
- c++学习--继承与派生
继承和派生 1 含有对象成员(子对象)的派生类的构造函数,定义派生类对象成员时,构造函数的执行顺序如下: 1 调用基类的构造函数,对基类数据成员初始化: 2 调用对象成员的构造函数,对对象成员的数据成 ...
- 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成员)
[源码下载] 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成 ...
- [C++]类的继承与派生
继承性是面向对象程序设计的第二大特性,它允许在既有类的基础上创建新类,新类可以继承既有类的数据成员和成员函数,可以添加自己特有的数据成员和成员函数,还可以对既有类中的成员函数重新定义.利用类的继承和派 ...
- C++继承与派生的概念、什么是继承和派生
在C++中可重用性是通过继承(inheritance)这一机制来实现的.因此,继承是C++的一个重要组成部分. 前面介绍了类,一个类中包含了若干数据成员和成员函数.在不同的类中,数据成员和成员函数是不 ...
- O-c中类的继承与派生的概念
什么是继承 众所周知,面向对象的编程语言具有: 抽象性, 封装性, 继承性, 以及多态性 的特征. 那么什么是继承呢? 传统意义上是指从父辈那里获得父辈留下的东西 在开发中, 继承就是"复用 ...
- 程序设计实习MOOC / 继承和派生——编程作业 第五周程序填空题1
描述 写一个MyString 类,使得下面程序的输出结果是: 1. abcd-efgh-abcd- 2. abcd- 3. 4. abcd-efgh- 5. efgh- 6. c 7. abcd- 8 ...
随机推荐
- Linux防火墙命令
linux 查看防火墙状态 1.查看防火墙状态 systemctl status firewalld firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning ...
- intellij idea里神坑的@autowire
当你写完项目的时侯serviceimpl层下的@autowire->对应的是dao层的注入,其下面会出现一条红线 在Intellij Idea开发工具在@Autowired或者@Resource ...
- Jquery系列:checkbox 获取值、选中、设置值、事件监听等操作
<div id="divId" class="divTable"> <div class="tableBody"> ...
- Tomcat配置https之 JDK SSL证书生成与验证
关于证书 SSL证书通过在客户端浏览器和Web服务器之间建立一条SSL安全通道(Secure socket layer(SSL),安全协议是由Netscape Communication公司设计开发. ...
- org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported或其他Content type不支持处理
很久没从头到尾搭框架,今天搭的过程中,springmvc controller方法入参用@RequestBody自动绑定参数时一直提示各种 not supported 排查问题有两个解决路径: 1)使 ...
- CSS基础语法与选择器
CSS基础 语法 : <head> <style type="text/css"> 选择器(即修饰对象){ 修饰属性:属性值; 修饰属性:属性值; } &l ...
- 【基础笔记】tomcat安装后运行出现出现问题(the JRE_HOME environment variable is not defined correctly This environment variabl)
之前装好tomcat后正常运行 后来重装系统后,又一次配置环境时却报错. 在网上查找了两篇文章. https://blog.csdn.net/haleyliu123/article/details/ ...
- 转:SQL Server附加数据库提示“版本为661,无法打开,支持655版本……”
在我们使用别人导出的数据库的时候,有时候我们会通过附加数据库的方法,把别人导出的数据库附加到我们的电脑中,这时,或许你会遇到这种问题,附加时,提示版本为XXX,无法打开,支持AAA版本. 这是怎么回事 ...
- spring@Autowired注入为null的问题,2017年9月14日21点41分记录
这个小问题纠结了三个小时..发出来留个纪念 这是启动项目的时候 这是请求控制器的时候 图1注入的时候是null,图2请求控制器的时候是有的,这是因为图1debug的地方是构造器..autowire ...
- Python——追加学习笔记(四)
函数 1.python里的函数可以返回一个值或者对象,知识在返回一个容器对象的时候有点不同,看起来像是能返回多个对象. >>> def bar(): ... return 'abc' ...
