C++:继承性_程序

编译器:vs2022

main.cpp

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

const int Sum = 2;

class Group
{
protected:
    PostGra st[Sum];
    int sum;
public:
    Group();
    void Input();
    void SortByID();
    void Output();
};

Group::Group()
{
    sum = Sum;
}

void Group::Input()
{
    int i;
    for (i = 0; i < sum; i++)
        st[i].Input();
}

void Group::SortByID()
{
    int index, i, k;
    PostGra temp;
    for (k = 0; k < sum - 1; k++)
    {
        index = k;
        for (i = k + 1; i < sum; i++)
            if (st[i].GetID() < st[index].GetID())
                index = i;
        if (index != k)
        {
            temp = st[index];
            st[index] = st[k];
            st[k] = temp;
        }
    }
}

void Group::Output()
{
    int i;
    std::cout << std::endl << "The group information:" << std::endl;
    std::cout << "ID" << std::setw(10) << "Name" << std::setw(10) << "Age" << std::setw(10) << "Gender" <<
        std::setw(10) << "Spec" << std::setw(10) << "research"<< std::endl;
    for (i = 0; i < sum; i++)
    {
        std::cout << st[i].GetID() << std::setw(10) << st[i].GetName()
            << std::setw(10) << st[i].GetGender() << std::setw(10) << st[i].GetSpeciality()
            << std::setw(10) << st[i].GetResearchTopic() << std::setw(10) << st[i].GetAcademicTitle() << std::endl;
    }
}

int main()
{
    Group group;
    group.Input();
    group.SortByID();
    group.Output();
    return 0;
}

person.h

cpp 复制代码
#pragma once
#ifndef _PERSON
#define _PERSON
#include <string>

class Person
{
protected:
    std::string ID;
    std::string name;
    std::string gender;
public:
    Person(std::string = "000", std::string = "unknown", std::string = "unknown");
    void Input();
    std::string GetID();
    std::string GetName();
    std::string GetGender();
};

class Student : virtual public Person
{
protected:
    std::string speciality;
public:
    Student(std::string, std::string, std::string = "000", std::string = "unknown");
    void Input();
    std::string GetSpeciality();
};

class Graduate :virtual public Student
{
protected:
    std::string researchTopic;
public:
    Graduate(std::string=" ", std::string=" ", std::string=" ", std::string = "000", std::string = "unknown");
    void Input();
    std::string GetResearchTopic();
};

class Teacher :virtual public Person
{
protected:
    std::string academicTitle;
public:
    Teacher(std::string, std::string, std::string, std::string = "unknown");
    void Input();
    std::string GetAcademicTitle();
};

class PostGra :public Graduate, public Teacher
{
public:
    PostGra(std::string=" ", std::string=" ", std::string=" ", std::string=" ", std::string = "000", std::string = "unknown");
    void Input();
};
#endif

person.cpp

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

Person::Person(std::string id, std::string name, std::string gender)
{
    ID = id;
    name = name;
    gender = gender;
}

void Person::Input()
{
    std::cout << "Enter ID: ";
    std::cin >> ID;
    std::cout << "Enter name: ";
    std::cin >> name;
    std::cout << "Enter gender: ";
    std::cin >> gender;
}

std::string Person::GetID()
{
    return ID;
}

std::string Person::GetName()
{
    return name;
}

std::string Person::GetGender()
{
    return gender;
}

Student::Student(std::string id, std::string name, std::string gender, std::string speciality) : Person(id, name, gender)
{
    speciality = speciality;
}

void Student::Input()
{
    Person::Input();
    std::cout << "Enter speciality: ";
    std::cin >> speciality;
}

std::string Student::GetSpeciality()
{
    return speciality;
}

Graduate::Graduate(std::string id, std::string name, std::string gender, std::string speciality, std::string rese) :Person(id, name, gender), Student(id, name, gender, speciality)
{
    researchTopic = rese;
}

void Graduate::Input()
{
    Student::Input();
    std::cout << "Enter research topic: ";
    std::cin >> researchTopic;
}

std::string Graduate::GetResearchTopic()
{
    return researchTopic;
}

Teacher::Teacher(std::string id, std::string name, std::string gender, std::string title) : Person(id, name, gender)
{
    academicTitle = title;
}

void Teacher::Input()
{
    Person::Input();
    std::cout << "Enter academic title: ";
    std::cin >> academicTitle;
}

std::string Teacher::GetAcademicTitle()
{
    return academicTitle;
}

PostGra::PostGra(std::string id, std::string name, std::string gender, std::string speciality, std::string rese, std::string title) : Person(id, name, gender), Student(id, name, gender, speciality), Teacher(id, name, gender, title)
{
    ;
}

void PostGra::Input()
{
    Graduate::Input();
    std::cout << "Enter academic title: ";
    std::cin >> academicTitle;
}
相关推荐
神奇夜光杯4 分钟前
Python酷库之旅-第三方库Pandas(202)
开发语言·人工智能·python·excel·pandas·标准库及第三方库·学习与成长
Themberfue6 分钟前
Java多线程详解⑤(全程干货!!!)线程安全问题 || 锁 || synchronized
java·开发语言·线程·多线程·synchronized·
plmm烟酒僧8 分钟前
Windows下QT调用MinGW编译的OpenCV
开发语言·windows·qt·opencv
测试界的酸菜鱼20 分钟前
Python 大数据展示屏实例
大数据·开发语言·python
我是谁??20 分钟前
C/C++使用AddressSanitizer检测内存错误
c语言·c++
小码农<^_^>21 分钟前
优选算法精品课--滑动窗口算法(一)
算法
羊小猪~~24 分钟前
神经网络基础--什么是正向传播??什么是方向传播??
人工智能·pytorch·python·深度学习·神经网络·算法·机器学习
晨曦_子画29 分钟前
编程语言之战:AI 之后的 Kotlin 与 Java
android·java·开发语言·人工智能·kotlin
Black_Friend37 分钟前
关于在VS中使用Qt不同版本报错的问题
开发语言·qt
软工菜鸡1 小时前
预训练语言模型BERT——PaddleNLP中的预训练模型
大数据·人工智能·深度学习·算法·语言模型·自然语言处理·bert