Confused by Assertion Errors.

This is a discussion on Confused by Assertion Errors. within the C++ Programming forums, part of the General Programming Boards category; I'm currently writing a basic program with 6 classes, Shape, TwoDimensionalShape, ThreeDimensionalShape, Square, Circle, Cube. They inherit from one another, ...

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.

At a glance, the problem probably as to do with the type member variable. You appear to be setting it to point to a string literal, and then attempt to use delete on it. I suggest that you simply make it a std::string and get rid of that delete. You could also turn it into an enum... or simply get rid of it entirely and make use of polymorphism.

I thought I was using polymorphism, when I just use the same instance of the type string for all my subclasses. Is that wrong?

Not necessarily wrong, but such type checking tends to break the Open-Closed principle: software entities (classes and functions, in C++'s context) should be open for extension but closed for modification. If you add a new derived class, the code in main() must change (and the code in Shape::getType() as well). Sure, you say that "Square and Circle are the only 2 dimensional shapes created", but that kind of assumption can be rather fragile.

Basically, the problem is that if the object is 2D shape, you want to get the area, but if the object is a 3D shape, you want to get the volume. One way I can think of is to have a virtual function named getQuantity() that returns the quantitative measure of the shape, be it area, volume, or some other measure devised for shapes of higher dimensions:

Notice that I made Shape into an abstract base class. There is no way of computing the quantitative measure of a Shape, hence getQuantity() is pure virtual. The operator<< will be used to print the shape. Notice that it calls print(), which is pure virtual. This allows derived classes to determine what to print.

Oh, and the destructor is virtual - base class destructors should be virtual in order to avoid undefined behaviour if you should delete a derived class object through a base class pointer.

Now, a TwoDimensionalShape is-a Shape, but it is also an abstract base class:

I have chosen to demonstrate the polymorphic nature of getQuantity() in the above code, though it is not actually part of your desired output. Notice that if you add say, a Rectangle class, the only code that needs to be added is the code for the Rectange class. This is the Open-Closed principle at work.

Last edited by laserlight; 11-19-2008 at 03:16 PM.
Reason: getQuantity() can be implemented with getArea() in TwoDimensionalShape.

The basica rule that you must ALWAYS follow when it comes to memory allocation is that you must delete what you new (and only what you new). You didn't use new on 'type', therefore you don't use delete.
In fact you don't even need destructors at all!
Going even furthur, you don't even need that 'type' variable at all either. Just make a virtual function that returns the name of the class, like it was a property.

Just make a virtual function that returns the name of the class, like it was a property.

I considered that, but it will not be enough for ASURugger: the text to be printed includes "area" and "volume", which in turn depends on whether the underlying Shape object is a TwoDimensionalShape or a ThreeDimensionalShape, thus a dynamic_cast would still be needed. Using a pure virtual print function works around this problem.