C ++での継承

メーカー名(文字列型)を含む車両(車両)と呼ばれる基本クラス、エンジン番号(int型)シリンダと所有者(Person型)を作成し、派生クラストラック車両の種類(トラック)を作成し、それが持っていますいくつかの追加の負荷を含む性質、(double型単位:トン)とトラクション(int型、単位:ポンド)。合理的なクラスのコンストラクタ引数のメンバ関数(GET)、割り当てメンバ関数(SET)、オーバーロードされた代入演算子とコピーコンストラクタを持つ必要があります。

試験方法の主な機能でテストコードを書くと、特定のページに従ったPersonクラスの宣言のすべてのメンバーを完了する。

コード実装セクション:

#include <iostream>

#include<string.h>

using namespace std;

class Person

{

public:

    Person();

    Person(string str);

    string getName()const;

    Person& operator=(const Person& p);

    friend istream& operator>>(istream& in, Person& p);

    friend ostream& operator<<(ostream& out, const Person& p);

private:

    string name;

};

/*车*/

class Vehicele

{

public:

    Vehicele()

    {



    };

    Vehicele(Person, string, int);

    Vehicele(const Vehicele&);//拷贝

    Person get_person()const;

    void set_person(Person);

    string get_manufacturer()const;

    void set_manufacturer(string);

    int get_enginenum()const;

    void set_enginenum(int);

    Vehicele& operator=(const Vehicele&);

protected:

    Person person;

    string manufacturer;//制造商

    int enginenum;//发动机缸数

};

//卡车

class Truck :public Vehicele

{

public:

    Truck(double, int, Person,string, int);

    Truck(const Truck&);

    double get_ton()const;

    void set_ton(double);

    int get_pound()const;

    void set_pound(int);

    Truck& operator=(const Truck& t);

protected:

    double ton;//吨

    int pound;//牵引力

};

Person::Person()

{

    name = "";

}

Person::Person(string str)

{

    name = str;

}

string Person::getName()const

{

    return name;

}

Person& Person::operator=(const Person& p)

{

    this->name = p.name;

    return *this;

}

istream& operator>>(istream& in, Person& p)

{

    in >> p.name;

    return in;

}

ostream& operator<<(ostream& out, const Person& p)

{

    out << p.name;

    return out;

};



Vehicele::Vehicele(Person p, string a, int b)

{

    person = p;

    manufacturer = a;

    enginenum = b;

}

Person Vehicele::get_person()const

{

    return this->person;

}

void Vehicele::set_person(Person p)

{

    this->person = p;

}

string Vehicele::get_manufacturer() const

{

    return manufacturer;

}

void Vehicele::set_manufacturer(string a)

{

    manufacturer = a;



}



int Vehicele::get_enginenum() const

{

    return enginenum;

}



void Vehicele::set_enginenum(int a)

{

    enginenum = a;

}



Vehicele& Vehicele::operator=(const Vehicele& v)

{

    enginenum = v.enginenum;

    manufacturer = v.manufacturer;

    return *this;

}



Vehicele::Vehicele(const Vehicele& t)

{

    this->enginenum = t.enginenum;

    this->manufacturer = t.manufacturer;

    this->person = t.person;

}





Truck::Truck(double a, int b, Person p, string s, int c) :Vehicele(p, s, c)

{

    this->ton = a;

    this->pound = b;

}

Truck::Truck(const Truck& t) : Vehicele(t.person, t.manufacturer, t.enginenum)

{

    this->ton = t.ton;

    this->pound = t.pound;

}

double Truck::get_ton()const

{

    return ton;

}

void Truck::set_ton(double a)

{

    ton = a;

}

int Truck::get_pound()const

{

    return pound;

}

void Truck::set_pound(int a)

{

    this->pound = a;



}

Truck& Truck::operator=(const Truck& t)

{

    ton = t.ton;

    pound = t.pound;

    return *this;

}

int main()

{

    /*车类继承*/

    Person person;

    string manufacturer;//制造商

    int enginenum;//发动机缸数

    double ton = 0.00;//吨

    int pound = 0;//牵引力



    cout << "你好,请填写你的购车信息" << endl;

    cout << "姓名、制造商" << endl;

    cin >> person>> manufacturer;

    cout << "发动机缸数、吨、牵引力" << endl;

    cin >> enginenum >> ton >> pound;

    cout << endl;



    /*小车*/

    Vehicele vc(person, manufacturer, enginenum);

    cout << vc.get_person() << "购买了一辆:" << vc.get_manufacturer() << "发动机缸数是:" << vc.get_enginenum();



    /*卡车*/

    Truck tu(ton, pound, person, manufacturer, enginenum);

    /*调用浅拷贝*/

    Truck tc(tu);

    cout << tu.get_person() << "购买了一辆:" << tu.get_manufacturer() << "发动机缸数是:" << tu.get_enginenum()

        << "吨数:"<< tu.get_ton()<<"牵引力:"<< tu.get_pound()<<endl;

}

出力が表示されます。

 

おすすめ

転載: blog.csdn.net/qq_41979469/article/details/91356245