shourya
shourya

Reputation: 1

Polymorphism (types)

These are some lines from “Thinking in C++” vol 1 (page 716 para2)by Bruce Eckel:

"Polymorphism is a feature that cannot be viewed in isolation (like const or a switch statement, for example), but instead works only in concert, as part of a “big picture” of class relationships. People are often confused by other, non-object-oriented features of C++, like overloading and default arguments, which are sometimes presented as object-oriented. Don’t be fooled; if it isn’t late binding, it isn’t polymorphism."

I am not able to get this clearly .Does he mean that there is no such thing like Compile time Polymorphism?

Upvotes: 0

Views: 100

Answers (2)

Mike Seymour
Mike Seymour

Reputation: 254711

Does he mean that there is no such thing like Compile time Polymorphism?

No. He means that, like the C++ standard, he's using the term "polymorphism" specifically to refer to dynamic (runtime) polymorphism, which C++ supports through inheritance and virtual functions.

C++ also supports what some would call "static (compile-time) polymorphism" or "generics" through templates and function overloads. In the context of C++, we tend not to use the term "polymorphism" for that, to avoid confusion with the standard, and rather different, meaning of that term.

Upvotes: 2

Cory Kramer
Cory Kramer

Reputation: 118001

He is saying the following.

This is an example of polymorphism

class A
{
    virtual void foo() { std::cout << "Base class" << std::endl; }
};

class B : public A
{
    virtual void foo() override { std::cout << "Derived class" << std::endl; }
};

This is just overloading, which he is saying he doesn't consider to be OOP

// 3 overloads of a function
int foo(int x);
int foo(double x);
int foo(int x, double y);

Upvotes: 0

Related Questions