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 ...
随机推荐
- Regular Expression 正则表达式
1. "^"表示以什么字符开始,"$"表示以什么字符结束: 2. \w表示字符类,包括大小写字母和数字: 3. “+”表示一个或多个,"*" ...
- SharePoint Config database Log file too big – reduce it!
SharePoint Config database logs are one thing to keep an eye on since they do have a tendency to gro ...
- Linux中如何安装配置Mysql和SVN服务端
目标Linux系统为centOS 一.安装登陆mysql 1.直接以root用户运行:yum install mysql 和yum install mysql-server等带安装完成. 2.安装 ...
- apache poi根据模板导出excel
需要预先新建编辑好一个excel文件,设置好样式. 编辑好输出的数据,根据excel坐标一一对应. 支持列表数据输出,列表中列合并. 代码如下: package com.icourt.util; im ...
- JVM 虚拟机内存深入探究
[<深入理解java虚拟机>-整理笔记] by lijun JVM虚拟机内存逻辑模型: 方法区(全局变量 静态数据 常量等) 线程共享 堆栈区(对象实例 数组数据 new generat ...
- hdu-1452 Happy 2004---因子和+逆元
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1452 题目大意: 求2004^x次方的因子和mod29的值 解题思路: 首先2004 = 2 * 2 ...
- matlab各类数据l图像之间的转化
matlab各类数据图像之间的转化 rgb类型转化为二值的步骤例如以下: 1.採用命令im2double将rgb类型转化三维的double >> str='E:\programing\Ei ...
- 【LOJ6041】「雅礼集训 2017 Day7」事情的相似度(用LCT维护SAM的parent树)
点此看题面 大致题意: 给你一个\(01\)串,每次询问前缀编号在一段区间内的两个前缀的最长公共后缀的长度. 离线存储询问 考虑将询问离线,按右端点大小用邻接表存下来(直接排序当然也可以啦). 这样的 ...
- Jupyter notebook 的一个问题
Traceback (most recent call last): File , in get value = obj._trait_values[self.name] KeyError: 'all ...
- 【转】 ios的手势操作之UIGestureRecognizer浅析
一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touchesBegan:(NSSet *)touches withE ...