. 从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(学生)类等的更多相关文章

  1. 继承的综合运用《Point类派生出Circle类而且进行各种操作》

    类的组合与继承 (1)先建立一个Point(点)类.包括数据成员x,y(坐标点). (2)以Point为基类.派生出一个Circle(圆)类,添加数据成员(半径),基类的成员表示圆心: (3)编写上述 ...

  2. Python基础(16)_面向对象程序设计(类、继承、派生、组合、接口)

    一.面向过程程序设计与面向对象程序设计 面向过程的程序设计:核心是过程,过程就解决问题的步骤,基于该思想设计程序就像是在设计一条流水线,是一种机械式的思维方式 优点:复杂的问题的简单化,流程化 缺点: ...

  3. (74)c++再回顾一继承和派生

    一:继承和派生 0.默认构造函数即不带参数的构造函数或者是系统自动生成的构造函数.每一个类的构造函数可以有多个,但是析构函数只能有一个. 1.采用公用public继承方式,则基类的公有成员变量和成员函 ...

  4. c++学习--继承与派生

    继承和派生 1 含有对象成员(子对象)的派生类的构造函数,定义派生类对象成员时,构造函数的执行顺序如下: 1 调用基类的构造函数,对基类数据成员初始化: 2 调用对象成员的构造函数,对对象成员的数据成 ...

  5. 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成员)

    [源码下载] 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成 ...

  6. [C++]类的继承与派生

    继承性是面向对象程序设计的第二大特性,它允许在既有类的基础上创建新类,新类可以继承既有类的数据成员和成员函数,可以添加自己特有的数据成员和成员函数,还可以对既有类中的成员函数重新定义.利用类的继承和派 ...

  7. C++继承与派生的概念、什么是继承和派生

    在C++中可重用性是通过继承(inheritance)这一机制来实现的.因此,继承是C++的一个重要组成部分. 前面介绍了类,一个类中包含了若干数据成员和成员函数.在不同的类中,数据成员和成员函数是不 ...

  8. O-c中类的继承与派生的概念

    什么是继承 众所周知,面向对象的编程语言具有: 抽象性, 封装性, 继承性, 以及多态性 的特征. 那么什么是继承呢? 传统意义上是指从父辈那里获得父辈留下的东西 在开发中, 继承就是"复用 ...

  9. 程序设计实习MOOC / 继承和派生——编程作业 第五周程序填空题1

    描述 写一个MyString 类,使得下面程序的输出结果是: 1. abcd-efgh-abcd- 2. abcd- 3. 4. abcd-efgh- 5. efgh- 6. c 7. abcd- 8 ...

随机推荐

  1. [转]MySQL索引方法

    此为转载文章,仅做记录使用,方便日后查看,原文链接:https://www.cnblogs.com/luyucheng/p/6289048.html MySQL索引方法   MySQL目前主要有以下几 ...

  2. all-to-mqtt

  3. Netty-EventLoop

    1. public interface EventLoop extends EventExecutor, EventLoopGroup 2. public interface EventExecuto ...

  4. SharedPreferences的封装

    android.content.SharedPreferences是一个接口,用来获取和修改持久化存储的数据.有三种获取系统中保存的持久化数据的方式: 1). public SharedPrefere ...

  5. Android自定义验证码输入框

    未经允许,禁止

  6. 增加C盘空间大小

    随着我们使用电脑的时间越来越久,电脑C盘的空间会出现不够用的情况,这时我们需要的就是增加C盘的大小,基本上有两种方式 1.通过系统自带的磁盘管理(有可能没法操作,主要介绍第二种) 2.通过分区软件进行 ...

  7. JUnit报错:java.lang.ClassNotFoundException: com.mogodb.test.test

    最近在使用JUnit做单元测试时,发现新写好的测试类运行总是出错,报找不到类异常. Class not found com.mogodb.test.test java.lang.ClassNotFou ...

  8. May 16th 2017 Week 20th Tuesday

    The most fearful enemy is not having a firm conviction. 最可怕的敌人,就是没有坚强的信念. A firm conviction or belie ...

  9. March 31 2017 Week 13 Friday

    Sometimes, you think the sky is falling down, actually, that is just because you stand slanting. 有时候 ...

  10. March 27 2017 Week 13 Monday

    A book that remains shut is but a block. 有书闭卷不阅读,无异于一块木头. I had planned to buy a book and read it ev ...