继承与派生参考代码.docx
《继承与派生参考代码.docx》由会员分享,可在线阅读,更多相关《继承与派生参考代码.docx(26页珍藏版)》请在冰豆网上搜索。
![继承与派生参考代码.docx](https://file1.bdocx.com/fileroot1/2022-10/10/ef94f12b-d47f-4dde-a8d6-7fe81de18620/ef94f12b-d47f-4dde-a8d6-7fe81de186201.gif)
继承与派生参考代码
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>