C++統合と派生練習問題の解答

3063 ワード

総合問題1:考察書の継承方式
総合問題1.プログラムを作成して自動車類vehicleを設計し、データメンバーには車輪個数wheelsと車重weightが含まれている.小車類carはその私有派生類で、積載人数pasengerを含む.load.トラッククラスtruckはvehicleのプライベート派生クラスで、積載人数passenger_を含むloadと荷重payloadは,各クラスに関連データの出力方法がある.
#include
using namespace std;
class vehicle 
{
  int wheels,weight;
    public:
  vehicle() {wheels=4,weight=100;}
  void show1(){cout<

総合問題2構造関数と構造関数を考察する
#include 
using namespace std;
class base
{
public:
base(){cout<

総合問題3考察累の継承方式
テーマの要求:図書館の図書が本名、番号、著者の3つの属性を含むと仮定する.読者は名前と借書証の2つの属性を含んで、1人の読者は最大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<>i)
        {
            switch(i)
            {
                case 0:
                    cout<>na;
                    cout<>nu;
                    cout<>author;

                    b[num].name = na;
                    b[num].num = nu;
                    b[num].writer = author;
                    num++;
                }
                    break;
                case 2:
                if(num == 0) cout<>na;
                    int i;
                    for(i=0;i

総合問題4-虚基類の実現を考査する
以下のプログラムの実行結果を分析する
#include 
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<<:n/>

総合問題5
重集積クラスの設計を考察する
円クラスcircleとテーブルクラスtableを設計し、円卓クラスroundtableを設計します.これは前の2つのクラスから派生し、円卓の高さ、面積、色などのデータを出力する必要があります.
#include
#include
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<