继承与派生练习题

继承与派生练习题

在刚开始学习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[81];
       int id;
       double score;
       cin>>name>>id>>major>>score;
       CollegeStu cs(name,id,major,score);
       cs.Display();
       return 0;
}

继承与派生 03

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

 //main的代码如下:(不允许改动)
int main()
{
       char name[81],major[81];
       int id;
       double score;
       cin>>name>>id>>major>>score;  //输入学生的姓名、id号、专业、成绩
       CollegeStu cs(name,id,major,score);
       cs.Person::Display();  //输出姓名,id
       cs.Display();    //输出专业、成绩
       return 0;
}

Sample Input
Lixu 5 Software 87.5
Sample Output
Name:Lixu
ID:5
Major:Software
C++ Score:87.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()
{
    cout<<"Major:"<<m_major<<endl;
    cout<<"C++ Score:"<<m_score<<endl;
}
CollegeStu::~CollegeStu()
{
    delete []m_major;
}
//main的代码如下:(不允许改动)
int main()
{
       char name[81],major[81];
       int id;
       double score;
       cin>>name>>id>>major>>score;  //输入学生的姓名、id号、专业、成绩
       CollegeStu cs(name,id,major,score);
       cs.Person::Display();  //输出姓名,id
       cs.Display();    //输出专业、成绩
       return 0;
}

继承与派生 04

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

class Base
{
private:
       int b;
public:
       Base(int);
       ~Base();
};
class Derived:public Base
{
private:
       int d;
public:
       Derived(int,int);
       ~Derived();
};
int main()
{
       int a,b;
       cin>>a>>b;
       Derived dr(a,b);    
       return 0;
}

Sample Input
1 3
Sample Output
Base 1 says hello
Derived 3 says hi
Derived 3 says bye
Base 1 says goodbye

#include<iostream>
using namespace std;
class Base
{
private:
       int b;
public:
       Base(int a);
       ~Base();
};
Base::Base(int a)
{
    b=a;
    cout<<"Base "<<b<<" says hello"<<endl;
}
Base::~Base()
{
    cout<<"Base "<<b<<" says goodbye"<<endl;
}
class Derived:public Base
{
private:
       int d;
public:
       Derived(int a,int b);
       ~Derived();
};
Derived::Derived(int a,int b):Base(a)
{
    d=b;
    cout<<"Derived "<<d<<" says hi"<<endl;
}
Derived::~Derived()
{
    cout<<"Derived "<<d<<" says bye"<<endl;
}
int main()
{
       int a,b;
       cin>>a>>b;
       Derived dr(a,b);
       return 0;
}

继承与派生 05

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

//已知Array类的定义如下(不允许增加成员函数):
class Array
{
public:
       Array();    //构造函数,初始化为空数组(length置为0)
       int Length();   //获取数组的实际长度
       double Get(int pos);     //获取data中下标为pos的元素的值
       void Insert(int pos, double x); //在下标pos处插入x
       void Display();       //输出线性表        
private:
       double data[MaxSize];  //存储元素(MaxSize为常量)
       int length;              //数组的实际长度
};
//SortArray类定义如下(不允许增加成员函数):
class SortArray:private Array
{
public:
       SortArray();
       int Length();   //获取数组的实际长度
       double Get(int pos);     //获取data中下标为pos的元素的值
       void Display();       //输出线性表        
       void Insert(double x); //递增有序数组中插入x,使序列仍有序
};

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

//代码如下(不允许修改):
int main()
{
       SortArray sa;
       double num;
       while(1)
       {
              cin>>num;
              if(fabs(num)<=1e-6) break;
              try
              {
                     sa.Insert(num); //
              }
              catch(char* message)
              {
                     cout <<message<<endl;    //如失败提示失败信息
              }
       }
       sa.Display();
      
    return 0;
}

Sample Input
2.5 6.7 8.3 2.8 6.53 6.82 7.33 0
Sample Output
The length:7
The elements:2.5 2.8 6.53 6.7 6.82 7.33 8.3

#include<iostream>
#include<cmath>
using namespace std;
const int MaxSize=100;
//已知Array类的定义如下(不允许增加成员函数):
class Array
{
public:
       Array();    //构造函数,初始化为空数组(length置为0)
       int Length();   //获取数组的实际长度
       double Get(int pos);     //获取data中下标为pos的元素的值
       void Insert(int pos, double x); //在下标pos处插入x
       void Display();       //输出线性表
private:
       double data[MaxSize];  //存储元素(MaxSize为常量)
       int length;              //数组的实际长度
};
Array::Array()    //构造函数,初始化为空数组(length置为0)
{
    length=0;
}
int Array::Length()   //获取数组的实际长度
{
    return length;
}
double Array::Get(int pos)     //获取data中下标为pos的元素的值
{
    return data[pos];
}
void Array::Insert(int pos, double x) //在下标pos处插入x
{
    for(int i=length-1;i>=pos;i--)
        data[i+1]=data[i];
    data[pos]=x;
    length++;
}
void Array::Display()      //输出线性表
{
    cout<<"The length:"<<length<<endl;
    cout<<"The elements:";
    for(int i=0;i<length;i++)
        cout<<data[i]<<" ";
    cout<<endl;
}
//SortArray类定义如下(不允许增加成员函数):
class SortArray:private Array
{
public:
       SortArray();
       int Length();   //获取数组的实际长度
       double Get(int pos);     //获取data中下标为pos的元素的值
       void Display();       //输出线性表
       void Insert(double x); //递增有序数组中插入x,使序列仍有序
};
SortArray::SortArray():Array(){}
int SortArray::Length()   //获取数组的实际长度
{
    return Array::Length();
}
double SortArray::Get(int pos)     //获取data中下标为pos的元素的值
{
    return Array::Get(pos);
}
void SortArray::Display()      //输出线性表
{
    Array::Display();
}
void SortArray::Insert(double x)//递增有序数组中插入x,使序列仍有序
{
    int i;
    for(i=0;i<Length();i++)
        if(Get(i)>x)
           break;
    Array::Insert(i,x);
}
//代码如下(不允许修改):
int main()
{
       SortArray sa;
       double num;
       while(1)
       {
              cin>>num;
              if(fabs(num)<=1e-6) break;
              try
              {
                     sa.Insert(num); //
              }
              catch(char* message)
              {
                     cout <<message<<endl;    //如失败提示失败信息
              }
       }
       sa.Display();
      
    return 0;
}

继承与派生 06

//已知Array类的定义如下(不允许增加成员函数):
class Array
{
public:
       Array(int size); 
 //构造函数,初始化数据成员(为data分配内存,MaxSize置为size,length置为0)
       int Length();   //获取顺序表实际长度
       double Get(int pos);     //获取下标为pos的元素的值
       void Insert(int pos, double x); //在下标pos处插入x
       void Display();       //输出线性表  
      ~Array();  //destructor      
private:
       double *data; //存储元素
    int MaxSize;  
       int length;              //数组的实际长度
};
 
//SortArray类定义如下(不允许增加其它成员函数):
class SortArray:private Array
{
public:
       SortArray(int size);
       int Length();   //获取顺序表实际长度
       double Get(int pos);     //获取下标为pos的元素的值
       void Display();       //输出线性表        
       void Insert(double x); //递增有序数组中插入x,使序列仍有序
};
//main中的代码如下(不允许改动):
int main()
{
       int size;
       cin>>size;
       SortArray sa(size);
       double num;
       while(1)
       {
              cin>>num;
              if(fabs(num)<=1e-6) break;
              try
              {
                     sa.Insert(num);
              }
              catch(char* wrong)
              {
                     cout <<wrong<<endl;    //如失败提示失败信息
              }
       }
       sa.Display();  
    return 0;
}

请实现Array类和SortArray类的成员函数。
Sample Input
20 2.5 6.7 8.3 2.8 6.53 6.82 7.33 0
Sample Output
The length:7
The elements:2.5 2.8 6.53 6.7 6.82 7.33 8.3

#include<iostream>
#include<cmath>
using namespace std;
//已知Array类的定义如下(不允许增加成员函数):
class Array
{
public:
       Array(int size); 
 //构造函数,初始化数据成员(为data分配内存,MaxSize置为size,length置为0)
       int Length();   //获取顺序表实际长度
       double Get(int pos);     //获取下标为pos的元素的值
       void Insert(int pos, double x); //在下标pos处插入x
       void Display();       //输出线性表  
      ~Array();  //destructor      
private:
       double *data; //存储元素
    int MaxSize;  
       int length;              //数组的实际长度
};
Array::Array(int size) 
 //构造函数,初始化数据成员(为data分配内存,MaxSize置为size,length置为0)
 {
     MaxSize=size;
     data=new double[MaxSize];
     length=0;
 }
int Array::Length()   //获取顺序表实际长度
{
    return length;
}
double Array::Get(int pos)    //获取下标为pos的元素的值
{
    return data[pos];
}
void Array::Insert(int pos, double x) //在下标pos处插入x
{
    for(int i=length-1;i>=pos;i--)
        data[i+1]=data[i];
    data[pos]=x;
    length++;
}
void Array::Display()       //输出线性表  
{
    cout<<"The length:"<<length<<endl;
    cout<<"The elements:";
    for(int i=0;i<length;i++)
        cout<<data[i]<<" ";
    cout<<endl;
}
Array::~Array()  //destructor      
{
    delete []data;
}
//SortArray类定义如下(不允许增加其它成员函数):
class SortArray:private Array
{
public:
       SortArray(int size);
       int Length();   //获取顺序表实际长度
       double Get(int pos);     //获取下标为pos的元素的值
       void Display();       //输出线性表        
       void Insert(double x); //递增有序数组中插入x,使序列仍有序
};
SortArray::SortArray(int size):Array(size){}
int SortArray::Length()   //获取顺序表实际长度
{
    return Array::Length();
}
double SortArray::Get(int pos)    //获取下标为pos的元素的值
{
    return Array::Get(pos);
}
void SortArray::Display()      //输出线性表        
{
    Array::Display();
}
void SortArray::Insert(double x) //递增有序数组中插入x,使序列仍有序
{
    int i;
    for(i=0;i<Length();i++)
        if(Get(i)>x)
           break;
    Array::Insert(i,x);
}
//main中的代码如下(不允许改动):
int main()
{
       int size;
       cin>>size;
       SortArray sa(size);
       double num;
       while(1)
       {
              cin>>num;
              if(fabs(num)<=1e-6) break;
              try
              {
                     sa.Insert(num);
              }
              catch(char* wrong)
              {
                     cout <<wrong<<endl;    //如失败提示失败信息
              }
       }
       sa.Display();  
    return 0;
}

继承与派生 07

已知由Automobille类派生出Car类和Wagon类,而后两者共同派生出StationWagon类,各类的定义及main中的代码(不允许改动)如下,请实现各个类的成员函数,完成相应的输出:

class Automobile              //汽车类
{
private:
   int power;   //马力
public:
       Automobile(int p);
       void Display();
};
class Car:virtual  public Automobile   //小客车类
{
private:
      int seat;     //座位
public:
       Car(int p,int s);
       void Display();
};
class Wagon:virtual  public Automobile //小货车类
{
private:
   int load;     //装载量
public:
    Wagon(int p,int l);
 void Display();
};
class StationWagon :public Car, public Wagon  //客货两用车类
{
public:
       StationWagon(int p, int s,int l);
       void Display();
};
int main()
{
       int power,load,seat;
       cin>>power>>seat>>load;
       StationWagon sw(power,seat,load);
       sw.Display();  
       return 0;
}

Sample Input
108 3 10
Sample Output
StationWagon:
Power:108
Seat:3
Load:10

#include<iostream>
using namespace std;
class Automobile              //汽车类
{
private:
   int power;   //马力
public:
       Automobile(int p);
       void Display();
};
Automobile::Automobile(int p)
{
    power=p;
}
void Automobile::Display()
{
    cout<<"Power:"<<power<<endl;
}
class Car:virtual  public Automobile   //小客车类
{
private:
      int seat;     //座位
public:
       Car(int p,int s);
       void Display();
};
Car::Car(int p,int s):Automobile(p)
{
    seat=s;
}
void Car::Display()
{
    cout<<"Seat:"<<seat<<endl;
}
class Wagon:virtual  public Automobile //小货车类
{
private:
   int load;     //装载量
public:
    Wagon(int p,int l);
    void Display();
};
Wagon::Wagon(int p,int l):Automobile(p)
{
    load=l;
}
void Wagon::Display()
{
    cout<<"Load:"<<load<<endl;
}
class StationWagon :public Car, public Wagon  //客货两用车类
{
public:
       StationWagon(int p, int s,int l);
       void Display();
};
StationWagon::StationWagon(int p, int s,int l) :Automobile(p),Car(p,s),Wagon(p,l){}
void StationWagon::Display()
{
    cout<<"StationWagon:"<<endl;
    Automobile::Display();
    Car::Display();
    Wagon::Display();
}
int main()
{
       int power,load,seat;
       cin>>power>>seat>>load;
       StationWagon sw(power,seat,load);
       sw.Display();  
       return 0;
}

继承与派生 08

Person类派生大学生CollegeStu类。设计一个Person类,其属性包括姓名name和身份证号id,其中name为string类型,id也为string型,编写成员函数:构造函数Person、Display函数(显示数据成员信息)和析构函数;由Person类派生出大学生类CollegeStu,其属性有专业major(string类型),C++程序设计课程成绩score(double型),编写构造函数(实现数据初始化)、输出函数Display(包括name,id,major,score)。

//main的代码如下:(不允许改动)
int main()
{
       string name,major;
       string id;
       double score;
       cin>>name>>id>>major>>score;
       CollegeStu cs(name,id,major,score);
       cs.Display();
       return 0;
}

Sample Input
Zhangsan 210302198909120938 Software 99.5
Sample Output
Name:Zhangsan
ID:210302198909120938
Major:Software
C++ Score:99.5

#include<iostream>
//#include<string>
using namespace std;
class Person
{
public:
    Person(string name,string id);
    void Display();
    ~Person();
private:
    string m_name;
    string m_id;
};
Person::Person(string name,string id)
{
    m_name=name;
    m_id=id;
}
void Person::Display()
{
    cout<<"Name:"<<m_name<<endl;
    cout<<"ID:"<<m_id<<endl;
}
Person::~Person(){}
class CollegeStu :public Person
{
public:
    CollegeStu(string name,string id,string major,double score);
    void Display();
private:
    string m_major;
    double m_score;
};
CollegeStu::CollegeStu(string name,string id,string major,double score):Person(name,id)
{
    m_major=major;
    m_score=score;
}
void CollegeStu::Display()
{
    Person::Display();
    cout<<"Major:"<<m_major<<endl;
    cout<<"C++ Score:"<<m_score<<endl;
}
//main的代码如下:(不允许改动)
int main()
{
       string name,major;
       string id;
       double score;
       cin>>name>>id>>major>>score;
       CollegeStu cs(name,id,major,score);
       cs.Display();
       return 0;
}

大家好,我是Lucky_追梦仔。一个正在学习编程的小白,希望我的博文,可以帮助到您学习,或者解决您遇到的问题。

  • 28
    点赞
  • 96
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值