继承与派生参考代码讲解.docx

上传人:b****7 文档编号:9478864 上传时间:2023-02-04 格式:DOCX 页数:31 大小:19.16KB
下载 相关 举报
继承与派生参考代码讲解.docx_第1页
第1页 / 共31页
继承与派生参考代码讲解.docx_第2页
第2页 / 共31页
继承与派生参考代码讲解.docx_第3页
第3页 / 共31页
继承与派生参考代码讲解.docx_第4页
第4页 / 共31页
继承与派生参考代码讲解.docx_第5页
第5页 / 共31页
点击查看更多>>
下载资源
资源描述

继承与派生参考代码讲解.docx

《继承与派生参考代码讲解.docx》由会员分享,可在线阅读,更多相关《继承与派生参考代码讲解.docx(31页珍藏版)》请在冰豆网上搜索。

继承与派生参考代码讲解.docx

继承与派生参考代码讲解

1197:

继承与派生1

Description

请以点类Point为基类派生出一个圆类Circle。

圆类Circle的数据成员为r(私有属性,存储圆的半径,圆心的点坐标通过继承点类Point加以实现),成员函数有构造函数Circle、计算圆的面积函数Area、计算圆的周长函数Perimeter和输出函数Display,其中构造函数实现基类和圆类的数据成员的初始化,Display函数实现圆心坐标(利用基类Point的Display实现)、圆的半径、圆的面积(利用Area函数实现)和圆的周长(利用Perimeter函数实现)的输出。

请编写圆类的定义及成员函数实现,并在主函数中定义圆类对象,验证各个函数的正确性。

说明:

圆周率PI的取值为3.14

已知Point类的定义及main代码如下:

(不允许改动)

classPoint

{

public:

Point(doublexx,doubleyy);//constructor

voidDisplay();//displaypoint

private:

doublex,y;//平面的点坐标x,y

};

intmain()

{

doublex,y,r;

cin>>x>>y>>r;//圆心的点坐标及圆的半径

CircleC(x,y,r);

C.Display();//输出圆心点坐标,圆的半径,圆的面积,圆的周长

return0;

}

Input

Output

SampleInput

1.52.61.8

SampleOutput

Center:

Point(1.5,2.6)

Radius:

1.8

Area:

10.1736

Perimeter:

11.304

**************************************************************************

#include

usingnamespacestd;

classPoint

{

public:

Point(doublexx,doubleyy)//constructor

{

x=xx;

y=yy;

}

voidDisplay()//displaypoint

{

cout<<"Center:

Point("<

}

private:

doublex,y;//平面的点坐标x,y

};

classCircle:

publicPoint

{

private:

doubler;

public:

Circle(doublexx,doubleyy,doublerr):

Point(xx,yy)

{

r=rr;

}

doubleArea()

{

return3.14*r*r;

}

doublePerimeter()

{

return2*3.14*r;

}

voidDisplay()

{

Point:

:

Display();

cout<<"Radius:

"<

cout<<"Area:

"<

cout<<"Perimeter:

"<

}

};

intmain()

{

doublex,y,r;

cin>>x>>y>>r;//圆心的点坐标及圆的半径

CircleC(x,y,r);

C.Display();//输出圆心点坐标,圆的半径,圆的面积,圆的周长

return0;

}

1217:

继承与派生2

Description

Person类派生大学生CollegeStu类

(1)。

设计一个Person类,其属性包括姓名name和身份证号id,其中name为指针类型,id为整型,编写成员函数:

构造函数Person、Display函数(显示数据成员信息)和析构函数;由Person类派生出大学生类CollegeStu,其属性有专业subject(指针类型),C++程序设计课程成绩score(double型),编写构造函数(实现数据初始化)、输出函数Display(包括name,id,subject,score)。

main的代码如下:

(不允许改动)

intmain()

{

charname[81],subject[81];

intid;

doublescore;

cin>>name>>id>>subject>>score;

CollegeStucs(name,id,subject,score);

cs.Display();

return0;

}

Input

Output

SampleInput

Zhangsan2Computer89.5

SampleOutput

Name:

Zhangsan

ID:

2

Subject:

Computer

C++Score:

89.5

**************************************************************************

#include

#include

usingnamespacestd;

classPerson

{

private:

char*name;

intid;

public:

Person()

{

name=NULL;

id=0;

}

Person(char*name1,intid1)

{

name=newchar[strlen(name1)+1];

strcpy(name,name1);

id=id1;

}

~Person()

{

delete[]name;

}

voidDisplay()

{

cout<<"Name:

"<

cout<<"ID:

"<

}

};

classCollegestu:

publicPerson

{

private:

char*subject;

doublescore;

public:

Collegestu()

{

subject=NULL;

score=0;

}

Collegestu(char*name1,intid1,char*subject1,doublescore1):

Person(name1,id1)

{

subject=newchar[strlen(subject1)+1];

strcpy(subject,subject1);

score=score1;

}

~Collegestu()

{

delete[]subject;

}

voidDisplay()

{

Person:

:

Display();

cout<<"Subject:

"<

cout<<"C++Score:

"<

}

};

intmain()

{

charname[81],subject[81];

intid;

doublescore;

cin>>name>>id>>subject>>score;

Collegestucs(name,id,subject,score);

cs.Display();

return0;

}

1218:

继承与派生3

Description

Person类派生大学生CollegeStu类

(2)。

设计一个Person类,其属性包括姓名name和身份证号id,其中name为指针类型,id为整型,编写成员函数:

构造函数Person、Display函数(显示数据成员信息)和析构函数;由Person类派生出大学生类CollegeStu,其属性有专业subject(指针类型),C++程序设计课程成绩score(double型),编写构造函数(实现数据初始化)、输出函数Display(只输出subject,score)。

main的代码如下:

(不允许改动)

intmain()

{

charname[81],subject[81];

intid;

doublescore;

cin>>name>>id>>subject>>score;//输入学生的姓名、id号、专业、成绩

CollegeStucs(name,id,subject,score);

cs.Person:

:

Display();//输出姓名,id

cs.Display();//输出专业、成绩

return0;

}

Input

Output

SampleInput

Lixu5Software87.5

SampleOutput

Name:

Lixu

ID:

5

Subject:

Software

C++Score:

87.5

**************************************************************************

#include

#include

usingnamespacestd;

classPerson

{

private:

char*name;

intid;

public:

Person()

{

name=NULL;

id=0;

}

Person(char*name1,intid1)

{

name=newchar[strlen(name1)+1];

strcpy(name,name1);

id=id1;

}

~Person()

{

delete[]name;

}

voidDisplay()

{

cout<<"Name:

"<

cout<<"ID:

"<

}

};

classCollegeStu:

publicPerson

{

private:

char*subject;

doublescore;

public:

CollegeStu()

{

subject=NULL;

score=0;

}

CollegeStu(char*name1,intid1,char*subject1,doublescore1):

Person(name1,id1)

{

subject=newchar[strlen(subject1)+1];

strcpy(subject,subject1);

score=score1;

}

~CollegeStu()

{

delete[]subject;

}

voidDisplay()

{

cout<<"Subject:

"<

cout<<"C++Score:

"<

}

};

intmain()

{

charname[81],subject[81];

intid;

doublescore;

cin>>name>>id>>subject>>score;//输入学生的姓名、id号、专业、成绩

CollegeStucs(name,id,subject,score);

cs.Person:

:

Display();//输出姓名,id

cs.Display();//输出专业、成绩

return0;

}

1219:

继承与派生4

Description

已知Base为基类,派生出Derived类,两个类的定义及main的代码如下(不允许改动),请完成Base类和Derived类的构造函数和析构函数,能够根据输入获取相应的输出。

classBase

{

private:

intb;

public:

Base(int);

~Base();

};

classDerived:

publicBase

{

private:

intd;

public:

Derived(int,int);

~Derived();

};

intmain()

{

inta,b;

cin>>a>>b;

Deriveddr(a,b);

return0;

}

Input

Output

SampleInput

13

SampleOutput

Base1sayshello

Derived3sayshi

Derived3saysbye

Base1saysgoodbye

**************************************************************************

#include

usingnamespacestd;

classBase

{

private:

intb;

public:

Base(intc)

{

b=c;

cout<<"Base"<

}

~Base()

{

cout<<"Base"<

}

};

classDerived:

publicBase

{

private:

intd;

public:

Derived(intc,intb):

Base(c)

{

d=b;

cout<<"Derived"<

}

~Derived()

{

cout<<"Derived"<

}

};

intmain()

{

inta,b;

cin>>a>>b;

Deriveddr(a,b);

return0;

}

1220:

继承与派生5

Description

由Array类派生出有序数组SortArray类,SortArray类中实现有序数组的插入。

已知Array类的定义如下(不允许增加成员函数):

classArray

{

public:

Array();//构造函数,初始化为空数组(length置为0)

intLength();//获取数组的实际长度

doubleGet(intpos);//获取data中下标为pos的元素的值

voidInsert(intpos,doublex);//在下标pos处插入x

voidDisplay();//输出线性表

private:

doubledata[MaxSize];//存储元素(MaxSize为常量)

intlength;//数组的实际长度

};

SortArray类定义如下(不允许增加成员函数):

classSortArray:

privateArray

{

public:

SortArray();

intLength();//获取数组的实际长度

doubleGet(intpos);//获取data中下标为pos的元素的值

voidDisplay();//输出线性表

voidInsert(doublex);//递增有序数组中插入x,使序列仍有序

};

请实现Array类和SortArray类的成员函数,main中输入若干个实数,以0结束,利用SortArray类中的Insert函数将它们插入data中,得到有序序列,再利用Display函数输出有序序列。

代码如下(不允许修改):

intmain()

{

SortArraysa;

doublenum;

while

(1)

{

cin>>num;

if(fabs(num)<=1e-6)break;

try

{

sa.Insert(num);//

}

catch(char*message)

{

cout<

}

}

sa.Display();

return0;

}

Input

Output

SampleInput

2.56.78.32.86.536.827.330

SampleOutput

Thelength:

7

Theelements:

2.52.86.536.76.827.338.3

**************************************************************************

#include

#include

usingnamespacestd;

constintMaxSize=100;//顺序表的最大长度

classArray

{

public:

Array();//构造函数,初始化为空数组(length置为0)

intLength();//获取顺序表实际长度

doubleGet(intpos);//获取下标为pos的元素的值

voidInsert(intpos,doublex);//在下标pos处插入x

voidDisplay();//输出线性表

private:

doubledata[MaxSize];//存储元素

intlength;//数组的实际长度

};

Array:

:

Array()

{length=0;}

intArray:

:

Length()

{returnlength;}

doubleArray:

:

Get(intpos)

{

if(pos<0||pos>length-1)//下标不合法

throw"Illegalposition";

returndata[pos];

}

voidArray:

:

Insert(intpos,doublex)//在下标pos处插入x

{

inti;

if(length>=MaxSize)//表满不能插入

throw"Overflow";

if(pos<0||pos>length)//下标不合法

throw"Illegalposition";

for(i=length-1;i>=pos;i--)//将下标大于等于pos的元素后移

data[i+1]=data[i];

data[pos]=x;//在下标pos处插入元素x

length++;//线性表长度增1

}

voidArray:

:

Display()//输出线性表

{

inti;

cout<<"Thelength:

"<

cout<<"Theelements:

";

for(i=0;i

cout<

cout<

}

 

//classSortArray

classSortArray:

privateArray

{

public:

SortArray();

intLength();

doubleGet(intpos);

voidDisplay();

voidInsert(doublex);//递增有序数组中插入x,使序列仍有序

};

SortArray:

:

SortArray():

Array(){}

intSortArray:

:

Length()

{returnArray:

:

Length();}

doubleSortArray:

:

Get(intpos)

{returnArray:

:

Get(pos);}

voidSortArray:

:

Display()

{Array:

:

Display();}

voidSortArray:

:

Insert(doublex)//insert

{

inti;

if(Length()>=MaxSize)throw"Overflow";

for(i=0;i

if(Get(i)>x)

break;

Array:

:

Insert(i,x);

}

intmain()

{

SortArraysa;

doublenum;

while

(1)

{

cin>>num;

if(fabs(num)<=1e-6)break;

try

{

sa.Insert(num);//

}

catch(char*message)

{

cout<

}

}

sa.Display();

return0;

}

1221:

继承与派生6

Description

已知Array类的定义如下(不允许增加成员函数):

classArray

{

public:

Array(intsize);

//构造函数,初始化数据成员(为data分配内存,MaxSize置为size,length置为0)

intLength();//获取顺序表实际长度

doubleGet(intpos);//获取下标为pos的元素的值

voidInsert(intpos,doublex);//在下标pos处插入x

voidDisplay();//输出线性表

private:

double*data;//存储元素

intMaxSize;

intlength;//数组的实际长度

};

SortArray类定义如下(不允许增加其它成员函数):

classSortArray:

privateArray

{

public:

SortArray(intsize);

intLength();//获取顺序表实际长度

doubleGet(intpos);//获取下标为pos的元素的值

voidDisplay();//输出线性表

voidInsert(doublex);//递增有序数组中插入x,使序列仍有序

};

main中的代码如下(不允许改动):

intmain()

{

intsize;

cin>>size;

SortArraysa(si

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

当前位置:首页 > 自然科学 > 生物学

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

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