类继承的典型案例

Date.h

#ifndef DATECLASS
#define DATECLASS
#include <iostream>
using namespace std;
class Date
{
protected:
    int year;
    int month;
    int day;
public:
    Date(){}
    Date(int year=0,int month=0,int day=0);
    Date(Date &d);
    void show()const;
};
#endif

Date,cpp

#include"Date.h"
Date::Date(int year,int month,int day)
{
    this->year=year;
    this->month=month;
    this->day=day;
}
Date::Date(Date &d)
{
    year=d.year;
    month=d.month;
    day=d.day;
}
void Date::show()const
{
    cout<<year<<"/"<<month<<"/"<<day<<endl;
}
People.h

#ifndef PEOPLECLASS
#define PEOPLECLASS
#include<string>
#include"Date.h"
class People
{
protected:
    string name,sex,number,identify;
    Date birthday;
public:
    People(string name,string sex,string number,string identify,Date b);
    People(People &p);
    void show()const;
};
#endif

People.cpp

#include"People.h"
People::People(string name,string sex,string number,string identify,Date b):birthday(b)
    {
        this->name=name;
        this->sex=sex;
        this->number=number;
        this->identify=identify;
    }
People::People(People &p):birthday(p.birthday)
{
    name=p.name;
    sex=p.sex;
    number=p.number;
    identify=p.identify;
}
void People::show()const
{
    cout<<"name: "<<name<<endl;
    cout<<"sex: "<<sex<<endl;
    cout<<"birthday: ";
    birthday.show();
    cout<<"number: "<<number<<endl;
    cout<<"identify: "<<identify<<endl;
}

Student.h

#ifndef STUDENTCLASS
#define STUDENTCLASS
#include"People.h"
class Student:virtual public People
{
protected:
    int classno;
public:
    Student(People &p,int classno);
    Student(Student &s);
    void show()const;
};
#endif

Student.cpp

#include"Student.h"
Student::Student(People &p,int classno):People(p)
{
    this->classno=classno;
}
Student::Student(Student &s):People(s)
{
    classno=s.classno;
}
void Student::show()const
{
    People::show();
    cout<<"classno: "<<classno<<endl;
}

Teacher.h

#ifndef TEACHERCLASS
#define TEACHERCLASS
#include"People.h"
class Teacher:virtual public People
{
protected:
    string principaiship,department;
public:
    Teacher(People &p,string principaiship,string department);
    Teacher(Teacher &t);
    void show()const;
    void show1()const;
};
#endif

Teacher.cpp

#include"Teacher.h"
Teacher::Teacher(People &p,string principaiship,string department):People(p)
{
    this->principaiship=principaiship;
    this->department=department;
}
Teacher::Teacher(Teacher &t):People(t)
{
    principaiship=t.principaiship;
    department=t.department;
}
void Teacher::show()const
{
    People::show();
    cout<<"principaiship: "<<principaiship<<endl;
    cout<<"department: "<<department<<endl;
}
void Teacher::show1()const
{
    cout<<"principaiship: "<<principaiship<<endl;
    cout<<"department: "<<department<<endl;
}

Graduate.h

#ifndef GRADUATECLASS
#define GRADUATECLASS
#include"Student.h"
#include"Teacher.h"
class Graduate:public Student
{
protected:
    string major;
    Teacher t;
public:
    Graduate(Student &s,Teacher nt,string major);
    Graduate(Graduate &g);
    void show()const;
};
#endif

Graduate.cpp

#include"Graduate.h"
Graduate::Graduate(Student &s,Teacher nt,string major):People(s),Student(s),t(nt)
{
    this->major=major;
}
Graduate::Graduate(Graduate &g):People(g),Student(g),t(g.t)
{
    major=g.major;
}
void Graduate::show()const
{
    Student::show();
    cout<<"major: "<<major<<endl;
    cout<<"my tutor:"<<endl;
    t.show();
}
Double.h

#ifndef DOUBLECLASS
#define DOUBLECLASS
#include"Teacher.h"
#include"Graduate.h"
class Double:public Teacher,public Graduate
{
public:
    Double(Teacher &t,Graduate &g);
    Double(Double &d);
    void show()const;
};
#endif
Double.cpp

#include"Double.h"
Double::Double(Teacher &t,Graduate &g):People(g),Teacher(t),Graduate(g){}
Double::Double(Double &d):People(d),Teacher(d),Graduate(d){}
void Double::show()const
{
    cout<<"Double:"<<endl;
    cout<<"我的学业:"<<endl;
    Graduate::show();
    cout<<"我的工作:"<<endl;
    Teacher::show1();
}
main.cpp

#include"Date.h"
#include"People.h"
#include"Student.h"
#include"Teacher.h"
#include"Graduate.h"
#include"Double.h"
int main()
{
    Date d1(1995,9,18),d2(1989,8,12);
    People p1("Black Manba","M","120","120244199509181516",d1);
    People p2("Baymax","F","121","1202245648899449",d2);
    cout<<"People: "<<endl;
    p1.show();
    cout<<"-------------------------------"<<endl;
    cout<<"People: "<<endl;
    p2.show();
    cout<<"-------------------------------"<<endl;
    Student s(p1,8);
    cout<<"Student: "<<endl;
    s.show();
    cout<<"-------------------------------"<<endl;
    Teacher t(p2,"professor","math");
    cout<<"Teacher: "<<endl;
    t.show();
    cout<<"-------------------------------"<<endl;
    Graduate g(s,t,"computer");
    cout<<"Graduate: "<<endl;
    g.show();
    cout<<"-------------------------------"<<endl;
    Double d(t,g);
    d.show();
    cout<<"-------------------------------"<<endl;
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值