派生类与继承实验报告.docx

上传人:b****7 文档编号:10885189 上传时间:2023-02-23 格式:DOCX 页数:26 大小:189.72KB
下载 相关 举报
派生类与继承实验报告.docx_第1页
第1页 / 共26页
派生类与继承实验报告.docx_第2页
第2页 / 共26页
派生类与继承实验报告.docx_第3页
第3页 / 共26页
派生类与继承实验报告.docx_第4页
第4页 / 共26页
派生类与继承实验报告.docx_第5页
第5页 / 共26页
点击查看更多>>
下载资源
资源描述

派生类与继承实验报告.docx

《派生类与继承实验报告.docx》由会员分享,可在线阅读,更多相关《派生类与继承实验报告.docx(26页珍藏版)》请在冰豆网上搜索。

派生类与继承实验报告.docx

派生类与继承实验报告

洛阳理工学院实验报告

系别

计算机与信息工程系

班级

B11050

学号

B11050

姓名

课程名称

C++面向对象与程序设计

实验日期

2013.10.8

实验名称

派生类与继承

成绩

实验目的:

1、掌握派生类的声明方法和派生类构造函数的定义方法;

2、掌握不同继承方式下,基类成员在派生类中的访问属性;

3、掌握在继承方式下,构造函数与析构函数的执行顺序与构造规则;

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

实验条件:

装有MicrosoftVisualC++6.0软件的计算机

实验内容:

1.输入下列程序。

//test4_1.cpp

#include

usingnamespacestd;

classBase{

public:

voidsetx(inti)

{x=i;}

Intgetx()

{returnx;}

public:

intx;

};

classDerived:

publicBase{

public:

voidsety(inti)

{y=i;}

intgety()

{returny;}

voidshow()

{cout<<”Base:

:

x=”<

}

public:

inty;

};

intmain()

{Derivedbb;

bb,setx(16);

bb.sety(25);

bb.show();

cout<<”Base:

:

x=”<

cout<<”Derived:

:

y=”<

cout<<”Base:

:

x=”<

cout<<”Derived:

:

y=”<

return0;

}

 

写出程序的运行结果。

 

(2)按以下要求,对程序进行修改后再调试,指出调试中出错的原因。

将基类Base中数据成员x的访问权限改为private时,会出现哪些错误?

为什么?

将基类Base中数据成员x的访问权限改为protected时,会出现哪些错误?

为什么?

在源程序的基础上,将派生类Derived的继承方式改为private时,会出现哪些错误?

为什么?

在源程序的基础上,将派生类Derived的继承方式改为protected时,会出现哪些错误?

为什么?

 

解答如下;

将基类Base中数据成员x的访问权限改为private时,会出现哪些错误?

为什么?

因为基类Base中数据成员x的访问权限改为private时,基类的私有成员在派生类中并没有成为派生类的私有成员,使得再通过派生类的对象调用x时,不能被系统所识别,出现了错误。

将基类Base中数据成员x的访问权限改为protected时,会出现哪些错误?

为什么?

因为基类Base中数据成员x的访问权限改为protected时,公用基类的保护成员在公用派生类中的访问属性仍然是保护,在公用派生类外就不能通过公用派生类的对象访问该成员,所以出现了错误。

 

在源程序的基础上,将派生类Derived的继承方式改为private时,会出现哪些错误?

为什么?

将派生类Derived的继承方式改为private时,私有基类中的公用成员在私有派生类中的访问属性为私有,在类外不能通过对象调用私有成员,只能通过成员函数调用.

在源程序的基础上,将派生类Derived的继承方式改为protected时,会出现哪些错误?

为什么?

因为私有继承时基类中的公有函数到派生类中一是私有成员,不能通过对象访问,只能通过成员函数来访问。

2.编写一个学生和教师的数据输入和显示程序。

学生数据有编号、姓名、性别、年龄、系别和成绩,教师数据有编号、姓名、性别、年龄、职称和部门。

要求将编号、姓名、性别、年龄的输入和显示设计成一个类Person,并作为学生类Student和教师类Teacher的基类。

供参考的类结构如下:

classPerson{

...

};

classStudent:

publicPerson{

...

};

classTeacher:

publicPerson{

...

};

 

程序代码:

#include

#include

usingnamespacestd;

classPerson

{public:

Person(intn,stringnam,chars,inta)

{num=n;

name=nam;

sex=s;

age=a;

}

~Person(){}

protected:

intnum;

stringname;

charsex;

intage;

};

classStudent:

publicPerson

{public:

Student(intn,stringnam,chars,inta,stringap,intsc):

Person(n,nam,s,a)

{apart=ap;

score=sc;

}

voidshow1()

{cout<<"num:

"<

cout<<"name:

"<

cout<<"sex:

"<

cout<<"age:

"<

cout<<"apart:

"<

cout<<"score:

"<

}

~Student(){}

private:

stringapart;

intscore;

};

classTeacher:

publicPerson

{

public:

Teacher(intn,stringnam,chars,inta,stringz,stringt):

Person(n,nam,s,a)

{zhi=z

title=t;

}

voidshow2()

{cout<<"num:

"<

cout<<"name:

"<

cout<<"sex:

"<

cout<<"age:

"<

cout<<"zhi:

"<

cout<<"title:

"<

}

~Teacher(){}

private:

stringzhi;

stringtitle;

};

intmain()

{StudentS(1001,"yang-heng",'f',21,"computer",98);

TeacherT(2009,"li-sheng",'f',35,"manager","education");

S.show1();

T.show2();

return0;

}

程序运行结果:

(3)调试程序时会出现以下错误:

调用函数是应注意其访问权限,基类函数的私有成员只能被其成员函数访问,不能被派生类的对象和成员函数所访问,还要注意调用派生类的构造函数时的参数列表表达形式。

(4)结果分析:

注意构造函数和派生类调用基类的构造函数赋值,在后面对新增的参数初始化,通过对象赋值后再对其输出。

3.按要求阅读、编辑、编译、调试和运行以下程序。

(1)阅读、编辑、编译、调试和运行以下程序,并写出程序的运行结果。

//test4_3_1.cpp

#include

#include

usingnamespacestd;

classMyArray{

public:

MyArray(intleng);

~MyArray();

voidInput();

voidDisplay(string);

protected:

int*alist;

intlength;

};

MyArray:

:

MyArray(intleng)

{if(leng<=0)

{cout<<"errorlength";

exit

(1);

}

alist=newint[leng];

length=leng;

if(alist==NULL)

{cout<<"assignfailure";

exit

(1);

}

cout<<"MyArray类对象已创建。

"<

}

MyArray:

:

~MyArray()

{delete[]alist;

cout<<"MyArray类对象被撤销。

"<

}

voidMyArray:

:

Display(stringstr)

{inti;

int*p=alist;

cout<

";

for(i=0;i

cout<<*p<<"";

cout<

}

voidMyArray:

:

Input()

{cout<<"请键盘输入"<

";

inti;

int*p=alist;

for(i=0;i

cin>>*p;

}

intmain()

{MyArraya(5);

a.Input();

a.Display("显示已输入的");

return0;

}

 

(2)声明一个类SortArray继承类MyArray,在该类中定义一个函数,具有将输入的整数从小到大进行排序的功能。

程序代码:

#include

#include

usingnamespacestd;

classMyArray

{

public:

MyArray(intleng);

~MyArray();

voidInput();

voidDisplay(string);

protected:

int*alist;

intlength;

};

classSortArray:

publicMyArray{

public:

voidSort();

SortArray(intleng):

MyArray(leng)

{cout<<"SortArray类对象已创建。

"<

}

virtual~SortArray();

};

SortArray:

:

~SortArray()

{cout<<"SortArray类对象被撤销。

"<

}

MyArray:

:

MyArray(intleng)

{if(leng<=0)

{cout<<"errorlength";

exit

(1);

}

alist=newint[leng];

length=leng;

if(alist==NULL)

{cout<<"assignfailure";

exit

(1);

}

cout<<"MyArray类对象已创建。

"<

}

MyArray:

:

~MyArray()

{

delete[]alist;

cout<<"MyArray类对象被撤销。

"<

}

voidMyArray:

:

Display(stringstr)

{inti;

int*p=alist;

cout<

";

for(i=0;i

cout<<*p<<"";

cout<

}

voidMyArray:

:

Input()

{cout<<"请键盘输入"<

";

inti;

int*p=alist;

for(i=0;i

cin>>*p;

}

voidSortArray:

:

Sort()

{inttemp,i,j;

for(i=0;i

for(j=0;j

if(alist[j]>alist[j+1])

{temp=alist[j];

alist[j]=alist[j+1];

alist[j+1]=temp;

}

}

intmain()

{SortArrays(5);

s.Input();

s.Display("显示排序以前的");

s.Sort();

s.Display("显示排序以后的");

return0;

}

 

程序运行结果:

(2).声明一个类ReArray继承类MyArray,在该类中定义一个函数,具有将输入的整数进行倒置的功能。

程序代码:

#include

#include

usingnamespacestd;

classMyArray{

public:

MyArray(intleng);

~MyArray();

voidInput();

voidDisplay(string);

protected:

int*alist;

intlength;

};

classReArray:

publicMyArray{

public:

voidreverse();

~ReArray()

{cout<<"ReArray类对象被撤销。

"<

};

ReArray(intleng):

MyArray(leng)

{cout<<"ReArray类对象已创建。

"<

}

};

MyArray:

:

MyArray(intleng)

{if(leng<=0)

{cout<<"errorlength";

exit

(1);

}

alist=newint[leng];

length=leng;

if(alist==NULL)

{cout<<"assignfailure";

exit

(1);

}

cout<<"MyArray类对象已创建。

"<

}

MyArray:

:

~MyArray()

{delete[]alist;

cout<<"MyArray类对象被撤销。

"<

}

voidMyArray:

:

Display(stringstr)

{inti;

int*p=alist;

cout<

";

for(i=0;i

cout<<*p<<"";

cout<

}

voidMyArray:

:

Input()

{cout<<"请键盘输入"<

";

inti;

int*p=alist;

for(i=0;i

cin>>*p;

}

voidReArray:

:

reverse()

{inti,temp;

for(i=0;i

{temp=alist[i];

alist[i]=alist[4-i];

alist[4-i]=temp;

}

}

intmain()

{ReArraya(5);

a.Input();

a.Display("显示已输入的");

a.reverse();

a.Display("显示已输出的");

return0;

}

 

程序运行结果:

(3)声明一个类AverArray继承类MyArray,在该类中定义一个函数,具有求输入的整数平均值的功能。

程序代码:

#include

#include

usingnamespacestd;

classMyArray

{

public:

MyArray(intleng);

~MyArray();

voidInput();

voidDisplay(string);

protected:

int*alist;

intlength;

};

MyArray:

:

MyArray(intleng)

{if(leng<=0)

{cout<<"errorlength";

exit

(1);

}

alist=newint[leng];

length=leng;

if(alist==NULL)

{cout<<"assignfailure";

exit

(1);

}

cout<<"MyArray类对象已创建。

"<

}

MyArray:

:

~MyArray()

{

delete[]alist;

cout<<"MyArray类对象被撤销。

"<

}

voidMyArray:

:

Display(stringstr)

{inti;

int*p=alist;

cout<

";

for(i=0;i

cout<<*p<<"";

cout<

}

voidMyArray:

:

Input()

{cout<<"请键盘输入"<

";

inti;

int*p=alist;

for(i=0;i

cin>>*p;

}

classAverArray:

publicMyArray{

public:

AverArray(intleng):

MyArray(leng)

{cout<<"MyArray类对象已创建。

"<

~AverArray()

{cout<<"AverArray类对象被撤销。

"<

doubleAver();

};

doubleAverArray:

:

Aver()

{doublesum=0,average;

inti;

for(i=0;i

sum+=alist[i];

average=sum/length;

cout<<"平均数是:

"<

return0;

}

intmain()

{AverArraya(5);

a.Input();

a.Display("显示已输入的");

a.Aver();

return0;

}

程序运行结果:

(2)程序代码:

#include

#include

usingnamespacestd;

classMyArray

{

public:

MyArray(intleng);

~MyArray();

voidInput();

voidDisplay(string);

protected:

int*alist;

intlength;

};

MyArray:

:

MyArray(intleng)

{if(leng<=0)

{cout<<"errorlength";

exit

(1);

}

alist=newint[leng];

length=leng;

if(alist==NULL)

{cout<<"assignfailure";

exit

(1);

}

cout<<"MyArray类对象已创建。

"<

}

MyArray:

:

~MyArray()

{

delete[]alist;

cout<<"MyArray类对象被撤销。

"<

}

voidMyArray:

:

Display(stringstr)

{inti;

int*p=alist;

cout<

";

for(i=0;i

cout<<*p<<"";

cout<

}

voidMyArray:

:

Input()

{cout<<"请键盘输入"<

";

inti;

int*p=alist;

for(i=0;i

cin>>*p;

}

classSortArray:

virtualpublicMyArray

{

public:

voidSort();

SortArray(intleng):

MyArray(leng)

{cout<<"SortArray类对象已创建。

"<

}

virtual~SortArray();

};

SortArray:

:

~SortArray()

{cout<<"SortArray类对象被撤销。

"<

}

voidSortArray:

:

Sort()

{inttemp,i,j;

for(i=0;i

for(j=0;j

if(alist[j]>alist[j+1])

{temp=alist[j];

alist[j]=alist[j+1];

alist[j+1]=temp;

}

}

 

classReArray:

virtualpublicMyArray

{

public:

voidreverse();

ReArray(intleng);

virtual~ReArray();

};

ReArray:

:

ReArray(intleng):

MyArray(leng)

{

if(leng<=0)

{

cout<<"errorlength";

exit

(1);

}

}

voidReArray:

:

reverse()

{inti,temp;

for(i=0;i

{

temp=alist[i];

alist[i]=alist[4-i];

alist[4-i]=temp;

}

}

ReArray:

:

~ReArray()

{cout<<"ReArray类对象被撤销。

"<

}

classAverArray:

virtualpublicMyArray{

public:

AverArray(intleng):

MyArray(leng)

{cout<<"AyArray类对象已创建。

"<

~AverArray()

{cout<<"AverArray类对象被撤销。

"<

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 高中教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1