C++实验: 继承与派生
1. 实验目的
(1) 了解继承在面向对象程序设计中的重要作用。
(2) 进一步掌握继承与派生的概念。
(3) 掌握通过继承派生出一个新的类的方法。
(4) 了解虚基类的作用与方法。
2. 实验内容
(1) 将程序片段用公用继承的方式补充和改写完整;
(2) 将程序片段用保护继承的方式补充和改写完整;
(3) 比较两种继承方式,考虑二者在什么情况下不能替代;
(4) 使用多重继承方式派生出新类。
3.源代码
#include "pch.h"
#include<iostream>
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<iostream>
using namespace std;
class Student
{
public:
void get_value();
void display();
protected