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;
}
目录
相关文章
|
17天前
|
编译器 C++
【C++】详解C++的继承
【C++】详解C++的继承
|
6天前
|
存储 Java 程序员
【c++】继承深度解剖
【c++】继承深度解剖
9 1
|
15天前
|
存储 编译器 数据安全/隐私保护
|
22天前
|
C++
C++职工管理系统(类继承、文件、指针操作、中文乱码解决)
C++职工管理系统(类继承、文件、指针操作、中文乱码解决)
20 0
C++职工管理系统(类继承、文件、指针操作、中文乱码解决)
|
22天前
|
C++
【C++】学习笔记——继承_2
【C++】学习笔记——继承_2
19 1
|
24天前
|
编译器 C++
C++中的继承
C++中的继承
20 1
|
24天前
|
C语言 图形学 C++
|
27天前
|
C++
C++一分钟之-继承与多态概念
【6月更文挑战第21天】**C++的继承与多态概述:** - 继承允许类从基类复用代码,增强代码结构和重用性。 - 多态通过虚函数实现,使不同类对象能以同一类型处理。 - 关键点包括访问权限、构造/析构、菱形问题、虚函数与动态绑定。 - 示例代码展示如何创建派生类和调用虚函数。 - 注意构造函数初始化、空指针检查和避免切片问题。 - 应用这些概念能提升程序设计和维护效率。
22 2
|
15天前
|
存储 编译器 C++
C++基础知识(六:继承)
多态是面向对象编程的四大基本原则之一,它让程序能够以统一的接口处理不同的对象类型,从而实现了接口与实现分离,提高了代码的灵活性和复用性。多态主要体现在两个层面:静态多态(编译时多态,如函数重载)和动态多态(运行时多态,主要通过虚函数实现)。