I am working on C++ coding for Shape.cpp and in the square I am getting scientific notation as the for the area and perimeter. Can anyone please tell me what I am doing wrong. Thank you. Here is my code
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using std::ostringstream;
using std::ostream;
using std::vector;
using std::string;
using std::cout;
using std::endl;
/****************************************************************
* Our base class: Shape, all methods are pure virtual
* and must be supplied by subclasses
****************************************************************/
class Shape
{
public:
virtual double getArea() const = 0;
virtual double getPerimeter() const = 0;
virtual string toString() const = 0;
};
// ------------------------------------------
// Set up an '<<' operator for Shape so
// you can do: cout << theShape << endl;
// ------------------------------------------
ostream &operator<<(ostream &out, const Shape &shape)
{
return(out << shape.toString());
}
// --------------------------------
// Circle, subclass of Shape
// --------------------------------
class Circle: public Shape
{
public:
Circle(double radius) : mRadius(radius) {}
double getArea() const;
double getPerimeter() const;
string toString() const;
static double PI;
private:
double mRadius;
};
// --------------------------------
// Initialize the constant PI
// and supply the 3 required
// methods: getArea(),
// getPerimeter() and toString()
// --------------------------------
double Circle::PI = 3.14159265358979323846;
double Circle::getArea() const
{
return(PI * mRadius * mRadius);
}
double Circle::getPerimeter() const
{
return(PI * 2.0 * mRadius);
}
string Circle::toString() const
{
ostringstream out;
out << "Circle, radius: " << mRadius;
return(out.str());
}
// --------------------------------
// Rectangle, subclass of Shape
// --------------------------------
class Rectangle: public Shape
{
public:
Rectangle(double width, double height) :
mWidth(width), mHeight(height) {}
double getArea() const;
double getPerimeter() const;
string toString() const;
private:
double mWidth;
double mHeight;
};
// --------------------------------
// Supply the 3 required methods:
// getArea(), getPerimeter()
// and toString()
// --------------------------------
double Rectangle::getArea() const
{
return(mWidth * mHeight);
}
double Rectangle::getPerimeter() const
{
return(2.0 * (mWidth + mHeight));
}
string Rectangle::toString() const
{
ostringstream out;
out << "Rectangle, width: " << mWidth << ", height: " << mHeight;
return(out.str());
}
class Square : public Rectangle
{
public:
Square( double side=0.0 )
: Rectangle( side, side ) { }
double getArea() const;
double getPerimeter() const;
string toString() const;
double lSide;
};
double Square::getArea() const
{
return(lSide * lSide);
}
double Square::getPerimeter() const
{
return(4 * lSide);
}
string Square::toString() const
{
ostringstream out;
out << "Square, length: " << lSide;
return(out.str());
}
static void handleShape(const Shape &theShape)
{
cout << "Description: " << theShape << endl;
cout << "Area: " << theShape.getArea() << endl;
cout << "Perimeter " << theShape.getPerimeter() << endl;
cout << endl;
}
static void sortShapes(vector<Shape *> &shapes)
{
int n = shapes.size();
for(int i = 0; i < n - 1; i ++)
{
for(int j = 0; j < n - i - 1; j ++)
{
if(shapes[j]->getArea() > shapes[j + 1]->getArea())
{
Shape *ptr = shapes[j];
shapes[j] = shapes[j + 1];
shapes[j + 1] = ptr;
}
}
}
}
int main()
{
Circle c1(1.0);
Circle c2(10.0);
Rectangle r1(2.0, 3.0);
Rectangle r2(20.0, 30.0);
Square s1(1.0);
Square s2(10.0);
cout << "Shapes" << endl;
cout << "------" << endl << endl;
handleShape(c1);
handleShape(c2);
handleShape(r1);
handleShape(r2);
handleShape(s1);
handleShape(s2);
// Now, illustrate how we can sort by area even though
// we're dealing with differing types of objects (Circle,
// Rectangle, Square)
vector<Shape *> shapes;
shapes.push_back(&c1);
shapes.push_back(&c2);
shapes.push_back(&r1);
shapes.push_back(&r2);
shapes.push_back(&s1);
shapes.push_back(&s2);
cout << "Sorted By Area" << endl;
cout << "--------------" << endl << endl;
sortShapes(shapes);
for(int i = 0, n = shapes.size(); i < n; i ++)
handleShape(*shapes[i]);
system("PAUSE");
return(0);
}