There are several major issues with this code:
First, you should not use multiple inheritance for this. It is completely unnecessary and will lead to very difficult to track down bugs.
Second, you do not need to test for nullptr
before deleting a pointer - it is redundant as delete
already does that.
Third, neither of your base classes has a virtual destructor. (your current error)
Forth, you are violating the Rule of 3 in your Output
class (and likely will need to use it in all of them).
Fifth, assuming string
means std::string
. There is no reason for it to be a string*
- just use std::string
and avoid having to allocate and deallocate it.
I didn't fix your design issues, but your memory access and polymorphic issues are fixed here.
#include <iostream>
#include <string>
using namespace std;
class Polygon
{
protected:
int width, height;
public:
Polygon (int a, int b) : width(a), height(b) {}
virtual ~Polygon() { } // needed!
virtual int area() = 0;
};
class Output
{
private:
std::string myString; // no need to be a pointer
public:
Output() { }
Output(const string& s) : myString(s) { }
virtual ~Output() { } // also needed!
virtual int area() = 0;
void print () {
cout << myString << this->area() << '\n';
}
};
class Rectangle: public Polygon, public Output
{
public:
Rectangle (int a, int b) : Polygon(a,b), Output{"A Rectangle's area is: "} {}
int area () {
return width*height;
}
};
class Triangle: public Polygon, public Output
{
public:
Triangle (int a, int b) : Polygon{a,b}, Output{"A Triangle's area is: "} {}
int area ()
{ return width*height/2; }
};
int main ()
{
Output * ptr1 = new Rectangle(4,5);
Output * ptr2 = new Triangle(4,5);
ptr1->print();
ptr2->print();
// Causes core dump.
delete ptr1;
delete ptr2;
return 0;
}
EDIT: An example of a better way to implement the desired program can be found here.