fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class Polygon
  7. {
  8. protected:
  9. int width, height;
  10. public:
  11. Polygon (int a, int b) : width(a), height(b) {}
  12. virtual ~Polygon() { }
  13. virtual int area() = 0;
  14. };
  15.  
  16. class Output
  17. {
  18. private:
  19. std::string myString;
  20.  
  21. public:
  22. Output() { }
  23.  
  24. Output(const string& s) : myString(s) { }
  25.  
  26. // This seems to work, but ther core dump happens right afterwards.
  27. virtual ~Output() { }
  28. virtual int area() = 0;
  29. void print () {
  30. cout << myString << this->area() << '\n';
  31. }
  32. };
  33.  
  34.  
  35.  
  36. class Rectangle: public Polygon, public Output
  37. {
  38. public:
  39. Rectangle (int a, int b) : Polygon(a,b), Output{"A Rectangle's area is: "} {}
  40. int area () {
  41. return width*height;
  42. }
  43. };
  44.  
  45. class Triangle: public Polygon, public Output
  46. {
  47. public:
  48. Triangle (int a, int b) : Polygon{a,b}, Output{"A Triangle's area is: "} {}
  49. int area ()
  50. { return width*height/2; }
  51. };
  52.  
  53. int main ()
  54. {
  55. Output * ptr1 = new Rectangle(4,5);
  56. Output * ptr2 = new Triangle(4,5);
  57.  
  58. ptr1->print();
  59. ptr2->print();
  60.  
  61. // Causes core dump.
  62. delete ptr1;
  63. delete ptr2;
  64.  
  65. return 0;
  66. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
A Rectangle's area is: 20
A Triangle's area is: 10