C++之Student和Teacher继承Person类

main.cpp

cpp 复制代码
#include <iostream>
#include "Person.h"

#include "Student.h"

#include "Teacher.h"
using namespace std;

int main()
{
    Person p1("Alice", 1, "123 Street");
    Person p2("Bob", 2, "456 Avenue");

    Student s1("Charlie", 3, "789 Boulevard", Date(2000, 3, 1), "Math", 1001);
    Student s2("David", 4, "101 Lane", Date(2001, 7, 22), "Physics", 1002);

    Teacher t1("Eve", 5, "202 Road", Date(1975, 1, 1), "Biology", 50000);

    Person* people[] = {&p1, &p2, &s1, &s2, &t1}; // 容器

    for (const auto& person : people) // output
    {
        person->print();
    }

    return 0;
}

Person.h

cpp 复制代码
#ifndef PERSON_H

#define PERSON_H
#include <iostream>
#include "Date.h"
using namespace std;

class Person
{
public:
    Person();
    Person(string name, int id, string address);
    ~Person();

    void setPerson(string name, int id, string address);
    void setName(string name);
    void setID(int id);
    void setAddress(string address);
    void setBirthday(Date d);

    string getName();
    int getID();
    string getAddress();
    Date getBirthday();

    virtual void print(); // outPutResult
private:
    string Name;
    int ID;
    string Address;
    Date Birthday;
};
#endif // PERSON_H

Student.h

cpp 复制代码
#ifndef STUDENT_H

#define STUDENT_H
#include <iostream>
#include "Person.h"
#include "Date.h"
using namespace std;

class Student :public Person
{
public:
    Student();
    Student(string name, int id, string address, Date date, string department, int studentID);
    ~Student();

    void setDepartment(string department);
    void setStudentID(int studentID);

    string getDepartment();
    int getStudentID();

    virtual void print(); // outPutResult
private:
    string Department;
    int StudentID;
};
#endif // STUDENT_H

Teacher.h

cpp 复制代码
#ifndef TEACHER_H

#define TEACHER_H
#include "Person.h"
#include "Date.h"
using namespace std;

class Teacher :public Person
{
public:
    Teacher();
    Teacher(string name, int id, string address, Date date, string department, double salary);
    ~Teacher();

    void setDepartment(string department);
    void setSalary(double salary);

    string getDepartment();
    double getSalary();

    virtual void print(); // outPutResult
private:
    string Department;
    double Salary;
};
#endif // TEACHER_H

Person.cpp

cpp 复制代码
#include "Person.h"
#include <iostream>
using namespace std;

Person::Person()
{
    Name = "S.M.Wang";
    ID = 070145;
    Address = "莲花路200号";
}

Person::Person(string name, int id, string address)
{
    setPerson(name, id, address);
}

Person::~Person()
{
    //cout << "object Destructor is called" << endl;
}


void Person::setPerson(string name, int id, string address)
{
    Name = name;
    ID = id;
    Address = address;
}


void Person::setName(string name)
{
    Name = name;
}

void Person::setID(int id)
{
    ID = id;
}

void Person::setAddress(string address)
{
    Address = address;
}

string Person::getName()
{
    return Name;
}

int Person::getID()
{
    return ID;
}

string Person::getAddress()
{
    return Address;
}


void Person::setBirthday(Date d) // 调用的形参是类
{
    Birthday = d;
}

Date Person::getBirthday() // 返回的是类
{
    return Birthday;
}

void Person::print()
{
    cout << "Name:" << getName() << endl;
    cout << "ID:" << getID() << endl;
    cout << "Address:" << getAddress() << endl;

    cout << "Birthday:" << getBirthday().getYear(); // getBirthday()返回的是类,再调用类中的子函数满足cout的返回值。
    cout << " " << getBirthday().getMonth();
    cout << " " << getBirthday().getDay() << endl;
    cout << endl;
}

Student.cpp

cpp 复制代码
#include "Student.h"
#include <iostream>
using namespace std;

Student::Student()
{

}

Student::Student(string name, int id, string address, Date date, string department, int studentID)
{
    setPerson(name, id, address);
    setBirthday(date);
    setDepartment(department);
    setStudentID(studentID);
}

Student::~Student()
{
    //cout << "object Destructor is called" << endl;
}

void Student::setDepartment(string department)
{
    Department = department;
}

string Student::getDepartment()
{
    return Department;
}

void Student::setStudentID(int studentID)
{
    StudentID = studentID;
}

int Student::getStudentID()
{
    return StudentID;
}

void Student::print()
{
    cout << "Name:" << getName() << endl;
    cout << "ID:" << getID() << endl;
    cout << "Address:" << getAddress() << endl;

    cout << "Birthday:" << getBirthday().getYear(); // getBirthday()返回的是类,再调用类中的子函数满足cout的返回值。
    cout << " " << getBirthday().getMonth();
    cout << " " << getBirthday().getDay() << endl;

    cout << "Department:" << getDepartment() << endl;
    cout << "StudentID:" << getStudentID() << endl;
    cout << endl;

}

Teacher.cpp

cpp 复制代码
#include "Teacher.h"
#include <iostream>
using namespace std;

Teacher::Teacher()
{

}

Teacher::Teacher(string name, int id, string address, Date date, string department, double salary)
{
    setPerson(name, id, address);
    setBirthday(date);
    setDepartment(department);
    setSalary(salary);
}

Teacher::~Teacher()
{
    //cout << "object Destructor is called" << endl;
}

void Teacher::setDepartment(string department)
{
    Department = department;
}

string Teacher::getDepartment()
{
    return Department;
}

void Teacher::setSalary(double salary)
{
    Salary = salary;
}

double Teacher::getSalary()
{
    return Salary;
}

void Teacher::print()
{
    cout << "Name:" << getName() << endl;
    cout << "ID:" << getID() << endl;
    cout << "Address:" << getAddress() << endl;

    cout << "Birthday:" << getBirthday().getYear(); // getBirthday()返回的是类,再调用类中的子函数满足cout的返回值。
    cout << " " << getBirthday().getMonth();
    cout << " " << getBirthday().getDay() << endl;

    cout << "Department:" << getDepartment() << endl;
    cout << "Salary:" << getSalary() << endl;
    cout << endl;
}

Result:

相关推荐
东风吹柳2 分钟前
观察者模式(sigslot in C++)
c++·观察者模式·信号槽·sigslot
A懿轩A10 分钟前
C/C++ 数据结构与算法【栈和队列】 栈+队列详细解析【日常学习,考研必备】带图+详细代码
c语言·数据结构·c++·学习·考研·算法·栈和队列
汪洪墩12 分钟前
【Mars3d】设置backgroundImage、map.scene.skyBox、backgroundImage来回切换
开发语言·javascript·python·ecmascript·webgl·cesium
Python机器学习AI15 分钟前
分类模型的预测概率解读:3D概率分布可视化的直观呈现
算法·机器学习·分类
云空17 分钟前
《QT 5.14.1 搭建 opencv 环境全攻略》
开发语言·qt·opencv
Anna。。20 分钟前
Java入门2-idea 第五章:IO流(java.io包中)
java·开发语言·intellij-idea
我曾经是个程序员43 分钟前
鸿蒙学习记录
开发语言·前端·javascript
爱上语文43 分钟前
宠物管理系统:Dao层
java·开发语言·宠物
吕小明么1 小时前
OpenAI o3 “震撼” 发布后回归技术本身的审视与进一步思考
人工智能·深度学习·算法·aigc·agi
大胆飞猪1 小时前
C++9--前置++和后置++重载,const,日期类的实现(对前几篇知识点的应用)
c++