C++实验: 继承与派生

C++实验: 继承与派生

1. 实验目的

(1) 了解继承在面向对象程序设计中的重要作用。

(2) 进一步掌握继承与派生的概念。

(3) 掌握通过继承派生出一个新的类的方法。

(4) 了解虚基类的作用与方法。

2. 实验内容

(1) 将程序片段用公用继承的方式补充和改写完整;

(2) 将程序片段用保护继承的方式补充和改写完整;

(3) 比较两种继承方式,考虑二者在什么情况下不能替代;

(4) 使用多重继承方式派生出新类。

3.源代码

#include "pch.h"
#include
using namespace std;
class Student
{ public:
    void get_value()
      {
cin >> num >> name >> sex;
}

void display()
  {
cout << "num:" << num << endl;
cout << "name:"<< name << endl;
cout << "sex:"<< sex << endl;
}

private:
    int num;
    char name[10];
    char sex;
};

class Student1 :public Student
{
public:
    void get_value_1()
    {
         get_value();
         cin>> age >> addr;
    }

    void display_1()
    {
         cout<< "age:" << age << endl;
         cout<< "address:" << addr << endl;
    }
private:
    int age;
    char addr[30];
};

int main()
{
    Student1 stud1;
    stud1.get_value_1();
    stud1.display();
    stud1.display_1();
    return 0;
}
#include "pch.h"
#include
using namespace std;

class Student
{
public:
    void get_value();
    void display();
protected:
   int num;
   char name[10];
   char sex;
};

void Student::get_value()
{
    cin >> num >> name >> sex;
}

void Student::display()
{
    cout<< "num:" << num << endl;
    cout<< "name:" << name << endl;
    cout<< "sex:" << sex << endl;
}

class Student1 :protected Student
{
public:
    void get_value_1();
    void display1();
private:
    int age;
    char addr[30];
};

void Student1::get_value_1()
{
    get_value();
    cin>>age>>addr;
}

void Student1::display1()
{
    cout<< "num:" << num << endl;
    cout<<"name:"<< name << endl;
    cout<< "sex:" << sex << endl;
    cout<<"age:"<< age << endl;
    cout<<"address:" << addr << endl;
}

int main()
{
    Student1 stud1;
    stud1.get_value_1();
    stud1.display1();
    return 0;
}
#include "pch.h"
#include
using namespace std;

class Student
{public:
    void get_value();
    void display();
protected:
    int num;
    char name[10];
    char sex;
};

void Student::get_value()
{
    cin >> num >> name >> sex;
}

void Student::display()

{
    cout<< "num:" << num << endl;
    cout<< "name:" << name << endl;
    cout<< "sex:"<< sex << endl;
}

class Student1:public Student
{public:
    void get_value_1();
    void display1();
private:
    int age;
    char addr[30];
};

void Student1::get_value_1()
{
    get_value();
    cin >> age >> addr;
}

void Student1::display1()
{
    cout<< "num:"<<num<< endl;
    cout<< "name:"<<name<< endl;
    cout<< "sex:"<<sex<< endl;
    cout<< "age:"<<age<< endl;
    cout<< "address:"<<addr<< endl;
}

int main()
{
    Student1 stud1;
    stud1.get_value_1();
    stud1.display1();
    return 0;
}
#include "pch.h"
#include
#include 
using namespace std;

class Teacher
{public:
    Teacher(string nam, int a, char s, string tit, string ad, string t);

    void display();
protected:
    string name;
    int age;
    char sex;
   string title;
    string addr;
    string tel;
};

Teacher::Teacher(string nam,int a,char s, string tit,string ad,string t):name(nam),age(a),sex(s),title(tit),addr(ad),tel(t){}

void Teacher::display()
{
    cout<< "name:" << name << endl;
    cout<< "age:" << age << endl;
    cout<< "sex:" << sex << endl;
    cout<< "title:" << title << endl;
    cout<< "address:" << addr << endl;
    cout<< "tel:" << tel << endl;
}

class Cadre
{public:
    Cadre(string nam, int a, char s, string p, string ad, string t);
    void display();
protected:
    string name;
    int age;
    char sex;
   string post;
   string addr;
   string tel;
};
Cadre::Cadre(string nam, int a,char s,string p,string ad,string t):name(nam),age(a),sex(s),post(p),addr(ad),tel(t){}

void Cadre::display()
{
    cout<< "name:" << name << endl;
    cout<< "age:" << age << endl;
    cout<< "sex:" << sex << endl;
    cout<< "post:" << post<< endl;
    cout<< "address:" << addr << endl;
    cout<< "tel:" << tel << endl;
}

class Person:public Teacher,public Cadre
{public:
    Person(string nam, int a, char s, string tit, string p, string ad, string t, float w);
    void show();
private:
    float wage;
};
Person::Person(string nam,int a,char s,string t,string p,string ad,string tel,float w):Teacher(nam,a,s,t,ad,tel),Cadre(nam, a, s, t, ad, tel),wage(w){}

void Person::show()
{
    Teacher::display();
    cout<< "post:" << Cadre::post << endl;
    cout<< "wage:" << wage << endl;
}

int main()
{
   Person person1("xaiocong", 10, 'M', "prof.", "president", "135 Beijing
Road,Shanghai", "(021)34567891", 1534.5);
    person1.show();
    return 0;
}

4.运行结果

(1)
C++实验: 继承与派生_第1张图片
(2)

C++实验: 继承与派生_第2张图片
(3)
C++实验: 继承与派生_第3张图片
(4)

C++实验: 继承与派生_第4张图片

5.实验总结

1.公用继承是指在定义一个派生类时将基类的继承方式指定为public的;

2.保护继承是指在定义一个派生类时将基类的继承方式指定为protected的;

3.公用继承和保护继承的执行过程相同的时候,其运行结果也相同;

4.不同的继承方式使派生类的成员具有不同的特性,会对程序的执行产生不同的影响。

你可能感兴趣的:(c++实验,c++)