實驗四 繼承

2022-09-10 08:06:10 字數 2183 閱讀 8954

task2.cpp

#include #include 

//definitation of graph

class

graph

};//

definition of rectangle, derived from graph

class rectangle : public

graph

};

//definition of circle, derived from graph

class circle : public

graph

};

//definitaion of fun(): as a call inte***ce

void fun(graph*ptr)

//test

intmain()

結果截圖

歸納總結:

同名覆蓋原則:當呼叫派生類內與基類同名的函式時,預設呼叫派生類內的函式。

二元作用域分辨符:派生類名.基類::函式名,此時呼叫的是作用域標定的類內函式。

型別相容原則:當派生類與基類為public派生時,可以互相呼叫彼此類內的函式及資料。

virtual後:

graph::draw()變為虛基類,指標指向不再由指標型別決定,而是指向定義時的定義型別。於是r1指向的是rectangle類,呼叫draw()時預設呼叫rectangle::draw();c1同理。

task3:

battery.hpp:

#include#include

using

namespace

std;

class

battery

int get_capacity()

};

car.hpp:

#include#include

using

namespace

std;

class

car

void

info()

void update_odometers(int

new_odometers)

};

electriccar.hpp:

#include"

battery.hpp

"#include

"car.hpp

"class electriccar :public

car

void

info()

};

task3.cpp:

#include"

electriccar.hpp

"int

main()

結果截圖:

結果截圖:

實驗四 繼承

簡單說明 基於虛基類的一些特性進行的驗證性實驗,理解並掌握虛基類的使用特點與注意事項。源 如下 關於虛基類已修改 include include definitation of graph class graph definition of rectangle,derived from graph ...

實驗四 繼承

任務二 1 cpp include include definitation of graph class graph definition of rectangle,derived from graph class rectangle public graph definition of circ...

實驗四 繼承

task2.cpp include include definitation of graph class graph definition of rectangle,derived from graph class rectangle public graph definition of circ...