實驗二**
#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 #includecar.hppusing
namespace
std;
class
battery
;battery::battery()
intbattery::get_capacity()
#include #includeelectriccar.hppusing
namespace
std;
class
car;
car::car()
car::car(
string maker0,string model0,int
year0)
void
car::info()
int car::update_odometers(int
a)
#include #includetask3.cppusing
namespace
std;
class electriccar : public
car ;
electriccar::electriccar()
electriccar::electriccar(
string maker1,string model1,int
year1)
void
electriccar::info()
#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...