• C++程序设计(第2版)课后习题答案第14章


    14.5

    View Code
      1 /*
      2 author:shajin
      3 date:2013-4-6
      4 */
      5 #include <iostream>
      6 #include<math.h>
      7 using namespace std;
      8 class Shape
      9 {
     10 public:
     11     double area();
     12     double girth();
     13     void show();
     14 };
     15 double Shape::area()
     16 {
     17     return 0;
     18 }
     19 
     20 double Shape::girth()
     21 {
     22     return 0.0;
     23 }
     24 
     25 void Shape::show()
     26 {
     27     cout<<"Shape Object:"<<endl;
     28 }
     29 
     30 class Circle:public Shape
     31 {
     32 private:
     33     double radius;
     34 public:
     35     Circle(double radius=5.0);
     36     double area();
     37     double girth();
     38     void show();
     39 };
     40 
     41 
     42 Circle::Circle(double r)
     43 {
     44     radius=r;
     45 }
     46 
     47 double Circle::area()
     48 {
     49     return 3.14*radius*radius;
     50 }
     51 
     52 void Circle::show()
     53 {
     54     cout<<"Circle:"<<radius<<endl;
     55     cout<<"面积"<<area()<<endl;
     56     cout<<"周长"<<girth()<<endl;
     57 }
     58 
     59 double Circle::girth()
     60 {
     61     return 2*3.14*radius;
     62 }
     63 
     64 class Triangle:public Shape
     65 {
     66 private:
     67     double a,b,c;//三角形三边
     68 public:
     69     Triangle(double a=3.0,double b=4.0,double c=5.0);
     70     //需要判断是否能构成三角形
     71     double area();
     72     double girth();
     73     void show();
     74 };
     75 Triangle::Triangle(double a,double b,double c)
     76 {
     77     this->a=a;
     78     this->b=b;
     79     this->c=c;
     80 }
     81 double Triangle::area()
     82 {
     83     double s=(a+b+c)/2;
     84     return sqrt(s*(s-a)*(s-b)*(s-c));
     85 }
     86 double Triangle::girth()
     87 {
     88     return a+b+c;
     89 }
     90 void Triangle::show()
     91 {
     92     cout<<"Triangle:"<<a<<b<<c<<endl;
     93     cout<<"面积"<<area()<<endl;
     94     cout<<"周长"<<girth()<<endl;
     95 }
     96 
     97 
     98 class Rectangle:public Shape
     99 {
    100 private:
    101     double width,height;
    102 public:
    103     Rectangle(double w=1.0,double h=1.0);
    104     double area();
    105     double girth();
    106     void show();
    107 };
    108 Rectangle::Rectangle(double w,double h)
    109 {
    110     width=w;
    111     height=h;
    112 }
    113 double Rectangle::area()
    114 {
    115     return width*height;
    116 }
    117 double Rectangle::girth()
    118 {
    119     return 2*(width+height);
    120 }
    121 void Rectangle::show()
    122 {
    123     cout<<"Rectangle:"<<width<<height<<endl;
    124     cout<<"面积"<<area()<<endl;
    125     cout<<"周长"<<girth()<<endl;
    126 }
    127 void main()
    128 {
    129     Circle c(1);
    130     c.show();
    131 
    132     Triangle t;
    133     t.show();
    134 
    135     Rectangle r(3,4);
    136     r.show();
    137 }

    14.6

    View Code
     1 // 文件point.h: 类Point的定义
     2 #if !defined __POINT__H__
     3 #define __POINT__H__
     4 
     5 #include <iostream.h>
     6 
     7 class Point
     8 {
     9     friend ostream &operator << (ostream &, const Point &);
    10 public:
    11     // 重载的构造函数
    12     Point(double = 0, double = 0);
    13     Point(const Point &p); // 复制构造函数
    14 
    15     // 析构函数
    16     ~Point();
    17 
    18     // 重载的定值函数
    19     void setPoint(double a, double b);
    20     void setPoint(Point &p);
    21 
    22     // 取值函数
    23     double getX()const
    24     {
    25         return x;
    26     } double getY()const
    27     {
    28         return y;
    29     }
    30 
    31 private:
    32     double x, y;
    33 };
    34 
    35 #endif
    View Code
     1 // 文件point.cpp: 类Point的实现
     2 #include "point.h"
     3 
     4 Point::Point(double a, double b)
     5 {
     6     x = a;
     7     y = b;
     8 }
     9 
    10 Point::Point(const Point &p)
    11 {
    12     x = p.getX();
    13     y = p.getY();
    14 }
    15 
    16 Point::~Point(){}
    17 
    18 void Point::setPoint(double a, double b)
    19 {
    20     x = a;
    21     y = b;
    22 }
    23 
    24 void Point::setPoint(Point &p)
    25 {
    26     x = p.getX();
    27     y = p.getY();
    28 }
    29 
    30 ostream &operator << (ostream &os, const Point &point)
    31 {
    32     os << "(" << point.x << ", " << point.y << ")";
    33     return os;
    34 }
    View Code
     1 // 文件line.h: 类Line的定义
     2 #if !defined __LINE__H__
     3 #define __LINE__H__
     4 
     5 #include <iostream.h>
     6 #include "point.h"
     7 
     8 class Line {
     9     friend ostream & operator<< (ostream &, const Line &);
    10 public:
    11     // 重载的构造函数
    12     Line(double startX = 0, double startY = 0, double endX = 0, double endY = 0);
    13     Line(Point start, Point end);
    14     Line(Line &line);    //复制构造函数
    15 
    16     // 析构函数
    17     ~Line();
    18 
    19     // 重载的定值函数
    20     void setLine(double startX = 0, double startY = 0, double endX = 0, double endY = 0);
    21     void setLine(Point , Point );
    22 
    23     double getLength() const; // 计算线段的长度
    24 
    25     // 取值函数
    26     Point getStartPoint()  {return startPoint;}
    27     Point getEndPoint()  {return endPoint;}
    28 
    29 private:
    30     Point startPoint, endPoint;
    31 };
    32 
    33 #endif
  • 相关阅读:
    SlipHover,能感知鼠标方向的图片遮罩效果jQuery插件
    jQuery插件开发精品教程,让你的jQuery提升一个台阶
    HTML5打造的炫酷本地音乐播放器-喵喵Player
    无论何时,记得做好代码的清理工作
    statcounter统计的浏览器市场占有率
    开大你的音响,感受HTML5 Audio API带来的视听盛宴
    requestAnimationFrame,Web中写动画的另一种选择
    好的用户界面-界面设计的一些技巧
    Windows上帝模式,上帝应该就是这样使用Windows的
    JavaScript字符转Unicode,顺便说句:GitHub的Oh no页面很亮
  • 原文地址:https://www.cnblogs.com/shajianheng/p/3013812.html
Copyright © 2020-2023  润新知