實驗1:
a.h
#ifndef a_hb.h#define a_h#include
"base.h
"class a:public
base;
int sub(int m,int n);
};#endif
#ifndef b_hc.h#define b_h#include
"base.h
"class b:public
base;
int mul(int m,int n);
};#endif
#ifndef c_hbase.h#define c_h#include
"base.h
"class c:public
base;
int div(int m,int n);
};#endif
#ifndef base_hmain.cpp#define base_h
class
base;
int add(int m,int n);
protected
:
intm;
intn;
};#endif
#include#include截圖:"a.h
"#include
"b.h
"#include
"c.h
"using
namespace
std;
intmain()
實驗2:
vehicle.h
#ifndef vehicle_hvehicle.cpp#define vehicle_h
class
vehicle;
#endif
#include#includebicycle.h"vehicle.h
"using
namespace
std;
vehicle::vehicle(
int m,int
w):maxspeed(m),weight(w)
vehicle::~vehicle()
void vehicle::run()
void vehicle::stop()
int vehicle::showmaxspeed()
int vehicle::showweight()
#ifndef bicycle_hbicycle.cpp#define bicycle_h#include
"vehicle.h
"class bicycle:virtual
public
vehicle;
#endif
#include#includemotorcar.h"bicycle.h
"using
namespace
std;
bicycle::bicycle(
int h,int m,int
w):height(h),vehicle(m,w)
bicycle::~bicycle()
int bicycle::showheight()
#ifndef motorcar_hmotorcar.cpp#define motorcar_h#include
"vehicle.h
"class motorcar:virtual
public
vehicle;
private
:
intseatnum;
};#endif
#include#includemotorcycle.h"motorcar.h
"using
namespace
std;
motorcar::motorcar(
int s,int m,int
w):seatnum(s),vehicle(m,w)
motorcar::~motorcar()
#ifndef motorcycle_hmotorcycle.cpp#define motorcycle_h#include
"motorcar.h
"#include
"bicycle.h
"class motorcycle:public bicycle,public
motorcar;
#endif
#include#includemain.cpp"motorcycle.h
"using
namespace
std;
motorcycle::motorcycle(
int h,int s,int m,int
w):bicycle(h,m,w),motorcar(s,m,w),vehicle(m,w)
motorcycle::~motorcycle()
#include#include截圖:"motorcycle.h
"using
namespace
std;
intmain()
實驗3:
faction.h
#ifndef faction_hfaction.cpp#define faction_h
class
faction
;#endif
#include "ifaction.hfaction.h
"#include
using
namespace
std;
//建構函式
faction::faction():top(0), bottom(1
) //
建構函式的過載
faction::faction(int t0):top(t0), bottom(1
) //
建構函式的過載
faction::faction(int t0,int
b0):top(t0), bottom(b0)
//輸出函式
void
faction::show()
faction faction::
operator+(faction &f0)
faction faction::
operator-(faction &f0)
faction faction::
operator*(faction &f0)
faction faction::
operator/(faction &f0)
//比較大小函式
void faction::compare(faction &f1)
else
}int
faction::gettop()
intfaction::getbottom()
#include "main.cppfaction.h
"#include
using
namespace
std;
class ifaction:public
faction;
ifaction(
int t):faction(t,1
){};
ifaction():faction(
0,1){};
void
ishow()
b/=a;
t/=a;
if (b == 0
)
else
if (t == 0
)
else
if ( t
else
cout
<< b << "("
<< t+b << "/"
<< b << ")"
<
}
};
#include #include這個報錯說using namespace std;那一行錯了,我不太懂……把派生類刪了也是那個地方報錯。"ifaction.h
"using
namespace
std;
intmain()
繼承和多型
物件導向程式設計時有乙個非常重要的原則 write once only once 編寫一次,且僅編寫一次 如果沒有繼承這種機制我們可能要重複寫很多 下面來看一下繼承 一 繼承 1 子類如果繼承父類必須使用extends這個關鍵字 2 子類呼叫父類的構造方法使用super關鍵字,也可以通過super來...
繼承和多型
物件導向程式設計時有乙個非常重要的原則 write once only once 編寫一次,且僅編寫一次 如果沒有繼承這種機制我們可能要重複寫很多 下面來看一下繼承 一 繼承 1 子類如果繼承父類必須使用extends這個關鍵字 2 子類呼叫父類的構造方法使用super關鍵字,也可以通過super來...
繼承和多型
類還有乙個重要的特性就是繼承,什麼是繼承?它又有什麼作用呢?繼承最主要的目的就是為了擴充套件原類的功能,加強或改進原類所沒有定義的屬性及方法。例如我們有狗這個類,可是狗的種類還是有很多,比如說博美狗,聖伯納,柴犬等等,如果只用狗一種類來定義所有種類狗的屬性及方法,那麼用這個類所產生出來的物件一定會有...