C++ 集成和派生练习题解答

综合题1:考察单继承方式

综合题1.编写一个程序设计一个汽车类vehicle,包含的数据成员有车轮个数wheels和车重weight。小车类car是它的私有派生类,其中包含载人数pasenger_load。卡车类truck是vehicle的私有派生类,其中包含载人数passenger_load和载重量payload,每个类都有相关数据的输出方法。

#include<iostream>
using namespace std;
class vehicle 
{
  int wheels,weight;
    public:
  vehicle() {wheels=4,weight=100;}
  void show1(){cout<<"wheels="<<wheels<<" ,wight="<<weight<<endl;}
};
class car:public vehicle
{
	int pasenger_load;
  public:
  car(){ pasenger_load=5;}
  void show2(){show1();cout<<"pasenger_load:"<<pasenger_load<<endl;}
};
class truck:public vehicle
{
	int pasenger_load,payload;
  public:
  truck(){ pasenger_load=5;payload=7;}
  void show3(){show1();cout<<"pasenger_load:"<<pasenger_load<<", payload="<<payload<<endl;}
};

int main()
{
  vehicle a;
  car b;
  truck c;
  a.show1();
  b.show2();
  c.show3();
}

综合题2 考察构造函数和析构函数

#include <iostream>
using namespace std;
class base
{
public:
base(){cout<<"constructing base class"<<endl;}
~base(){cout<<"destructing base class"<<endl;}
};
class subs:public base
{
public:
subs(){cout<<"constructing base class"<<endl;}
~subs(){cout<<"destructing base class"<<endl;}
};
int main()
{
subs s;
}
综合题3 考察累的继承方式

题目要求:假设图书馆的图书包含书名、编号,作者三个属性;读者包含姓名和借书证两个属性,每位读者最多可借5本书,编写程序列出某读者的借书情况。

#include "iostream"
#include "string"
using namespace std;
class obj
{
 public:
    string name;
    string num;
 public:
 obj(){name="0";num="0";}
  obj(string x,string y)
  {
    name=x;
    num=y;
  }
};
class book :public obj
{
    public:
        string writer;
    public:
book():obj(){writer="0";}

};
class reader:public obj
{
    private:
     book b[5];
     const int MAX;
     static int num;
    public:
    reader(string x,string y):obj(x,y),MAX(5){}
  int rentbook();
   void print();
};
int reader:: num =0 ;
int reader:: rentbook()
 {
     int i;
     cout<<"输入选项:0、退出,1、借书,2、还书 "<<endl;

        while(cin>>i)
        {
            switch(i)
            {
                case 0:
                    cout<<"!!!"<<endl;
                    return 0;
                case 1:
                if(num == 4) cout<<"已经借满5本"<<endl;
                else
                {
                    string na ,nu,author;
                    cout<<"输入所借书的书名:";
                    cin>>na;
                    cout<<"输入所借图书的编号:";
                    cin>>nu;
                    cout<<"输入图书的作者:";
                    cin>>author;

                    b[num].name = na;
                    b[num].num = nu;
                    b[num].writer = author;
                    num++;
                }
                    break;
                case 2:
                if(num == 0) cout<<"没有书籍可还!"<<endl;
                else
                {
                    string na ;
                    cout<<"输入所还书的书名";
                    cin>>na;
                    int i;
                    for(i=0;i<MAX;i++)
                        if(b[i].name == na)
                           { b[i].name ="0";b[i].num ="0";b[i].writer="0";cout<<"还书成功"<<endl; goto B ;}
                            cout<<"没有此书"<<endl;
                }

                B:break;
                default :
                    cout<<"please input the right number!!"<<endl;
                break;
            }
   cout<<"输入选项:0、退出,1、借书,2、还书 "<<endl;
 }
 }

 void reader:: print()
 {
     int i;
    cout<<"姓名:"<<obj::name<<"学号"<<obj::num<<endl;
    cout<<"所借图书如下:"<<endl;
    cout<<"书名"<<"\t"<<"编号"<<"\t"<<"作者"<<endl;
    for(i=0;i<MAX;i++)
    if(b[i].name !="0")
    cout<<b[i].name<<"\t"<<b[i].num<<"\t"<<b[i].writer<<endl;
 }
int main()
{reader a("wang","1234");
 a.rentbook();
 a.print();
}

综合题4 - 考查虚基类的实现

分析以下程序的执行结果

#include <iostream>
using namespace std;
class A
{
public:
int n;
};
class B:virtual public A{};
class C:virtual public A{};
class D:public B,public C
{
int get() {return B::n;}
};
int main()
{
D d;
d.B::n=10;
d.C::n=20;
cout<<d.B::n<<","<<d.C::n<<endl;
}
综合题5   考察重集成类的设计

设计一个圆类circle和一个桌子类table,另设计一个圆桌类roundtable,它是从前两个类派生的,要求输出一个圆桌的高度、面积和颜色等数据。

#include<iostream>
#include<string.h>
using namespace std;
class cicle 
{
  float radius;
  public:
  cicle(float a){radius=a;}
  float getaerea() {return 3.14*radius*radius;}
};
class table
{
  float height;
  public:
  table(float a){height=a;}
  float getheight() {return height;}
};
class roundtable:public cicle,public table
{
	string color;
	public:
  roundtable(float a,float b,string c): cicle(a),table(b){color=c;}
  string get() {return color;}
};
int main()
{
  roundtable AA(10,2,"wanglei");
 
  cout<<AA.getaerea()<<","<<AA.getheight()<<","<<AA.get()<<endl;
  
}




  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值