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);
}

This is what I see when I compile it? Shapes__ description: square, length:-9.25596e+061 Area:8.56729e+123

Your Square class inherits from Rectangle and sets the values for the side lengths of the rectagle class in it's constructor, but not its own lSide variable. You have also re-implemented the functions for the Shape interface in the Square class, which you don't need to, since their implementation is the same as Rectangle. However, since you've provided them then they're used and they're doing their calculation using the uninitialised lSide variable, so you get some random result.

Basically, your Square class needs nothing but a constructor that takes a single argument and passes it to the Rectangle class:

class Square : public Rectangle
{
public :
    Square( double size ) : Rectangle( size, size ) {}
};

That should be all you need, since all the other bits are inherited from Rectangle.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.