0
class Polygon {
  protected:
    int width, height;
  public:
    void set_values (int a, int b)
      { width=a; height=b; }
};

class Rectangle: public Polygon {
  public:
    int area()
      { return width*height; }
};

int main () {
  Rectangle rect;
  Polygon * ppoly1 = ▭
  ppoly1->set_values (4,5);
  cout << rect.area() << '\n';
  return 0;
}

In above example, what does ppoly1 points to and how is this pointer not able to access rectangle class's function?

WHY ppoly1->area() is an error

Thanks!

2
  • 1
    Because you can only access base class methods via a base class pointer. Commented Jan 6, 2014 at 13:49
  • 1
    because Polygon has no function named area. Commented Jan 6, 2014 at 13:50

3 Answers 3

6

The expression ppoly1->area() is an error because ppoly1 is typed to Polygon which has no area method declared. When C++ tries to evaluate this member it essentially starts at Polygon sees no member named area and hence issues an error

It sounds like you want to give the Polygon type the notion of an area method without an implementation (forcing the derived types to provide one). If that is the case then you should declare an un-implemented virtual method in Polygon

class Polygon { 
  ...
  virtual ~Polygon() { } 
  virtual int area() = 0;
};
Sign up to request clarification or add additional context in comments.

Comments

0

Base classes know nothing about their derived classes. When a base class is being defined there is no yet any derived class.

Variable ppoly1 is of the type Polygon *. Class Polygon has no method area so the compiler issues the error.

If you want to use a common interface for derived classes you should declare it in the base class. For example

class Polygon {
  protected:
    int width, height;
  public:
    void set_values (int a, int b)
      { width=a; height=b; }
    virtual int area() const = 0;
    virtual ~Polygon(){}
};

class Rectangle: public Polygon {
  public:
    int area() const
      { return width*height; }
};

int main () {
  Rectangle rect;
  Polygon * ppoly1 = &rect;
  ppoly1->set_values (4,5);
  cout << rect.area() << '\n';
  return 0;
}

Comments

0

ppoly1 is a Polygon pointer. The fact that the pointer points to a Rectangle object does not enable it to call Rectangle functions; the type is still Polygon*. To enable it to call Rectangle functions you need to either make it a Rectangle pointer, or implement a virtual method in your Polygon class,

e.g.

virtual int area() const;

This means that when a Polygon object has area() called on it, it will go looking for the most derived instance of area(). On ppoly1 this will be Rectangle->area(). You can keep your Rectangle code the same as before.

Wikipedia on virtual functions: http://en.wikipedia.org/wiki/Virtual_function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.