继承与派生练习题
在刚开始学习c++的时候刷了很多基础题,这些基础题比较适合初学C++的码友,所以在学完就立即进行了整理,一是为了让初学C++的码友有所参考,二也是为了复习一下所学过知识。
但因为当时在整理时,时间有点紧促,可能会出现一些小错误,于是利用五一假期对之前的文章进行检查,修改了一些小错误,可能有些错误我还没有发现,欢迎码友们对其指正。
以下八道题主要考查了类与对象中继承与派生的知识,适合初学继承与派生的码友进行练习。
继承与派生 01
请以点类Point为基类派生出一个圆类Circle。圆类Circle的数据成员为r(私有属性,存储圆的半径,圆心的点坐标通过继承点类Point加以实现),成员函数有构造函数Circle、计算圆的面积函数Area、计算圆的周长函数Perimeter和输出函数Display,其中构造函数实现基类和圆类的数据成员的初始化,Display函数实现圆心坐标(利用基类Point的Display实现)、圆的半径、圆的面积(利用Area函数实现)和圆的周长(利用Perimeter函数实现)的输出。请编写圆类的定义及成员函数实现,并在主函数中定义圆类对象,验证各个函数的正确性。
说明:圆周率PI的取值为3.14
//已知Point类的定义及main代码如下:(不允许改动)
class Point
{
public:
Point(double xx,double yy); //constructor
void Display(); //display point
private:
double x,y; //平面的点坐标x,y
};
int main()
{
double x,y,r;
cin>>x>>y>>r; //圆心的点坐标及圆的半径
Circle C(x,y,r);
C.Display(); //输出圆心点坐标,圆的半径,圆的面积,圆的周长
return 0;
}
Sample Input
1.5 2.6 1.8
Sample Output
Center:Point(1.5,2.6)
Radius:1.8
Area:10.1736
Perimeter:11.304
#include<iostream>
using namespace std;
const double PI=3.14;
//已知Point类的定义及main代码如下:(不允许改动)
class Point
{
public:
Point(double xx,double yy); //constructor
void Display(); //display point
private:
double x,y; //平面的点坐标x,y
};
Point::Point(double xx,double yy) //constructor
{
x=xx;
y=yy;
}
void Point::Display()
{
cout<<"Point("<<x<<","<<y<<")"<<endl;
}
class Circle :public Point
{
public:
Circle(double xx,double yy,double rr);
double Area();
double Perimeter();
void Display();
private:
double r;
};
Circle::Circle(double xx,double yy,double rr) :Point(xx,yy)
{
r=rr;
}
double Circle::Area()
{
return PI*r*r;
}
double Circle::Perimeter()
{
return 2*PI*r;
}
void Circle::Display()
{
cout<<"Center:";
Point::Display();
cout<<"Radius:"<<r<<endl;
cout<<"Area:"<<Area()<<endl;
cout<<"Perimeter:"<<Perimeter()<<endl;
}
int main()
{
double x,y,r;
cin>>x>>y>>r; //圆心的点坐标及圆的半径
Circle C(x,y,r);
C.Display(); //输出圆心点坐标,圆的半径,圆的面积,圆的周长
return 0;
}
继承与派生 02
Person类派生大学生CollegeStu类。设计一个Person类,其属性包括姓名name和身份证号id,其中name为指针类型,id为整型,编写成员函数:构造函数Person、Display函数(显示数据成员信息)和析构函数;由Person类派生出大学生类CollegeStu,其属性有专业major(指针类型),C++程序设计课程成绩score(double型),编写构造函数(实现数据初始化)、输出函数Display(包括name,id,major,score)。
//main的代码如下:(不允许改动)
int main()
{
char name[81],major[81];
int id;
double score;
cin>>name>>id>>major>>score;
CollegeStu cs(name,id,major,score);
cs.Display();
return 0;
}
Sample Input
Zhangsan 2 Computer 89.5
Sample Output
Name:Zhangsan
ID:2
Major:Computer
C++ Score:89.5
#include<iostream>
#include<cstring>
using namespace std;
class Person
{
public:
Person(char*name,int id);
void Display();
~Person();
private:
char *m_name;
int m_id;
};
Person::Person(char*name,int id)
{
int len=strlen(name);
m_name=new char[len+1];
strcpy(m_name,name);
m_id=id;
}
void Person::Display()
{
cout<<"Name:"<<m_name<<endl;
cout<<"ID:"<<m_id<<endl;
}
Person::~Person()
{
delete []m_name;
}
class CollegeStu :public Person
{
public:
CollegeStu(char *name,int id,char *major,double score);
void Display();
~CollegeStu();
private:
char *m_major;
double m_score;
};
CollegeStu::CollegeStu(char *name,int id,char *major,double score):Person(name,id)
{
int len=strlen(major);
m_major=new char[len+1];
strcpy(m_major,major);
m_score=score;
}
void CollegeStu::Display()
{
Person::Display();
cout<<"Major:"<<m_major<<endl;
cout<<"C++ Score:"<<m_score<<endl;
}
CollegeStu::~CollegeStu()
{
delete []m_major;
}
//main的代码如下:(不允许改动)
int main()
{
char name[81],major

本文提供8道C++继承与派生练习题,涵盖类派生、构造函数、析构函数、数据成员初始化、输出函数等内容,适合巩固基本概念并提升编程能力。通过实例演示如何创建圆类Circle、大学生类CollegeStu等,以及如何处理字符串类型属性和成员函数的实现。
最低0.47元/天 解锁文章
1118

被折叠的 条评论
为什么被折叠?



