0

I'm having trouble with a basic polymorphism feature. I have a base class with a draw function and two derived classes each with a draw function implemented as well. I'd like to use a base class pointer pointing to a derived class, to use the base class's implementation of the draw function. Then I would like to use the derived classes draw function.

#include <iostream>
#include <string>
#include <fstream>
#ifndef SHAPEHEADER_H
#define SHAPEHEADER_H

using namespace std;

class Shape {
public:
Shape(float, float);
virtual ~Shape() = default;
virtual float draw() const { return 94.9; };

private:
float a;
float b;
};
#endif // !SHAPEHEADER_H


#include <iostream>
#include <string>
#include <fstream>
#include "ShapeHeader.h"
#ifndef CIRCLEHEADER_H
#define CIRCLEHEADER_H

using namespace std;

class Circle : public Shape{
public:
Circle(float);
float draw() const override { return pi * radius*radius; };

private:
float radius;
static float pi;
};
#endif // !SHAPEHEADER_H

Shape s0(1.0, 1.0);
Circle c0(1.0);
Square sq0(3.0, 4.0);

Shape *shape0 = &s0;
Shape *shape1 = &c0;
Circle *circle0 = &c0;

cout << "Shape:" << shape0->draw() << endl;
cout << "Circle:" << circle0->draw() << endl;
system("PAUSE");


cout << "Pointing to circle and square with shape pointer" << endl;
cout << "Circle:" << shape1->draw() << endl;
system("PAUSE");

This does not produce the shape draw output it outputs the circle draw function.

On Tutorialspoint.com they have the following code:

#include <iostream> 
using namespace std;

class Shape {
 protected:
  int width, height;

 public:
  Shape( int a = 0, int b = 0){
     width = a;
     height = b;
  }
  int area() {
     cout << "Parent class area :" <<endl;
     return 0;
  }
};
 class Rectangle: public Shape {
  public:
  Rectangle( int a = 0, int b = 0):Shape(a, b) { }

  int area () { 
     cout << "Rectangle class area :" <<endl;
     return (width * height); 
   }
};

class Triangle: public Shape {
 public:
  Triangle( int a = 0, int b = 0):Shape(a, b) { }

  int area () { 
     cout << "Triangle class area :" <<endl;
     return (width * height / 2); 
  }
};

// Main function for the program
int main() {
Shape *shape;
Rectangle rec(10,7);
Triangle  tri(10,5);

// store the address of Rectangle
shape = &rec;

// call rectangle area.
shape->area();

// store the address of Triangle
shape = &tri;

// call triangle area.
shape->area();

return 0;
}

Why is it when they use a base class pointer pointing to an inherited class and they invoke draw, which all three poses, their program outputs the base class function. In my code, I had the same setup, a base class pointer to a inherited class object and it would output the inherited class draw. Thank you in advance.

  • To be clear, you want the pointer to the shape to call the shape version, and the pointer to a circle to call the circle version? –  Aug 08 '19 at 04:09
  • 1
    It isn't clear what you want. Invoking `shape1->draw();` will invoke `Circle::draw` due to the very polymorphic design *you* coded. If you don't want that, don't make the member virtual. If you want that, but want `Circle::draw` to invoke it's base class member, then `Shape::draw();` within `Circle::draw` would do that. If you want to invoke `Shape::draw()` from `shape1`, you need to do something like: `shape1->Shape::draw();` If you want something other than all of that, update your question and describe your problem better, please. – WhozCraig Aug 08 '19 at 04:20
  • Please avoid `using namespace std;`. It is considered bad practice and will ruin your life. See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721) – L. F. Aug 08 '19 at 08:19
  • Chipster. Yes almost. I want a shape pointer, pointing to an object of circle, to output the shape draw function. – Christian Galleisky Aug 08 '19 at 21:37
  • Chipster. Yes almost. I want a shape pointer, pointing to an object of circle, to output the shape draw function. WhozCraig, yes that is it. When would you want to use virtual. I come from a JAVA background and thought that was the basic way to tell the compiler that you are going to make inherited classes possess an overridden function of that type. But now it seems you don't need to put in virtual or override for that matter, to get the desired polymorphic effect. IN FACT it seems to get polymorphism to work in your program you need to NOT put virtual or override in your code. What say you? – Christian Galleisky Aug 08 '19 at 21:43

0 Answers0