1、 理解继承的含义,掌握派生类的定义方法和实现;2、 理解公有继承下基类成员对派生类成员和派生类对象的可见性,能正确地访问继承层次中的各种类成员;3、 理解保护成员在继承中的作用,能够在适当的时候选择使用保护成员以便派生类成员可以访问基类的部分非公开的成员;4、 理解虚函数在类的继承层次中的作用,虚函数的引入对程序运行时的影响,能够对使用虚函数的简单程序写出程序结果。
实验内容
1、编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班级和成绩,教师数据有编号、姓名、职称和部门。
2、要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类student和教师类数据操作类teacher的基类。
3、编写一个程序计算出球、圆柱和圆锥的表面积和体积。要求:i. 定义一个基类圆,至少含有一个数据成员半径;ii. 定义基类的派生类球、圆柱、圆锥,都含有求表面积和体积的成员函数和输出函数。iii. 定义主函数,求球、圆柱、圆锥的和体积。
程序/结果
1、#include <iostream>using namespace std;class person{public:即枢潋雳void set_information( ); //公用成员函数void show_information( ); //公用成员函数private: //数据成员为私有int number;char name[30];};class Student: public person {public:void show_information_1( ){ cout<<endl;cout<<"请输入学生信息: "<<endl;cout<<endl;cout<<"请输入班级: "<<endl;cin>>cls;cout<<"请输入成绩: "<<endl;cin>>gra;cout<<endl;cout<<"学生附加信息:"<<endl;cout<<" 班级: "<<cls<<endl; //引用派生类的私有成员,正确cout<<" 成绩: "<<gra<<endl;cout<<endl;} //引用派生类的私有成员,正确private:int cls;int gra;};class Teacher: public person {public:void show_information_2( ){ cout<<endl;cout<<"请输入教师信息: "<<endl;cout<<endl;cout<<"请输入职称: "<<endl;cin>>cls2;cout<<"请输入部门: "<<endl;cin>>gra2;cout<<endl;cout<<"教师附加信息:"<<endl;cout<<" 职称: "<<cls2<<endl; //引用派生类的私有成员,正确cout<<" 部门: "<<gra2<<endl;cout<<endl;} //引用派生类的私有成员,正确private:char cls2[30];char gra2[30];};int main( ){Student t1; Teacher t2; //定义对象t1 t1.set_information( ); //调用对象t1的成员函数set_time,向t1的数据成员输入数据t1.show_information_1(); t2.set_information( ); //调用对象t1的成员函数set_time,向t1的数据成员输入数据t2.show_information_2(); cout<<endl;return 0;}void person::set_information( ) //在类外定义set_time函数{cout<<endl;cout<<"请输入基本信息: "<<endl;cout<<endl;cout<<" "<<"姓名:";cin>>name;cout<<" "<<"号码:";cin>>number;}void person::show_information( ) //在类外定义show_time函数{cout<<endl;cout<<"人员信息: "<<endl;cout<<endl;cout<<"姓名:"<<name<<" "<<"号码: "<<number<<endl;}
2、#include <iostream>using namespace std;class Ctriangle{publ足毂忍珩ic:void set_R( ); //公用成员函数void show_R( ); //公用成员函数private: //数据成员为私有int r;int h;};class Student: private Ctriangle {public:void show_information_1( ){ cout<<endl;cout<<"请输入圆柱底面半径: "<<endl;cout<<endl;cin>>r;cout<<"请输入圆柱的高: "<<endl; cin>>h;cout<<endl;cout<<"底面半径为"<<r<<"高为"<<h<<"的圆柱:"<<endl;cout<<" 面积: "<<2*3.14*r*r+2*3.14*r*h<<endl; //引用派生类的私有成员,正确 cout<<" 体积: "<<3.14*r*r*h<<endl; //引用派生类的私有成员,正确cout<<endl;} //引用派生类的私有成员,正确private:int r;int h;};class Teacher: private Ctriangle {public:void show_information_2( ){ cout<<endl;cout<<"请输入圆锥底面半径: "<<endl;cout<<endl;cin>>r;cout<<"请输入圆锥的高: "<<endl; cin>>h;cout<<endl;cout<<"底面半径为"<<r<<"高为"<<h<<"的圆锥:"<<endl;cout<<" 面积: "<<3.14*r*h+3.14*r*r<<endl; //引用派生类的私有成员,正确 cout<<" 体积: "<<3.14*r*r*h/3<<endl; //引用派生类的私有成员,正确cout<<endl;} //引用派生类的私有成员,正确private:int r;int h;};int main( ){Ctriangle t;t.set_R();t.show_R();Student t1; Teacher t2; //定义对象t1 t1.show_information_1(); t2.show_information_2(); cout<<endl;return 0;}void Ctriangle::set_R( ) //在类外定义set_time函数{cout<<endl;cout<<"请输入球体半径: "<<endl;cout<<endl;cout<<" "<<"球体半径:";cin>>r;}void Ctriangle::show_R( ) //在类外定义show_time函数{cout<<endl;cout<<"半径为"<<r<<"的球体:"<<endl;cout<<endl;cout<<"表面积:"<<4*3.14*r*r<<endl; cout<<"体积:"<<(4/3)*3.14*r*r*r<<endl;}