資源簡介
清華大學出版社,龔曉慶付麗娜等編寫的《C++面向對象程序設計》第二版,課后編程題參考答案。

代碼片段和文件信息
/*
?*?ex10_3.cpp
?*
?*??3.利用抽象類和虛函數改寫第9?章習題中的Shape?類層次。
?*??再編寫一個totalArea()函數計算任意兩個形狀的面積之和。
?*
?*/
#include?
#include?
#include?
using?namespace?std;
//-----------------------------------------
class?Shape
{
public:
virtual?double?area()const?=?0;
virtual?double?perimeter()const?=?0;
virtual?void?scale(double?factor)?=?0;
};
//-----------------------------------------
class?Triangle?:?public?Shape
{
public:
Triangle(double?a=1?double?b=1?double?c=1);
double?area()const;
double?perimeter()const;
void?scale(double?factor);
double?getA(){return?a;}
double?getB(){return?b;}
double?getC(){return?c;}
//others......
private:
double?a?b?c;
static?bool?validate(double?a?double?b?double?c);
};
Triangle::Triangle(double?x?double?y?double?z)
{
bool?v?=?validate(xyz);
assert(v);
a=x;?b=y;?c=z;
}
double?Triangle::area()const
{
double?p?=?(a+b+c)/2;
double?s?=?p*(p-a)*(p-b)*(p-c);
return?sqrt(s);
}
double?Triangle::perimeter()const
{
return?a+b+c;
}
//others......
void?Triangle::scale(double?factor){
assert(factor>0);
a?*=?factor;
b?*=?factor;
c?*=?factor;
}
bool?Triangle::validate(double?a?double?b?double?c)
{
if((a<=0)?||?(b<=0)?||?(c<=0))
return?false;
if((a+b<=c)?||?(a+c<=b)?||?(b+c<=a))
return?false;
return?true;
}
//----------------------------------
class?Rectangle:public?Shape
{
public:
Rectangle(double?w=1.0double?h=1.0);
double?area()const;
double?perimeter()const;
void?scale(double?f);
double?width(){return?_width;}
double?height(){return?_height;}
private:
double?_height;
double?_width;
};
Rectangle::Rectangle(double?wdouble?h)
{?assert(w>0);
assert(h>0);
_width?=?w;
_height?=?h;
}
double?Rectangle::area()const
{
return?_height*_width;
}
double?Rectangle::perimeter()const
{
return?2*(_height+_width);
}
void?Rectangle::scale(double?f)
{
assert(f?>?0);
_height?*=?f;
_width?*=?f;
}
//------------------------------
class?Circle?:?public?Shape
{
public:
Circle(double?r?=?1.0);
double?area()const;
double?perimeter()const;
void?scale(double?factor);
double?getRadius(){return?radius;}
private:
double?radius;
};
const?double?pi=3.1415926;
Circle::Circle(double?r)
{
assert(r>0);
radius?=?r;
}
double?Circle::area()const
{
return?pi*radius*radius;
}
double?Circle::perimeter()const
{
return?pi*radius*2;
}
void?Circle::scale(double?f)
{
assert(f>0);
radius?*=?f;
}
//------------------------------------
double?totalArea(Shape&?s1?Shape&?s2)
{
return?s1.area()+s2.area();
}
//-------------------------
int?main()
{
Rectangle?r(53);
cout<<“Rectangle?r:\t“<<“width:?“< <<“\theight:“< cout<<“area=“< Circle?c(4);
cout<<“Circle?c:\t“<<“radius:?“< cout<<“area=“< Triangle?t(534);
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-07-24?20:35??excercise\
?????文件????????1848??2017-02-06?19:06??excercise\ch11\ex11_6\array.h
?????文件?????????371??2017-02-06?16:16??excercise\ch10\ex10_4\basket.cpp
?????文件?????????287??2017-02-06?16:16??excercise\ch10\ex10_4\basket.h
?????文件?????????604??2017-02-06?16:26??excercise\ch10\ex10_2\cart.h
?????文件?????????423??2017-02-06?19:00??excercise\ch11\ex11_4\client.cpp
?????文件?????????486??2017-02-06?19:04??excercise\ch11\ex11_5\client.cpp
?????文件?????????542??2017-02-06?19:10??excercise\ch12\ex12_3\client.cpp
?????文件??????????54??2016-09-02?18:47??excercise\ch4\data.txt
?????文件????????1833??2017-02-06?19:08??excercise\ch11\ex11_sikaoti3\deList.h
?????文件????????1277??2017-02-06?19:08??excercise\ch11\ex11_sikaoti3\delistfunc.h
?????文件?????????264??2016-07-28?14:52??excercise\ch2\ex2_5.cpp
?????文件?????????490??2016-07-28?14:58??excercise\ch2\ex2_6.cpp
?????文件?????????461??2016-07-28?14:52??excercise\ch2\ex2_7.cpp
?????文件?????????271??2016-07-28?15:01??excercise\ch2\ex2_8.cpp
?????文件?????????442??2016-07-28?17:42??excercise\ch3\ex3_1.cpp
?????文件?????????595??2016-07-28?17:49??excercise\ch3\ex3_2.cpp
?????文件?????????606??2016-07-28?17:55??excercise\ch3\ex3_3.cpp
?????文件????????1356??2016-07-28?18:13??excercise\ch3\ex3_4.cpp
?????文件?????????499??2016-07-28?18:21??excercise\ch3\ex3_5.cpp
?????文件?????????519??2016-07-28?18:28??excercise\ch3\ex3_6.cpp
?????文件?????????714??2016-07-28?18:38??excercise\ch3\ex3_7.cpp
?????文件?????????581??2016-07-28?18:44??excercise\ch3\ex3_8.cpp
?????文件?????????692??2016-07-28?18:52??excercise\ch3\ex3_9.cpp
?????文件?????????835??2016-07-28?19:41??excercise\ch3\ex3_10.cpp
?????文件?????????801??2016-07-28?19:49??excercise\ch3\ex3_11.cpp
?????文件?????????930??2016-07-28?20:01??excercise\ch3\ex3_12.cpp
?????文件?????????354??2016-07-28?19:09??excercise\ch3\ex3_13.cpp
?????文件?????????369??2016-07-28?19:21??excercise\ch3\ex3_14.cpp
?????文件?????????323??2016-07-28?19:11??excercise\ch3\ex3_15.cpp
?????文件?????????364??2016-07-28?19:12??excercise\ch3\ex3_16.cpp
............此處省略127個文件信息
評論
共有 條評論