1

In a C++ program in which there is a base class with a virtual function and two derived classes with redefinition of that virtual function , which class is a polymorphic class? either derived classes or base and derived classes together?

#include <iostream>
using namespace std;

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

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

class Triangle: public Polygon {
  public:
    int area ()
      { return (width * height / 2); }
};

int main () {
  Rectangle rect;
  Triangle trgl;
  Polygon poly;
  Polygon * ppoly1 = &rect;
  Polygon * ppoly2 = &trgl;
  Polygon * ppoly3 = &poly;
  ppoly1->set_values (4,5);
  ppoly2->set_values (4,5);
  ppoly3->set_values (4,5);
  cout << ppoly1->area() << '\n';
  cout << ppoly2->area() << '\n';
  cout << ppoly3->area() << '\n';
  return 0;
}
ali rahimi
  • 35
  • 3

1 Answers1

2

Polymorphism in the general computer science meaning:

From Polymorphism (computer science) - Wikipedia:

polymorphism is the provision of a single interface to entities of different types[1] or the use of a single symbol to represent multiple different types.

So polymorphism is a concept that involves several entities. I would say that your example as a whole uses this concept, since Polygon acts as an interface implemented by Rectangle and Triangle. This is the first part of the definition above. When you'll use the interface, you will be doing what is mentioned in the second part of the above definition.

You could make Polygon::area() pure virtual (by adding =0 and omitting the implementation), in order to emphasize the fact that Polygon is only an interface.

You could also extract the methods from Polygon into another base class with only pure virtual methods. This would be the "classic" way of defining an interface.

Polymorphism in a strict C++ meaning:

According to the standard (see: Polymorphic objects):

Objects of a class type that declares or inherits at least one virtual function are polymorphic objects.

Therefore, in your example, all three classes (Polygon, Rectangle, Triangle) are polymorphic. Polygon declares a virtual method, and the others inherit it.

You could also use type traits to check if a class is polymorphic: see std::is_polymorphic.

The code below demonstrates using std::is_polymorphic in your case:

#include <iostream>

class A {};

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

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

class Triangle : public Polygon {
public:
    virtual int area() override { return (width * height / 2); }
};

int main() {
    std::cout << "A: " << std::is_polymorphic<A>::value << std::endl;
    std::cout << "Polygon: " << std::is_polymorphic<Polygon>::value << std::endl;
    std::cout << "Rectangle: " << std::is_polymorphic<Rectangle>::value << std::endl;
    std::cout << "Triangle: " << std::is_polymorphic<Triangle>::value << std::endl;
    return 0;
}

Output:

A: 0
Polygon: 1
Rectangle: 1
Triangle: 1

2 last notes:

  1. Although it's not a must, it's recomended to tag your overriding methods with override. This forces the compiler to ensure this method actually overrides a virtual base class method. I also add virtual (also not a must in an overriding method), seems more readable to me.

  2. Better to avoid using namespace std - see Why is "using namespace std;" considered bad practice?.

wohlstad
  • 12,661
  • 10
  • 26
  • 39