Alex Newb
Alex Newb

Reputation: 21

Is method overriding (without virtual methods or pointers) considered a part of Polymorphism?

First of all, I would like to say that I searched for similar questions, but the answers seem to focus on different things.

I started learning C++ but I have a problem understanding what exactly is considered as polymorphism. I have written two different programs.

In the first, I don't use virtual methods or pointers to objects.

In the second, I use a virtual method (area) and an array with pointers to objects.

I understand that the second program is using polymorphism (a parent class pointer is used to point to a child class object).

But since the first program produces the exact same results by overriding the function area, is it considered to be using polymorphism too?

Program 1

#include <iostream>

using namespace std;

class Polygon {
protected:
    float base;
    float height;
public:
    Polygon(): base(0), height(0) {}
    Polygon(float b, float h): base(b), height(h) {}
    float area() {return -1;}
};

class Triangle:public Polygon {
public:
    Triangle(): Polygon() {}
    Triangle(float b, float h): Polygon(b,h) {}
    float area() {return base*height/2;}
};

class Rectangle:public Polygon {
public:
    Rectangle(): Polygon() {}
    Rectangle(float b, float h): Polygon(b,h) {}
    float area() {return base*height;}
};

int main() {
    //Overriding area method without pointers
    Triangle t1, t2(10,10);
    Rectangle r1, r2(5,5);
    cout<<t1.area()<<endl;  //Output: 0
    cout<<t2.area()<<endl;  //Output: 50
    cout<<r1.area()<<endl;  //Output: 0
    cout<<r2.area()<<endl;  //Output: 25
    cout<<t1.Polygon::area()<<endl; //Output: -1
}

Program 2

#include <iostream>

using namespace std;

class Polygon {
protected:
    float base;
    float height;
public:
    Polygon(): base(0), height(0) {}
    Polygon(float b, float h): base(b), height(h) {}
    virtual float area() {return -1;} //virtual method area
};

class Triangle:public Polygon {
public:
    Triangle(): Polygon() {}
    Triangle(float b, float h): Polygon(b,h) {}
    float area() {return base*height/2;}
};

class Rectangle:public Polygon {
public:
    Rectangle(): Polygon() {}
    Rectangle(float b, float h): Polygon(b,h) {}
    float area() {return base*height;}
};

int main() {
    //Polymorphism
    Triangle t1, t2(10,10);
    Rectangle r1, r2(5,5);
    Polygon *ptr[]={&t1,&t2,&r1,&r2};
    cout<<ptr[0]->area()<<endl;  //Output: 0
    cout<<ptr[1]->area()<<endl; //Output: 50
    cout<<ptr[2]->area()<<endl; //Output: 0
    cout<<ptr[3]->area()<<endl; //Output: 25
    cout<<ptr[0]->Polygon::area()<<endl;    //Output: -1
}

Upvotes: 2

Views: 425

Answers (1)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

But since the first program produces the exact same results by overriding the function area, is it considered to be using polymorphism too?

Well, no. There's no overriding in the 1st sample, but shadowing of the base class function implementations.


That your examples produce the same results is because of the fact, that in your 1st sample

    cout<<t1.area()<<endl;  //Output: 0
    cout<<t2.area()<<endl;  //Output: 50
    cout<<r1.area()<<endl;  //Output: 0
    cout<<r2.area()<<endl;  //Output: 25

you are calling functions with concrete class instances.

Something like

    Polygon *ptr[]={&t1,&t2,&r1,&r2};
    cout<<ptr[0]->area()<<endl;  //Output: 0
    cout<<ptr[1]->area()<<endl; //Output: 50
    cout<<ptr[2]->area()<<endl; //Output: 0
    cout<<ptr[3]->area()<<endl; //Output: 25

would fail being used with this code.


Polymorphism means that you have some interface declared, that might be accessed through a base class reference.

In c++ you have generally the choice of

  • Dynamic polymorphism (i.e. abstract/virtual base classes) and resolve overridden function calls at run time
  • Static polymorphism (i.e. using the CRTP to resolve overridden function calls at compile time)

Upvotes: 3

Related Questions