實驗四 繼承

2022-09-10 09:15:13 字數 2761 閱讀 3567

實驗二**

#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()

改動前的執行結果如下所示:

改動後的執行結果如下所示:

歸納總結:

同名覆蓋原則:當派生類與基類有同名成員時,若未明確指出,則通過派生類物件使用的是派生類中的同名成員。

二元作用域分辨符:通過使用繼承類::函式來訪問基類中被派生類覆蓋的同名函式。

型別相容原則:型別相容規則是指在需要基類物件的任何地方,都可以使用公有派生類的物件來替代。基類物件可以使用共有派生類的物件代替。

實驗三battery.hpp

#include #include 

using

namespace

std;

class

battery

;battery::battery()

intbattery::get_capacity()

car.hpp

#include #include 

using

namespace

std;

class

car;

car::car()

car::car(

string maker0,string model0,int

year0)

void

car::info()

int car::update_odometers(int

a)

electriccar.hpp

#include #include 

using

namespace

std;

class electriccar : public

car ;

electriccar::electriccar()

electriccar::electriccar(

string maker1,string model1,int

year1)

void

electriccar::info()

task3.cpp

#include #include 

#include

"battery.hpp

"#include

"car.hpp

"#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...