资源简介
清华大学出版社,龚晓庆付丽娜等编写的《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个文件信息
相关资源
- 国际象棋的qt源代码
- C++中头文件与源文件的作用详解
- C++多线程网络编程Socket
- VC++ 多线程文件读写操作
- 利用C++哈希表的方法实现电话号码查
- 移木块游戏,可以自编自玩,vc6.0编写
- C++纯文字DOS超小RPG游戏
- VC++MFC小游戏实例教程(实例)+MFC类库
- 连铸温度场计算程序(C++)
- 6自由度机器人运动学正反解C++程序
- Em算法(使用C++编写)
- libstdc++-4.4.7-4.el6.i686.rpm
- VC++实现CMD命令执行与获得返回信息
- 白话C++(全)
- C++标准库第1、2
- 大数类c++大数类
- C++语言编写串口调试助手
- c++素数筛选法
- C++ mqtt 用法
- 商品库存管理系统 C++ MFC
- c++ 多功能计算器
- C++17 In Detail
- 嵌入式QtC++编程课件
- 颜色识别形状识别STM103嵌入式代码
- c++ 邮件多附件群发
- c++ 透明代理(hookproxy)
- mfc 调用redis
- FTP客户端源码(c++)
- c++ 画图(14Qt-XPS)
- c++多边形交并差运算
川公网安备 51152502000135号
评论
共有 条评论