C++:继承性_程序

简介: C++:继承性_程序

编译器:vs2022

main.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

#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

#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;
}
目录
相关文章
|
3月前
|
C++
C++ 根据程序运行的时间和cpu频率来计算在另外的cpu上运行所花的时间
C++ 根据程序运行的时间和cpu频率来计算在另外的cpu上运行所花的时间
44 0
|
4月前
|
编译器 C++
【C++】详解C++的继承
【C++】详解C++的继承
|
1月前
|
存储 程序员 编译器
简述 C、C++程序编译的内存分配情况
在C和C++程序编译过程中,内存被划分为几个区域进行分配:代码区存储常量和执行指令;全局/静态变量区存放全局变量及静态变量;栈区管理函数参数、局部变量等;堆区则用于动态分配内存,由程序员控制释放,共同支撑着程序运行时的数据存储与处理需求。
103 21
|
1月前
|
安全 程序员 编译器
【C++篇】继承之韵:解构编程奥义,领略面向对象的至高法则
【C++篇】继承之韵:解构编程奥义,领略面向对象的至高法则
81 11
|
1月前
|
C++
C++番外篇——对于继承中子类与父类对象同时定义其析构顺序的探究
C++番外篇——对于继承中子类与父类对象同时定义其析构顺序的探究
53 1
|
1月前
|
C++
C++番外篇——虚拟继承解决数据冗余和二义性的原理
C++番外篇——虚拟继承解决数据冗余和二义性的原理
39 1
|
2月前
|
C++
【C++基础】程序流程结构详解
这篇文章详细介绍了C++中程序流程的三种基本结构:顺序结构、选择结构和循环结构,包括if语句、三目运算符、switch语句、while循环、do…while循环、for循环以及跳转语句break、continue和goto的使用和示例。
45 2
|
1月前
|
安全 编译器 程序员
C++的忠实粉丝-继承的热情(1)
C++的忠实粉丝-继承的热情(1)
18 0
|
1月前
|
编译器 C++
C++入门11——详解C++继承(菱形继承与虚拟继承)-2
C++入门11——详解C++继承(菱形继承与虚拟继承)-2
30 0
|
1月前
|
程序员 C++
C++入门11——详解C++继承(菱形继承与虚拟继承)-1
C++入门11——详解C++继承(菱形继承与虚拟继承)-1
32 0