Confused by Assertion Errors.
I'm currently writing a basic program with 6 classes, Shape, TwoDimensionalShape, ThreeDimensionalShape, Square, Circle, Cube. They inherit from one another, square and circle from two dimensional, and two dimensional from Shape.
In the execution of the main program I'm getting an assertion error. After some reading I see that the compiler is throwing an error because it got an unexpected input. Im still confused where I went wrong, long code segment below. Any tips on proper indenting and what not to make it more readable is appreciated.
Code:
#include<iostream>
#include<string>
using namespace std;
class Shape{
public:
double length, width;
char* type;
Shape::Shape(double l, double w)
{
length = l;//basic constructor
width = w;
type = "undefined";
}
Shape::~Shape()
{
delete type;//delete the pointer variable so it isnt randomly at memory.
}
double Shape:: getArea()
{
return 0.0;//to be overridden in every child class that uses it.
}
double Shape :: getVolume()
{
return 0.0;//to be overridden in every child class that uses it.
}
char* Shape::getShapeType()
{
return type;
}
int Shape::getType()
{
if(strcmp(type, "Square") || strcmp(type, "Circle"))
{
return 0;/*Utilized strings instead of number type identifying, didnt see the reason.
Square and Circle are the only 2 dimensional shapes created.*/
}
else return 1;
}
};
class TwoDimensionalShape : virtual public Shape{
public:
double area;
TwoDimensionalShape::TwoDimensionalShape()
{
length = 0;
type = "TwoDimensionalShape";
}
TwoDimensionalShape::~TwoDimensionalShape(){delete type;}
};
class ThreeDimensionalShape : virtual public Shape{
protected:
double volume;
ThreeDimensionalShape::ThreeDimensionalShape()
{
volume = 0;
type = "ThreeDimensionalShape";
}
ThreeDimensionalShape::~ThreeDimensionalShape(){delete type;}
};
class Square : public TwoDimensionalShape
{
public:
double sideLength;
Square::Square(double a)
{
sideLength = a;
type = "Square";
}
Square::~Square(){delete type;}
double Square::getArea()
{
return (sideLength * sideLength);
}
};
class Circle: public TwoDimensionalShape
{
public:
double radius, pi;
Circle::Circle(double r)
{
radius = r;
pi = 3.14;
type = "Circle";
}
Circle::~Circle(){delete type;}
double Circle::getArea()
{
return (pi *(radius * radius));
}
};
class Cube: public ThreeDimensionalShape
{
public:
double length;
Cube::Cube(double l)
{
length = l;
type = "Cube";
}
Cube::~Cube(){delete type;};
double Cube:: getVolume()
{
return (length * length * length);
}
};
int main()
{
Shape * s[4];
Square s1(2.1);
s[0]= &s1;
s[1]=new Circle(3.0);
s[2]=new Cube(2.0);
s[3]=new Square(6.0);
for(int i = 0 ; i < 4 ; i++){
if(s[i]->getType() == 0)
cout<<"Type=" <<s[i]->getShapeType()<<"\t area="<< s[i]->getArea()<<endl;
else
cout<<"Type=" <<s[i]->getShapeType()<<"\t volume"<< s[i]->getVolume()<<endl;
}
return 0;
}