Lab3 Shape

(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 object
protected:
   double xCenter; // x part of coordinate pair
   double yCenter; // y part of coordinate pair
}; // end class Shape

#endif

#include "Shape.h"

// default constructor
Shape::Shape( double x, double y )
{
   xCenter = x;
   yCenter = y;
} // end Shape constructor

// return x from coordinate pair
double Shape::getCenterX() const 
{ 
   return xCenter; 
} // end function getCenterX

// return y from coordinate pair
double Shape::getCenterY() const 
{ 
   return yCenter; 
} // end function getCenterY

// overloaded output operator
ostream & 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 object
private:
   double radius; // Circle's radius
}; // end class Circle

#endif

#include <iostream>
using std::cout;

#include "Circle.h"

// default constructor
Circle::Circle( double r, double x, double y )
   : TwoDimensionalShape( x, y ) 
{ 
   radius = ( ( r > 0.0 ) ? r : 0.0 ); 
} // end Circle constructor

// return radius of circle object
double Circle::getRadius() const 
{ 
   return radius; 
} // end function getRadius

// return area of circle object
double Circle::getArea() const 
{ 
   return 3.14159 * radius * radius; 
} // end function getArea

// output circle object
void 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 object
private:
   double sideLength; // length of sides
}; // end class Square

#endif

#include <iostream>
using std::cout;

#include "Square.h"

// default constructor
Square::Square( double s, double x, double y )
   : TwoDimensionalShape( x, y ) 
{ 
   sideLength = ( ( s > 0.0 ) ? s : 0.0 );
} // end Square constructor

// return side of Square
double Square::getSideLength() const 
{ 
   return sideLength; 
} // end function getSideLength

// return area of Square
double Square::getArea() const 
{ 
   return sideLength * sideLength; 
} // end function getArea

// output Square object
void 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 object
private:
   double sideLength; // length of sides of Cube
}; // end class Cube

#endif

#include <iostream>
using std::cout;

#include "Cube.h"

// default constructor
Cube::Cube( double s, double x, double y )
   : ThreeDimensionalShape( x, y ) 
{ 
   sideLength = ( ( s > 0.0 ) ? s : 0.0 ); 
} // end Cube constructor

// return area of Cube
double Cube::getArea() const 
{ 
   return 6 * sideLength * sideLength; 
} // end function getArea

// return volume of Cube
double Cube::getVolume() const
{ 
   return sideLength * sideLength * sideLength; 
} // end function getVolume

// return length of sides
double Cube::getSideLength() const 
{ 
   return sideLength; 
} // end function getSideLength

// output Cube object
void 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 object
private:
   double radius; // radius of Sphere
}; // end class Sphere

#endif

#include <iostream>
using std::cout;

#include "Square.h"

// default constructor
Square::Square( double s, double x, double y )
   : TwoDimensionalShape( x, y ) 
{ 
   sideLength = ( ( s > 0.0 ) ? s : 0.0 );
} // end Square constructor

// return side of Square
double Square::getSideLength() const 
{ 
   return sideLength; 
} // end function getSideLength

// return area of Square
double Square::getArea() const 
{ 
   return sideLength * sideLength; 
} // end function getArea

// output Square object
void Square::print() const
{
   cout << "Square with side length " << sideLength << "; center at ("
      << xCenter << ", " << yCenter << ")";
} // end function print


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值