繼承與派生

2021-07-24 13:39:03 字數 2981 閱讀 9740

///剛看了繼承和派生 感覺懵懵噠~

/ 簡單的基類

/*#include #includeusing namespace std;

class tabletennisplayer

; char firstname[lim];

char lastname[lim];

bool hastable;

public:

tabletennisplayer(const char *fn = "none", const char *ln = "none", bool ht = false);

void name()const;

bool hastable()const

void resettable(bool v)

};tabletennisplayer::tabletennisplayer(const char *fn, const char* ln, bool ht)

void tabletennisplayer::name()const

class ratedplayer: public tabletennisplayer

void resetrating(unsigned int r)

};ratedplayer::ratedplayer(unsigned int r,const char *fn, const char *ln, bool ht): tabletennisplayer(fn, ln, ht)

ratedplayer::ratedplayer(unsigned int r, const tabletennisplayer & tp):tabletennisplayer(tp), rating(r)

int main()

*//*

ying, zhang:has a table

zeyun, zheng:hasn't a table

name: ying, zhang;rating:1140

name: zeyun, zheng;rating:1440

process returned 0 (0x0) execution time : 6.777 s

press any key to continue.

*//派生類和基類之間的特殊關係

/繼承 is-a關係(is a kind of)

/多型共有繼承

///在兩個類中行為相同的方法 只需在基類中宣告

#include#includeusing namespace std;

class brass

; char fullname[max];

long acctnum;

double balance;

public:

brass(const char *s = "nullbody", long an = -1, double bal = 0.0);

void deposit(double amt);

virtual void withdraw(double amt);

double balance()const;

virtual void viewacct()const;

virtual ~brass(){}

};class brassplus: public brass

void resetrate(double r)

void resetowes()

};brass::brass(const char *s, long an, double bal)

void brass::deposit(double amt)

void brass::withdraw(double amt)

double brass::balance()const

void brass::viewacct()const

brassplus::brassplus(const char *s, long an, double bal, double ml, double r):brass(s, an, bal)

void brassplus::viewacct()const

void brassplus::withdraw(double amt)

else

}int main()

/*client: porcelot pigg

account number: 381299

balance: $4000

client: horatio hogg

account number: 382288

balance: $3000

maxinum loan: $500

owed to bank: $0

loan rate: 10%

depositing $1000 into the hogg account:

new balance: $4000

withdrawing $4200 from the pigg account:

withdrawal amount of $4200 exceeds your balance.

withdrawal canceled

pigg account balance: $4000

withdrawing $4200 from the hogg account:

bank advance: $200

finance charge: $20

client: horatio hogg

account number: 382288

balance: $0

maxinum loan: $500

owed to bank: $220

loan rate: 10%

process returned 0 (0x0) execution time : 4.455 s

press any key to continue.

*/

派生與繼承 多重派生

1 理解下面的程式,並在 vc 6.0 下執行檢視結果,回答程式後面的問題。class cbase1 cbase1 void print protected int a class cbase2 cbase2 void print protected int b class cderive publ...

繼承與派生

實驗內容 建立乙個基類,兩個派生類,讓每乙個派生類都包含乙個函式area 分別用來返回矩形和三角形的面積。用建構函式對height和width進行初始化。要求通過基類指標訪問虛函式的方法。純虛函式 virtual double area 0 純虛函式的作用是在基類中為派生類保留乙個函式的名字,以便派...

繼承與派生

在c 中,所謂 繼承 就是在乙個已存在的類的基礎上建立乙個新的類。已存在的類成為 基類 新建立的類成為 派生類 乙個新類從已有的類那裡獲得其已有特性,這種現象成為累的繼承。從另一角度說,從已有的類產生乙個新的子類,稱謂累的派生。派生類的宣告方式 class 派生類名 繼承方式 基類名 派生類新增加的...