// Exercise 9.12 Solution: Point.h#ifndefPOINT_H#definePOINT_HclassPoint{public:Point(double=0.0,double=0.0);// default constructor// set and get functionsvoidsetX(double);voidsetY(double);doublegetX();doublegetY();private:double x;// 0.0 <= x <= 20.0double y;// 0.0 <= y <= 20.0};// end class Point #endif
// Exercise 9.12 Solution: Rectangle.h#ifndefRECTANGLE_H#defineRECTANGLE_H#include"Point.h"// include definition of class PointclassRectangle{public:// default constructorRectangle( Point =Point(0.0,1.0), Point =Point(1.0,1.0),
Point =Point(1.0,0.0), Point =Point(0.0,0.0));// sets x, y, x2, y2 coordinatesvoidsetCoord( Point, Point, Point, Point );doublelength();// lengthdoublewidth();// widthvoidperimeter();// perimetervoidarea();// areaboolsquare();// squareprivate:
Point point1;
Point point2;
Point point3;
Point point4;};// end class Rectangle #endif
// Exercise 9.12 Solution: Point.cpp// Member-function definitions for class Point.#include<stdexcept>#include"Point.h"// include definition of class Pointusingnamespace std;Point::Point(double xCoord,double yCoord ){setX( xCoord );// invoke function setXsetY( yCoord );// invoke function setY}// end Point constructor// set x coordinatevoidPoint::setX(double xCoord ){if( xCoord >=0.0&& xCoord <=20.0)
x = xCoord;elsethrowinvalid_argument("x must be >= 0.0 and <= 20.0");}// end function setX// set y coordinatevoidPoint::setY(double yCoord ){if( yCoord >=0.0&& yCoord <=20.0)
y = yCoord;elsethrowinvalid_argument("y must be >= 0.0 and <= 20.0");}// end function setY// return x coordinatedoublePoint::getX(){return x;}// end function getX// return y coordinatedoublePoint::getY(){return y;}// end function getY
// Exercise 9.12 Solution: Rectangle.cpp// Member-function definitions for class Rectangle.#include<iostream>#include<iomanip>#include<cmath>#include<stdexcept>#include"Rectangle.h"// include definition of class Rectangleusingnamespace std;Rectangle::Rectangle( Point a, Point b, Point c, Point d ){setCoord( a, b, c, d );// invokes function setCoord}// end Rectangle constructorvoidRectangle::setCoord( Point p1, Point p2, Point p3, Point p4 ){//使用成员对象来初始化// Arrangement of points// p4.........p3// . .// . .// p1.........p2// verify that points form a rectangleif(( p1.getY()== p2.getY()&& p1.getX()== p4.getX()&& p2.getX()== p3.getX()&& p3.getY()== p4.getY())){
point1 = p1;
point2 = p2;
point3 = p3;
point4 = p4;}// end ifelsethrowinvalid_argument("Coordinates do not form a rectangle!\n");}// end function setCoorddoubleRectangle::length(){double side1 =fabs( point4.getY()- point1.getY());// get side1double side2 =fabs( point2.getX()- point1.getX());// get side2double length =( side1 > side2 ? side1 : side2 );return length;}// end function lengthdoubleRectangle::width(){double side1 =fabs( point4.getY()- point1.getY());// get side1double side2 =fabs( point2.getX()- point1.getX());// get side2double width =( side1 < side2 ? side1 : side2 );return width;}// end function widthvoidRectangle::perimeter(){
cout << fixed <<"\nThe perimeter is: "<<setprecision(1)<<2*(length()+width())<< endl;}// end function perimetervoidRectangle::area(){
cout << fixed <<"The area is: "<<setprecision(1)<<length()*width()<< endl;}// end function areaboolRectangle::square(){returnlength()==width();}// end function square/*1、使用成员对象来初始化*/
// Exercise 9.12 Solution: Ex09_12.cpp#include<iostream>#include<stdexcept>#include"Rectangle.h"// include definition of class Rectangleusingnamespace std;intmain(){
Point w(1.0,1.0);
Point x(5.0,1.0);
Point y(5.0,3.0);
Point z(1.0,3.0);
Point j(0.0,0.0);
Point k(1.0,0.0);
Point m(1.0,1.0);
Point n(0.0,1.0);
Rectangle r1( z, y, x, w );
cout <<"Rectangle 1:\n";
cout <<"length = "<< r1.length();
cout <<"\nwidth = "<< r1.width();
r1.perimeter();
r1.area();
cout <<"The rectangle "<<( r1.square()?"is":"is not")<<" a square.\n";
Rectangle r2( j, k, m, n );
cout <<"\nRectangle 2:\n";
cout <<"length = "<< r2.length();
cout <<"\nwidth = "<< r2.width();
r2.perimeter();
r2.area();
cout <<"The rectangle "<<( r2.square()?"is":"is not")<<" a square.\n";try{
Rectangle r3( w, x, m, n );}catch( invalid_argument &e ){
cout <<"\nNot a valid Rectangle"<< endl;}try{
Point v(99.0,-2.3);}catch( invalid_argument &e ){
cout <<"Not a valid Point"<< endl;}}// end main