C++ 实验四 继承和派生

  • Post author:
  • Post category:其他


设计并编写Person(人员)类、Student(学生)类和Graduate(研究生)类,并测试这些类。

Person.h

#ifndef SHIYANSI_PERSON_H
#define SHIYANSI_PERSON_H

#include<iostream>
using namespace std;

class Person {

public :
    Person();
    Person(char n[10],char s,int a);
    Person(Person &p);
    ~Person();
    void SetInfo(char n[10],char s,int a);
    void Show();
private :
    char name[10];
    char sex;
    int age;
};


#endif //SHIYANSI_PERSON_H

Person.cpp

#include <cstring>
#include "Person.h"

Person::Person() {}
Person::Person(char *n, char s, int a)
{
    strcpy(name,n);
    sex=s;
    age=a;
}
Person::Person(Person &p)
{
    strcpy(name,p.name);
    sex=p.sex;
    age=p.age;

}
Person::~Person()
{

}
void Person::SetInfo(char *n, char s, int a)
{
    strcpy(name,n);
    sex=s;
    age=a;
}
void Person::Show()
{
    cout<<"姓名:"<<name<<" 性别:"<<sex<<" 年龄:"<<age<<endl;
}

Student.h

#ifndef SHIYANSI_STUDENT_H
#define SHIYANSI_STUDENT_H
#include"Person.h"

class Student: public Person{
private:
    int Num;
    char School[10];
    char Major[10];
public :
    Student();
    Student(char n[10],char s,int a,int num,char school[10],char major[10]);
    Student(Student &s);
    ~Student();
    void SetInfo1(int num,char school[10],char major[10]);
    void Show1();

};


#endif //SHIYANSI_STUDENT_H

Student.cpp

#include <cstring>
#include "Student.h"

Student::Student()
{

}
Student::Student(char n[10],char s,int a,int num, char *school, char *major): Person(n,s,a)
{
    Num=num;
    strcpy(School,school);
    strcpy(Major,major);
}
Student::Student(Student &s): Person(s)
{
    Num=s.Num;
    strcpy(School,s.School);
    strcpy(Major,s.Major);
}
Student::~Student() {}

void Student::SetInfo1(int num, char *school, char *major)
{
    Num=num;
    strcpy(School,school);
    strcpy(Major,major);
}
void Student::Show1()
{
    Person::Show();
    cout<<"学号:"<<Num<<" 学校:"<<School<<" 专业:"<<Major<<endl;
}

Graduate.h

#ifndef SHIYANSI_GRADUATE_H
#define SHIYANSI_GRADUATE_H
#include"Student.h"
#include<iostream>
using namespace std;

class Graduate :public Student{

private:
    char Research_Direction[10];
public :
    Graduate();
    Graduate(char n[10],char s,int a,int num,char school[10],char major[10],char research_Direction[10]);
    Graduate(Graduate &g);
    ~Graduate();
    void SetInfo2(char research_Direction[10]);
    void Show2();

};

#endif //SHIYANSI_GRADUATE_H

Graduate.cpp

#include <cstring>
#include "Graduate.h"

Graduate::Graduate()
{

}
Graduate::Graduate(char n[10],char s,int a,int num,char school[10],char major[10],char research_Direction[10]): Student(n,s,a,num,school,major)
{
    strcpy(Research_Direction,research_Direction);

}
Graduate::Graduate(Graduate &g): Student(g)
{
    strcpy(Research_Direction,g.Research_Direction);
}
Graduate::~Graduate() {}
void Graduate::SetInfo2(char *research_Direction)
{
    strcpy(Research_Direction,research_Direction);
}
void Graduate::Show2()
{
    Student::Show1();
    cout<<"研究方向:"<<Research_Direction<<endl;
}

main.cpp

#include <iostream>
#include"Person.h"
#include"Student.h"
#include"Graduate.h"
int main()
{
    Person p1("张三",'m',18);
    p1.Show();
    p1.SetInfo("李四",'m',20);
    p1.Show();
    Person p2(p1);
    p2.Show();

    cout<<endl<<"Student:"<<endl;
    Student s1("李华",'m',19,2021,"黑大","软件");
    s1.Show1();
    s1.SetInfo("11",'m',11);
    s1.SetInfo1(2022,"黑大","计算机");
    s1.Show1();
    Student s2(s1);
    s2.Show1();

    cout<<endl<<"Graduate:"<<endl;
    Graduate g1("小明",'m',21,2020,"黑大","软件","android");
    g1.Show2();
    g1.SetInfo2("java");
    g1.Show2();
    Graduate g2(g1);
    g2.Show2();
    
}



版权声明:本文为m0_73991668原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。