Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphism c++

In some books there is written that class that declares or inherits a virtual function is called a polymorphic class.

Class B doesn't have any virtual functions but passes more than one is-a test.

Class C has one virtual function but doesn't inherit.

class A {};
class B : public A {};

class C
{
public:
    virtual void f () {}
};

is class B or C polymorphic ?

like image 646
rl84 Avatar asked Jul 22 '11 22:07

rl84


People also ask

Is polymorphism allowed in C?

In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface. According to that definition, no, C doesn't natively support polymorphism.

What is polymorphism and example?

The word “polymorphism” means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. A real-life example of polymorphism is a person who at the same time can have different characteristics.

What is meant by the polymorphism?

Polymorphism, as related to genomics, refers to the presence of two or more variant forms of a specific DNA sequence that can occur among different individuals or populations. The most common type of polymorphism involves variation at a single nucleotide (also called a single-nucleotide polymorphism, or SNP).

Can you do OOP in C?

In principle OOP can be done in any language, even assembly. This is because all OO language compilers/assemblers (e.g. C++) ultimately translate the high level constructs of the language into machine language.


1 Answers

2003: 10.3/1 states, clearly:

A class that declares or inherits a virtual function is called a polymorphic class.

You actually said this yourself, word-for-word, so I don't really understand what the question is.

C (and its descendants, if you add any) is polymorphic; A and B are not.


Note that, in a wider OOP sense, you can always perform some "polymorphism" in that C++ always allows you to upcast; thus all objects that inherit can be treated as a different (but related) type.

However, the term "polymorphic" is defined slightly differently in C++, where it has more to do with whether you can downcast as well. If you don't want to be confusing like the C++ standard, you might call this "dynamic polymorphism".

like image 81
Lightness Races in Orbit Avatar answered Sep 19 '22 14:09

Lightness Races in Orbit