okay im working on a shapes hierarchy and i want to know if u can find whats my problem in my program. im thinking it might be my virtual void function or its in the main. this is what i have to do ,create a program that uses vector of shapes pointers to objects of each concrete class in the hierarchy, the program should print the object to which each vector element points. this is what i have so far:
Code:#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
class 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
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
class Square : public TwoDimensionalShape{
public:
Square(int Length): TwoDimensionalShape(Length,Length){}
virtual ~Square(){}
virtual double getArea()const=0;
protected:
int Length;
};
class Circle : public TwoDimensionalShape{
public:
Circle(int radius): TwoDimensionalShape(radius,radius){}
virtual ~Circle(){}
virtual double getArea()const=0;
protected:
int radius;
};
class Triangle : public TwoDimensionalShape{
public:
Triangle(int base,int height): TwoDimensionalShape(base,height){}
virtual ~Triangle(){}
virtual double getArea()const=0;
protected:
int base;
int height;
};
int main(){
vector <Shape*> shapes(3);
shapes[0]= new Square(4);
shapes[1]= new Circle(3);
shapes[2]= new Triangle(2,4);
return 0;
}
double Square::getArea() const
{
return Length*Length;
}
double Circle::getArea() const
{
return 3.14 * radius*radius;
}
double Triangle::getArea() const
{
return (base * height)/2;
}
void Shape::print() const {
cout << "Area is: " << pCircle->getArea() << endl;
cout << "Area is: " << pSquare->getArea() << endl;
cout << "Area is: " << pTriangle->getArea() << endl;
}
