当前位置:文档之家› 实验四 继承与派生讲解学习

实验四 继承与派生讲解学习

实验四继承与派生实验四派生类与继承【实验类型】验证性实验【实验课时】2学时【实验目的】(1)理解类的继承的概念,能够定义和使用类的继承关系。

(2)掌握派生类的声明与定义方法。

(3)熟悉公有派生和私有派生的访问特性。

(4)学习虚基类在解决二义性问题中的作用。

【实验环境】硬件:计算机软件:Microsoft Visual C++ 6.0【实验内容】1、按要求阅读、编写、调试和运行以下程序。

(1)实验内容①定义一个基类MyArray,基类中可以存放一组整数。

class MyArray{public:MyArray(int leng);~MyArray();void Input();void Display();protected:long int *alist; // 指向动态申请的一组空间int length;}; // 整数的个数基类中有构造函数、析构函数、输入数据和输出数据的函数。

②定义一个类SortArray继承自MyArray ,在该类中定义函数实现排序功能。

③定义一个类ReArray继承自MyArray ,在该类中定义函数实现逆转功能。

④定义一个类AverArray继承自MyArray ,在该类中定义函数Aver求解整数的平均值。

⑤定义NewArray类,同时继承了SortArray, ReArray和AverArray,使得NewArray类的对象同时具有排序、逆转、和求平均值的功能。

在继承的过程中声明为虚基类,体会虚基类在解决二义性问题中的作用。

(2)实验程序 (参考)程序如下:#include "iostream.h"#include "process.h"class MyArray{public:MyArray(int leng);~MyArray();void Input();void Display();protected:long int *alist; // 指向动态申请的一组空间int length; // 整数的个数};MyArray::MyArray(int leng){ length=leng;alist=new long int[length];if(alist==NULL){cout<<"对不起,创建失败。

请重试。

";exit(1);}}MyArray::~MyArray(){delete[] alist;cout<<"数组被清空。

"<<endl;}void MyArray::Display() // 显示数组内容{int i;long int *p=alist;for (i=0;i<length;i++,p++)cout<<" "<<*p;}void MyArray::Input() // 从键盘若干整数{cout<<"请输入:"<<length<<"个整数:";int i;long int *p=alist;for(i=0;i<length;i++,p++)cin>>*p;}class SortArray: virtual public MyArray{private:int len;long int *sp;public:SortArray(int leng):MyArray(leng){len=leng;Sort();};void Sort(){sp=new long int[len];long int q;sp=alist;for(int i=0;i<len;i++){for(int j=0;j<len-1;j++){if(*(sp+j)>*(sp+j+1)){q=*(sp+j);*(sp+j)=*(sp+j+1);*(sp+j+1)=q;}}}}};class ReArray: virtual public MyArray {// 这里是虚基类,public:void Reverse(){rp=new long int[len];long int q;rp=alist;for(int i=0;i<len/2;i++){q=*(rp+i);*(rp+i)=*(rp+len-i-1);*(rp+len-i-1)=q;}}ReArray(int leng):MyArray(leng){len=leng;Reverse();}private:int len;long int *rp;};class AverArray:virtual public MyArray{ // 这里是虚基类,public:double Aver(){ap=new long int[len];double q=0;ap=alist;for(int i=0;i<len;i++){q=q+*ap;ap++;}q=q/len;return q;}AverArray(int leng):MyArray(leng){len=leng;}private:int len;long int *ap;};class NewArray:public ReArray,public SortArray,public AverArray { public:NewArray(int leng);~NewArray();};NewArray::NewArray(intleng):MyArray(leng),SortArray(leng),ReArray (leng),AverArray(leng){cout<<"\n新数组正在创建。

\n";}NewArray::~NewArray(){cout<<"\n新数组已被清空。

\n";}void main(){char b;int leng;do{cout<<"请输入数组长度:"<<endl;cin>>leng;while(leng<=0){cout<<"数组长度必须为大于零的整数,请重新输入数组长度:\n";exit(1);cin>>leng;}cout<<"\n开始:\n";NewArray n(leng);n.Input();cout<<"\n您输入的数组为:"<<endl;n.Display(); // 显示数组n.Reverse(); //显示逆序cout<<"\n倒序数组为:"<<endl;n.Display(); // 显示逆转以前的情况cout<<"\n平均值是:"<<n.Aver()<<endl;//求平均值n.Sort(); //排序cout<<"\n排序后(从小到大)数组为:"<<endl;n.Display(); // 显示排序以后的情况cout<<"\n[A]继续 [Q]退出"<<endl;cin>>b;}while(b=='A'||b=='a');}执行结果为:2、编写一个学生和教师数据输入和显示程序。

(1)实验内容编写学生和教师数据输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。

要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类student和教师数据操作类teacher 的基类。

(2)实验程序(参考)#include<iostream.h>class person{protected:int m;char A[20];char *name;public:void input(){cout<<"编号:";cin>>m;cout<<"姓名:";cin>>A;name=&A[0];}void display(){cout<<"编号:"<<m<<endl;cout<<"姓名:"<<name<<endl;}};class student:public person{protected:int classnum, mark;public:void input1(){cout<<"输入一个学生数据:"<<endl;input();cout<<"班号:";cin>>classnum;cout<<"成绩:";cin>>mark;}void display1(){cout<<"显示一个学生的数据:"<<endl;display();cout<<"班号:"<<classnum<<endl;cout<<"成绩:"<<mark<<endl;}};class teacher: public person{protected:char zhicheng[20],bumen[20];char *p;public:void input2(){cout<<"显示一个老师的数据:"<<endl;input();cout<<"职称:";cin>>zhicheng;cout<<"部门:";cin>>bumen; }void display2(){cout<<"显示一个老师的数据:"<<endl;display();p=&zhicheng[0];cout<<"职称:"<<p<<endl;p=&bumen[0];cout<<"部门:"<<p<<endl;}};void main(){student S;teacher T;S.input1();T.input2();S.display1();T.display2();}【实验提示】继承是面向对象程序设计的一个重要特性,它允许在已有类的基础上创建新的类,新类可以从一个或多个既有类中继承函数和数据,而且可以重新定义或加进新的数据和函数,从而形成类的层次或等级。

相关主题