Lab3 Shape
来源:互联网 发布:淘宝的alexa排名是多少 编辑:程序博客网 时间:2024/06/04 22:45
(Shape Hierarchy) Implement theShape hierarchy designed inExercise 12.7 (which is based on the hierarchy inFig. 12.3). EachTwoDimensionalShape should contain functiongetArea to calculate the area of the two-dimensional shape. EachThreeDimensionalShape should have member functionsgetArea andgetVolume to calculate the surface area and volume of the three-dimensional shape, respectively. Create a program that uses avector ofShape pointers to objects of each concrete class in the hierarchy. The program should print the object to which each vector element points. Also, in the loop that processes all the shapes in thevector, determine whether each shape is aTwoDimensionalShape or aThreeDimensionalShape. If a shape is aTwoDimensionalShape, display its area. If a shape is aThreeDimensionalShape, display its area and volume.
#ifndef SHAPE_H#define SHAPE_H#include <iostream>using std::ostream;class Shape { friend ostream & operator<<( ostream &, Shape & );public: Shape( double = 0.0, double = 0.0 ); // default constructor double getCenterX() const; // return x from coordinate pair double getCenterY() const; // return y from coordinate pair virtual void print() const = 0; // output Shape objectprotected: double xCenter; // x part of coordinate pair double yCenter; // y part of coordinate pair}; // end class Shape#endif
#include "Shape.h"// default constructorShape::Shape( double x, double y ){ xCenter = x; yCenter = y;} // end Shape constructor// return x from coordinate pairdouble Shape::getCenterX() const { return xCenter; } // end function getCenterX// return y from coordinate pairdouble Shape::getCenterY() const { return yCenter; } // end function getCenterY// overloaded output operatorostream & operator<<( ostream &out, Shape &s ){ s.print(); return out;} // end overloaded output operator function
#ifndef TWODIM_H#define TWODIM_H#include "Shape.h"class TwoDimensionalShape : public Shape {public: // default constructor TwoDimensionalShape( double x, double y ) : Shape( x, y ) { } virtual double getArea() const = 0; // area of TwoDimensionalShape}; // end class TwoDimensionalShape#endif
#ifndef THREEDIM_H#define THREEDIM_H#include "Shape.h"class ThreeDimensionalShape : public Shape {public: // default constructor ThreeDimensionalShape( double x, double y ) : Shape( x, y ) { } virtual double getArea() const = 0; // area of 3-dimensional shape virtual double getVolume() const = 0; // volume of 3-dimensional shape}; // end class ThreeDimensionalShape#endif
#include "TwoDimensionalShape.h"class Circle : public TwoDimensionalShape {public: // default consturctor Circle( double = 0.0, double = 0.0, double = 0.0 ); virtual double getRadius() const; // return radius virtual double getArea() const; // return area void print() const; // output Circle objectprivate: double radius; // Circle's radius}; // end class Circle#endif
#include <iostream>using std::cout;#include "Circle.h"// default constructorCircle::Circle( double r, double x, double y ) : TwoDimensionalShape( x, y ) { radius = ( ( r > 0.0 ) ? r : 0.0 ); } // end Circle constructor// return radius of circle objectdouble Circle::getRadius() const { return radius; } // end function getRadius// return area of circle objectdouble Circle::getArea() const { return 3.14159 * radius * radius; } // end function getArea// output circle objectvoid Circle::print() const{ cout << "Circle with radius " << radius << "; center at (" << xCenter << ", " << yCenter << ")";} // end function print
#ifndef SQUARE_H#define SQUARE_H#include "TwoDimensionalShape.h"class Square : public TwoDimensionalShape {public: // default constructor Square( double = 0.0, double = 0.0, double = 0.0 ); virtual double getSideLength() const; // return lenght of sides virtual double getArea() const; // return area of Square void print() const; // output Square objectprivate: double sideLength; // length of sides}; // end class Square#endif
#include <iostream>using std::cout;#include "Square.h"// default constructorSquare::Square( double s, double x, double y ) : TwoDimensionalShape( x, y ) { sideLength = ( ( s > 0.0 ) ? s : 0.0 );} // end Square constructor// return side of Squaredouble Square::getSideLength() const { return sideLength; } // end function getSideLength// return area of Squaredouble Square::getArea() const { return sideLength * sideLength; } // end function getArea// output Square objectvoid Square::print() const{ cout << "Square with side length " << sideLength << "; center at (" << xCenter << ", " << yCenter << ")";} // end function print
#include "ThreeDimensionalShape.h"class Cube : public ThreeDimensionalShape {public: // default constructor Cube( double = 0.0, double = 0.0, double = 0.0 ); virtual double getArea() const; // return area of Cube object virtual double getVolume() const; // return volume of Cube object double getSideLength() const; // return length of sides void print() const; // output Cube objectprivate: double sideLength; // length of sides of Cube}; // end class Cube#endif
#include <iostream>using std::cout;#include "Cube.h"// default constructorCube::Cube( double s, double x, double y ) : ThreeDimensionalShape( x, y ) { sideLength = ( ( s > 0.0 ) ? s : 0.0 ); } // end Cube constructor// return area of Cubedouble Cube::getArea() const { return 6 * sideLength * sideLength; } // end function getArea// return volume of Cubedouble Cube::getVolume() const{ return sideLength * sideLength * sideLength; } // end function getVolume// return length of sidesdouble Cube::getSideLength() const { return sideLength; } // end function getSideLength// output Cube objectvoid Cube::print() const{ cout << "Cube with side length " << sideLength << "; center at (" << xCenter << ", " << yCenter << ")";} // end function print
#ifndef SPHERE_H#define SPHERE_H#include "ThreeDimensionalShape.h"class Sphere : public ThreeDimensionalShape {public: // default constructor Sphere( double = 0.0, double = 0.0, double = 0.0 ); virtual double getArea() const; // return area of Sphere virtual double getVolume() const; // return volume of Sphere double getRadius() const; // return radius of Sphere void print() const; // output Sphere objectprivate: double radius; // radius of Sphere}; // end class Sphere#endif
#include <iostream>using std::cout;#include "Square.h"// default constructorSquare::Square( double s, double x, double y ) : TwoDimensionalShape( x, y ) { sideLength = ( ( s > 0.0 ) ? s : 0.0 );} // end Square constructor// return side of Squaredouble Square::getSideLength() const { return sideLength; } // end function getSideLength// return area of Squaredouble Square::getArea() const { return sideLength * sideLength; } // end function getArea// output Square objectvoid Square::print() const{ cout << "Square with side length " << sideLength << "; center at (" << xCenter << ", " << yCenter << ")";} // end function print
- Lab3 Shape
- lab3
- Lab3
- Lab3
- lab3
- ucore lab3
- ucore lab3
- DS-lab3
- shape
- Shape
- shape
- shape
- Shape
- shape
- shape
- shape
- shape
- shape
- jquery选择器
- mysql SUM聚合运算的返回值为MYSQL_TYPE_NEWDECIMAL
- C++ : 成员函数的地址
- A* obj=new C(); obj->fun(); class C:public B { private: virtual fun()
- Thrift:可扩展的跨语言服务实现
- Lab3 Shape
- ASP.NET_登录例子
- Linux环境中USB转串口的应用(minicom)
- mode_t
- 第六周实验报告(一)
- Android中的网络时间同步
- C# 时间差 TimeSpan
- CharSequence类型
- u-boot for tiny210 ver2.2.2(by liukun321咕唧咕唧)