1480: 多重继承派生(4)--person、student、teacher和graduate类

2023-11-06 22:30

本文主要是介绍1480: 多重继承派生(4)--person、student、teacher和graduate类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1480: 多重继承派生(4)--person、student、teacher和graduate类

Description

Person类包含私有成员数据姓名name(string),编号code(int)和出生年月日。Student类包含私有成员数据姓名name(string),编号code(int),出生年月日和分数score(int)。Teacher类包含私有成员数据姓名name(string),编号code(int),出生年月日和所在系department(string)。Graduate类包含私有成员数据姓名name(string),编号code(int),出生年月日,分数score(int)和所在系department(string)。
请根据给定的main函数以及运行结果设计相应的类及相互间的继承关系。
main函数已给定(如下所示),提交时只需要提交main函数外的代码部分。
int main()
{
string name,department;
int Cas=0,code,year,month,day,score;
while(cin>>name>>code>>year>>month>>day>>score>>department)
{
Cas++;
cout<<"CASE #"<<Cas<<":"<<endl;
Person person(name,code,year,month,day);
Student student(name,code,year,month,day,score);
Teacher teacher(name,code,year,month,day,department);
Graduate graduate(name,code,year,month,day,score,department);
Show(&person);
Show(&student);
Show(&teacher);
Show(&graduate);
}
return 0;
}

Input

包含多组数据(数据均正确合法)
每组测试数据1行,分别表示姓名,编号,出生日期,分数和部门。

Output

每组测试数据输出具体格式详见Sample Output。

Sample Input 

张三 201506 1978 6 14 85 计算机
王大夫 2012510 1984 6 7 82 机械
殷文琦 1005210 1980 2 4 86 电信

Sample Output

CASE #1:
Person::Constructor Function is called.
Person::Constructor Function is called.
Student::Constructor Function is called.
Person::Constructor Function is called.
Teacher::Constructor Function is called.
Person::Constructor Function is called.
Student::Constructor Function is called.
Teacher::Constructor Function is called.
Graduate::Constructor Function is called.
Show Function is called.
Person::Show Function is called.
NAME:张三 Code:201506 BIRTHDAY:1978-6-14
Show Function is called.
Student::Show Function is called.
NAME:张三 Code:201506 SCORE:85 BIRTHDAY:1978-6-14
Show Function is called.
Teacher::Show Function is called.
NAME:张三 Code:201506 DEPARTMENT:计算机 BIRTHDAY:1978-6-14
Show Function is called.
Graduate::Show Function is called.
NAME:张三 Code:201506 SCORE:85 DEPARTMENT:计算机 BIRTHDAY:1978-6-14
CASE #2:
Person::Constructor Function is called.
Person::Constructor Function is called.
Student::Constructor Function is called.
Person::Constructor Function is called.
Teacher::Constructor Function is called.
Person::Constructor Function is called.
Student::Constructor Function is called.
Teacher::Constructor Function is called.
Graduate::Constructor Function is called.
Show Function is called.
Person::Show Function is called.
NAME:王大夫 Code:2012510 BIRTHDAY:1984-6-7
Show Function is called.
Student::Show Function is called.
NAME:王大夫 Code:2012510 SCORE:82 BIRTHDAY:1984-6-7
Show Function is called.
Teacher::Show Function is called.
NAME:王大夫 Code:2012510 DEPARTMENT:机械 BIRTHDAY:1984-6-7
Show Function is called.
Graduate::Show Function is called.
NAME:王大夫 Code:2012510 SCORE:82 DEPARTMENT:机械 BIRTHDAY:1984-6-7
CASE #3:
Person::Constructor Function is called.
Person::Constructor Function is called.
Student::Constructor Function is called.
Person::Constructor Function is called.
Teacher::Constructor Function is called.
Person::Constructor Function is called.
Student::Constructor Function is called.
Teacher::Constructor Function is called.
Graduate::Constructor Function is called.
Show Function is called.
Person::Show Function is called.
NAME:殷文琦 Code:1005210 BIRTHDAY:1980-2-4
Show Function is called.
Student::Show Function is called.
NAME:殷文琦 Code:1005210 SCORE:86 BIRTHDAY:1980-2-4
Show Function is called.
Teacher::Show Function is called.
NAME:殷文琦 Code:1005210 DEPARTMENT:电信 BIRTHDAY:1980-2-4
Show Function is called.
Graduate::Show Function is called.
NAME:殷文琦 Code:1005210 SCORE:86 DEPARTMENT:电信 BIRTHDAY:1980-2-4

代码:

#include <iostream>


#include <string>
using namespace std;

class Person
{
public:
    Person(string name,int code,int year,int month,int day):name(name),code(code),year(year),month(month),day(day)
    {
        cout<<"Person::Constructor Function is called."<<endl;
    }
    virtual void Show()
    {
        cout<<"Person::Show Function is called."<<endl;
        cout<<"NAME:"<<name<<" Code:"<<code<<" BIRTHDAY:"<<year<<"-"<<month<<"-"<<day<<endl;
    }
    string GetName()
    {
        return name;
    }
    int GetCode()
    {
        return code;
    }
    int GetYear()
    {
        return year;
    }
    int GetMonth()
    {
        return month;
    }
    int GetDay()
    {
        return day;
    }
private:
    string name;
    int code;
    int year,month,day;
};

class Student:virtual public Person
{
public:
    Student(string name,int code,int year,int month,int day,int score):Person(name,code,year,month,day),score(score)
    {
        cout<<"Student::Constructor Function is called."<<endl;
    }
    void Show()
    {
        cout<<"Student::Show Function is called."<<endl;
        cout<<"NAME:"<<GetName()<<" Code:"<<GetCode()<<" SCORE:"<<GetScore()<<" BIRTHDAY:"<<GetYear()<<"-"<<GetMonth()<<"-"<<GetDay()<<endl;
    }
    int GetScore()
    {
        return score;
    }
private:
    int score;
};


class Teacher:virtual public Person
{
public:
    Teacher(string name,int code,int year,int month,int day,string department):Person(name,code,year,month,day),department(department)
    {
        cout<<"Teacher::Constructor Function is called."<<endl;
    }
    void Show()
    {
        cout<<"Teacher::Show Function is called."<<endl;
        cout<<"NAME:"<<GetName()<<" Code:"<<GetCode()<<" DEPARTMENT:"<<department<<" BIRTHDAY:"<<GetYear()<<"-"<<GetMonth()<<"-"<<GetDay()<<endl;
    }
    string GetDepartment()
    {
        return department;
    }
private:
    string department;
};
class Graduate:public Student,public Teacher
{
public:
    Graduate(string name,int code,int year,int month,int day,int score,string department):Person(name,code,year,month,day),Student(name,code,year,month,day,score),Teacher(name,code,year,month,day,department)
    {
        cout<<"Graduate::Constructor Function is called."<<endl;
    }
    void Show()
    {
        cout<<"Graduate::Show Function is called."<<endl;
        cout<<"NAME:"<<GetName()<<" Code:"<<GetCode()<<" SCORE:"<<GetScore()<<" DEPARTMENT:"<<GetDepartment()<<" BIRTHDAY:"<<GetYear()<<"-"<<GetMonth()<<"-"<<GetDay()<<endl;
    }
};
void Show(Person* p)
{
    cout<<"Show Function is called."<<endl;
    p->Show();
}
int main()
{
    string name,department;
    int Cas=0,code,year,month,day,score;
    while(cin>>name>>code>>year>>month>>day>>score>>department)
    {
        Cas++;
        cout<<"CASE #"<<Cas<<":"<<endl;
        Person person(name,code,year,month,day);
        Student student(name,code,year,month,day,score);
        Teacher teacher(name,code,year,month,day,department);
        Graduate graduate(name,code,year,month,day,score,department);
        Show(&person);
        Show(&student);
        Show(&teacher);
        Show(&graduate);
    }
    return 0;
}

这篇关于1480: 多重继承派生(4)--person、student、teacher和graduate类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/359419

相关文章

Pandas中多重索引技巧的实现

《Pandas中多重索引技巧的实现》Pandas中的多重索引功能强大,适用于处理多维数据,本文就来介绍一下多重索引技巧,具有一定的参考价值,感兴趣的可以了解一下... 目录1.多重索引概述2.多重索引的基本操作2.1 选择和切片多重索引2.2 交换层级与重设索引3.多重索引的高级操作3.1 多重索引的分组聚

hdu1171(母函数或多重背包)

题意:把物品分成两份,使得价值最接近 可以用背包,或者是母函数来解,母函数(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v) 其中指数为价值,每一项的数目为(该物品数+1)个 代码如下: #include<iostream>#include<algorithm>

多重背包转换成0-1背包

http://acm.hdu.edu.cn/showproblem.php?pid=2191 多重背包特点: 一种物品有C个(既不是固定的1个,也不是无数个) 优化的方法: 运用神奇的二进制,进行物品拆分,转化成01背包 物品拆分,把13个相同的物品分成4组(1,2,4,6) 用这4组可以组成任意一个1~13之间的数! 原理:一个数总可以用2^

JavaSE——封装、继承和多态

1. 封装 1.1 概念      面向对象程序三大特性:封装、继承、多态 。而类和对象阶段,主要研究的就是封装特性。何为封装呢?简单来说就是套壳屏蔽细节 。     比如:对于电脑这样一个复杂的设备,提供给用户的就只是:开关机、通过键盘输入,显示器, USB 插孔等,让用户来和计算机进行交互,完成日常事务。但实际上:电脑真正工作的却是CPU 、显卡、内存等一些硬件元件。

七、Maven继承和聚合关系、及Maven的仓库及查找顺序

1.继承   2.聚合   3.Maven的仓库及查找顺序

leetcode#551. Student Attendance Record I

题目 You are given a string representing an attendance record for a student. The record only contains the following three characters: ‘A’ : Absent. ‘L’ : Late. ‘P’ : Present. A student could be rew

OOP三个基本特征:封装、继承、多态

OOP三个基本特征:封装、继承、多态 C++编程之—面向对象的三个基本特征 默认分类 2008-06-28 21:17:04 阅读12 评论1字号:大中小     面向对象的三个基本特征是:封装、继承、多态。     封装 封装最好理解了。封装是面向对象的特征之一,是对象和类概念的主要特性。   封装,也就是把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信

c++ 类的继承详解

在 C++ 中,类的继承是一种面向对象编程(OOP)的特性,允许创建一个新的类(派生类)从一个已有的类(基类)派生。通过继承,派生类可以重用基类的属性和行为,并且可以扩展或修改这些行为。继承是一种代码重用和扩展的机制,使得派生类能够继承基类的特性并添加或修改特性。 1. 继承的基本语法 class Base {// 基类的成员};class Derived : public Base {//

C++ 第8章 继承

继承(Inheritance)是面向对象程序设计中软件重用的关键技术。 8.1 类之间的关系 一个大的应用程序,通常由多个类构成,类与类之间互相协同工作。 class Vehicle{int wheels;double weight;double loading;public:void initialize(int in_wheels, double in_weight);int get