喜刷刷4 shape類 4(完成RsubC類)

2021-07-03 15:48:20 字數 1689 閱讀 8713

小聰送給小亮和小華的形狀他們都很喜歡,小亮和小華非要比一下他們兩個的形狀,來看看小聰更愛誰

小強寫的檔案頭和shape類

#include

#define pi 3.14

using namespace std;

class shape

;shape::shape()

shape::shape(int c)

int shape::getcolor()

double shape::area()

小聰寫的rectangle類

class rectangle:public shape

;rectangle::rectangle(int c,double w,double h):shape(c)

double rectangle::getwidth()

double rectangle::getheight()

double rectangle::area()

double rectangle::price()

小聰寫的circle類

class circle:public shape;

circle::circle(int c,double r):shape(c)

double circle::getradius()

double circle::area()

double circle::price()

小聰的測試函式:

int main()

#include#define pi 3.14

using namespace std;

class shape

;shape::shape()

shape::shape(int c)

int shape::getcolor()

double shape::area()

class rectangle:public shape

;rectangle::rectangle(int c,double w,double h):shape(c)

double rectangle::getwidth()

double rectangle::getheight()

double rectangle::area()

double rectangle::price()

class circle:public shape

;circle::circle(int c,double r):shape(c)

double circle::getradius()

double circle::area()

double circle::price()

class rsubc:public shape

;rsubc::rsubc(int c,double w,double h,double r,bool s):shape(c),rectangle(c,w,h),circle(c,r),sign(s){}

double rsubc::area()

int main()

{rsubc rc=rsubc(1,2,3,1,1);

cout<

rsubc area:2.86

學習總結:

這幾個shape類的題層層巢狀,循序漸進,很有用。