實驗四:類的繼承,派生和多態(1)


 

【實驗結論】

#1.車輛基本信息管理

#ifndef BATTERY_H
#define BATTERY_H

class Battery
{
    public:
        Battery(int batterSize0=70);
        int getbatterysize();
        ~Battery();
    private:
        int batterySize;    
};

#endif
Battery.h
#include"Battery.h"

Battery::Battery(int batterySize0):batterySize(batterySize0)
{}

int Battery::getbatterysize()
{
    return batterySize;
}

Battery::~Battery()
{}
Battery.cpp
#ifndef CAR_H
#define CAR_H
#include<iostream>
using std::string;
using std::ostream;
class Car
{
    public:
        Car(string maker0,string model0,int year0);
        friend ostream &operator<<(ostream &out,const Car &c);
        void updateOdometer(int updatemeter);
        string getmaker() const;
        string getmodel() const;
        int getyear() const;
        int getodometer() const;
        ~Car();
    private:
        string maker;
        string model;
        int year;
        int odometer;
};

#endif
Car.h
#include<iostream>
#include"Car.h"
using namespace std;

Car::Car(string maker0,string model0,int year0):maker(maker0),model(model0),year(year0)
{
    odometer=0;
}

ostream &operator<<(ostream &out,const Car &c)
{
    out<<"maker:\t\t"<<c.maker<<endl
       <<"model:\t\t"<<c.model<<endl
       <<"year:\t\t"<<c.year<<endl
       <<"odometer:\t"<<c.odometer<<endl;
    return out;            
}

void Car::updateOdometer(int updatemeter)
{
    if(updatemeter<odometer)
    {
        cout<<"Warning:Wrong!"<<endl;
    }
    else odometer=updatemeter;
}

string Car::getmaker() const
{
    return maker;
}

string Car::getmodel() const
{
    return model;
}

int Car::getyear() const
{
    return year;
}

int Car::getodometer() const
{
    return odometer;
}

Car::~Car()
{ }
Car.cpp
#ifndef ElectricCar_H
#define ElectricCar_H
#include<iostream>
#include"Car.h"
#include"Battery.h"
using std::string;
using std::ostream;

class ElectricCar:public Car
{
    public:
        ElectricCar(string maker0,string model0,int year0);
        friend ostream &operator<<(ostream &out,const ElectricCar &e);
        ~ElectricCar();
    private:
        Battery battery;
        int batterySize;
};

#endif
ElectricCar.h
#include<iostream>
#include"ElectricCar.h"
#include"Car.h"
#include"Battery.h"
using namespace std;

ElectricCar::ElectricCar(string maker0,string model0,int year0):Car(maker0,model0,year0)
{
    batterySize=battery.getbatterysize();
}

ostream &operator<<(ostream &out,const ElectricCar &e)
{
    out<<"maker:\t\t"<<e.getmaker()<<endl
       <<"model:\t\t"<<e.getmodel()<<endl
       <<"year:\t\t"<<e.getyear()<<endl
       <<"odometer:\t"<<e.getodometer()<<endl
       <<"batterySize:\t"<<e.batterySize<<"-kWh"<<endl;
    return out;
}

ElectricCar::~ElectricCar()
{ }
    
ElectricCar.cpp
#include <iostream>
using namespace std;

#include "Car.h"
#include "ElectricCar.h" 

int main() {
    // 測試Car類 
    Car oldcar("Audi","a4",2016);
    cout << "--------oldcar's info--------" << endl;
    oldcar.updateOdometer(25000);
    cout << oldcar << endl;

    // 測試ElectricCar類 
    ElectricCar newcar("Tesla","model s",2016);
    newcar.updateOdometer(2500);
    cout << "\n--------newcar's info--------\n"; 
    cout << newcar << endl;

    system("pause");
    
    return 0;
}
main.cpp

 [修改]

       因為上面被折疊了,我這里也不好改(也許好改,但是找不到),所以在這里說明一下,這段程序中,用到string的地方要加頭文件#include<string>~(這里說一下,該頭文件是要加using namespace std的,或者std::string的,還有,他和#include<cstring>/<string.h>不是一個頭文件)。

[運行結果]

 

[擴充]

ostream &operator<<(ostream &out,const ElectricCar &e)
{
    out<<Car(e)
       <<"batterySize:\t"<<e.batterySize<<"-kWh"<<endl;
    return out;
}

這樣也行,調用了Car類中聲明的<<重載運算符友元函數。

 

#2.重載運算符[ ]

#ifndef ARRAY_INT_H
#define ARRAY_INT_H

class ArrayInt{
    public:
        ArrayInt(int n, int value=0);
        ~ArrayInt();
        int &operator[](int i); 
        void print(); 
    private:
        int *p;
        int size;
};

#endif
ArrayInt.h
#include "arrayInt.h"
#include <iostream>
#include <cstdlib>
using std::cout;
using std::endl;

ArrayInt::ArrayInt(int n, int value):size(n)
{
    p=new int[size];
    if(p==0)
    {
        cout<<"fail to mallocate memory"<<endl;
        exit(0); 
    } 
    for(int i=0;i<size;i++) p[i]=value;
}

ArrayInt::~ArrayInt() 
{
    delete[] p;
}

void ArrayInt::print() 
{
    for(int i=0;i<size;i++) cout<<p[i]<<" ";
    cout<<endl;
}

int &ArrayInt::operator[](int i)
{
    return p[i];
}
ArrayInt.cpp
#include <iostream>
using namespace std;

#include "arrayInt.h"

int main()
{
    // 定義動態整型數組對象a,包含2個元素,初始值為0
    ArrayInt a(2);
    a.print();
    
    // 定義動態整型數組對象b,包含3個元素,初始值為6
    ArrayInt b(3,6);
    b.print();

    // 通過對象名和下標方式訪問並修改對象元素
    b[0]=2;
    cout<<b[0]<<endl;
    b.print();

    system("pause");

    return 0;
}
main.cpp

 

[運行結果]

 

【實驗總結】

嗯,第一個汽車相關的實現,Battery和ElectricCar之間是組合關系,ElectricCar和Car之間是繼承關系,這里解釋一下組合和繼承:(由於最近時間關系沒有來得及整理知識點,所以就先現寫一下,將就一下,下次寫好發上來~)

      組合關系相當於has—it,即ElectricCar里面有Battery,簡單點來說,就是如果有一個教室,它是由桌椅,風扇,多媒體構成的,那么教室,桌椅,風扇,空調之間就是組合關系。

      繼承關系相當於is—it,即ElectricCar是Car,這個比較好理解一點,比如楓樹和樹,楓樹是樹,所以楓樹和樹是繼承關系。

關於第二個[ ] 的重載,思路就是要讓計算機知道b[0]是一個數組(大概是這么解釋的趴?)

 

附兩個最開始學繼承和組合時的blog:https://www.cnblogs.com/shmilxu/p/4849097.html(繼承),https://blog.csdn.net/ForestRound/article/details/52726984(組合)

再附一個<<和>>重載的blog:https://www.cnblogs.com/wuchanming/p/3879159.html

 

【評論地址】

https://www.cnblogs.com/mzy-1229/p/10889880.html#4264060

https://www.cnblogs.com/wyy0204/p/10891409.html#4264065

https://www.cnblogs.com/Kun-520/p/10896481.html#4264069

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM