1

How to resolve not a member of base class when create derived object using new operator

When i execute the below program, i am getting the below error only when i create a object using new operator "shape *s=new Rectangle";. But i havent faced any issues when i create object "Rectangle s"

Actually i dont want to use hello method in my Triangle class. Need to acces/uses hello method in rectangle class by creating object using new operator "shape *s=new Rectangle".

Please let us know how to resolve this issue using new operator.

Error:

1>c:\shape\shape\shape.cpp(60) : error C2039: 'hello' : is not a member of 'Shape'
1>c:\shape\shape\shape.cpp(10) : see declaration of 'Shape'

Code snippet:

// Shape.cpp : Defines the entry point for the console application.
//

#include <iostream>

using namespace std;

// Base class
class Shape 
{
public:
   // pure virtual function providing interface framework.
   virtual int getArea() = 0;

   void setWidth(int w)
   {
      width = w;
   }
   void setHeight(int h)
   {
      height = h;
   }
protected:
   int width;
   int height;
};

// Derived classes
class Rectangle: public Shape
{
public:
   int getArea()
   { 
      return (width * height); 
   }
   void hello()
   {
        std:cout<<std::endl<<"hello"<<std::endl;
   }
};
class Triangle: public Shape
{
public:
   int getArea()
   { 
      return (width * height)/2; 
   }
};

int main(void)
{

   Shape *s= new Rectangle;
  // Triangle  Tri;

   s->setWidth(5);
   s->setHeight(7);
   // Print the area of the object.
   cout << "Total Rectangle area: " << s->getArea() << endl;
   s->hello();
/*
   Tri.setWidth(5);
   Tri.setHeight(7);
   // Print the area of the object.
   cout << "Total Triangle area: " << Tri.getArea() << endl; 
*/
   return 0;
}
  • 1
    Your question is different from your problem. – Weak to Enuma Elish Feb 25 '16 at 11:01
  • 1
    ^^ this is right. You're mistaking a inheritance issue with the fact that you've told your pointer that it needs to be a pointer to Shape, not Rectangle. Shape doesn't know about hello(). Virtual void hello() in shape or just make a Rectangle* instead of a Shape* if thats viable. – WearyWanderer Feb 25 '16 at 11:05

1 Answers1

1

You have a pointer to Shape, but (and this is what your compiler tells you) Shape has no member hello().

So theres two ways to access it: Either you cast it to Rectangle* first (which might fail for non-rectangles) or you create a function virtual void hello() in Shape which is then resolved at compile-time - for all classes that inherit from Shape.

Anedar
  • 4,235
  • 1
  • 23
  • 41
  • Oops, just commented something similar on OP's post but this is dead on the money – WearyWanderer Feb 25 '16 at 11:06
  • or use dynamic_cast(s) if you are sure it's a rectangle – Garf365 Feb 25 '16 at 11:17
  • When i do type casting to Rectangle *, it works for me.. Thank you for your answer. But virtual void hello() in Shape, it doesnt resolve this issue. pls let me know how to resolve using "virtual void hello()" – user3205652 Feb 25 '16 at 12:35
  • Take a look at [this question](http://stackoverflow.com/questions/2391679/why-do-we-need-virtual-methods-in-c) - the accepted answer provides in good detail how to use virtual functions and why they are useful. – Anedar Feb 26 '16 at 18:49