answersLogoWhite

0


Best Answer

Consult the following program. Three classes are declared, A, B and C, such that B and C both inherit from A. Thus B and C are said to be a "kind of" A. Class A includes a virtual method named action() which is overridden by both B and C. Thus B and C are more specialised than A. All the methods simply print stubs to show which method has been executed.

The main function instantiates one instance of each class and passes all three to the nonpolymorph() function before passing them to the polymorph() function. Both functions accept a reference to an A object and both functions invoke the action() method for that object. Both functions execute the correct method regardless of which type of object is passed.

The nonpolymorphic() function is the least efficient method of enabling runtime polymorphism because the function itself needs to know the exact type of the object being passed in order to invoke the correct method. In this case it examines the last character of the runtime type name and switches accordingly. With more complex class names you might be forced to use nested ifs to compare names, which is even more costly in terms of performance. Once the correct class is determined, a dynamic cast is used for B and C objects to invoke the correct method.

Note that if you should happen to derive new classes from A, you must also update this function to cater for them (adding new cases). In a real program you might have dozens of functions like this and each must be updated accordingly to cater for all new types. This can very quickly become a maintenance nightmare.

The polymorph() function, on the other hand, is not only simpler to implement it requires no additional maintenance whatsoever. It will cater for any class that inherits from A both now and in the future. It is able to achieve runtime polymorphism by virtue of the fact the A class has a virtual function which both B and C override. When you declare a virtual function, you create a virtual table for that class as well as all those that derive from it (that is, one table per class, not one table per object of the class). This consumes a little bit more memory than normal, but the table is simply an array of virtual function pointers that point to the appropriate overrides for that particular class. So the A class table entry will point to A::action(), while the B class table entry will point to B::action(), and so on. Thus you get the expected behaviour without the need for expensive runtime information.

Virtual methods mimic the way objects work in real life to some extent. For instance, imagine the function is you and A, B and C are black box objects that can make a sound at the press of a button on top of the box. Someone places one of these objects in front of you and, although you have no idea what type of object it is, you know you can simply press the button and it will make a sound. In other words you know how the interface works, the object itself decides how that interface is implemented. If the button were not on top of the box then you might have to feel around the box to locate the button. This is akin to using runtime type information to get the additional information you need to use the interface. Sometimes this is required but it is usually a sign of poor design. When interfaces are consistent, then your need to know the type of object is wholly irrelevant.

#include<iostream>

#include<typeinfo>

using namespace std;

struct A

{

virtual void action () { cout << "A::action()\n" << endl; }

};

struct B : A

{

void action () override { cout << "B::action()\n" << endl; }

};

struct C: A

{

void action () override { cout << "C::action()\n" << endl; }

};

void nonpolymorph(A& a)

{

std::string s (typeid(a).name());

switch (s.at( s.size()-1))

{

case ('A'): a.action(); break;

case ('B'): (dynamic_cast<B&>(a)).action(); break;

case ('C'): (dynamic_cast<C&>(a)).action(); break;

}

}

void polymorph(A& a)

{

a.action();

}

int main()

{

A a;

B b;

C c;

nonpolymorph (a);

nonpolymorph (b);

nonpolymorph (c);

polymorph (a);

polymorph (b);

polymorph (c);

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is an example of run time polymorphism in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is Difference between dynamic polymorphism and static polymorphism with example?

Static polymorphism is used the concept of early binding or we can say compile time binding where as dynamic polymorphism used the concept of late binding or run time binding.


What is compile time polymorphism in java?

Compile Time Polymorphism in Java is when you have the several methods with same name and different parameters and compiler has to decide how to select which method has to run based on the arguments hence the name Compile time polymorphism or method overloading.


Why is c plus plus regarded as being a hybrid language?

C++ is regarded as hybrid because it is both procedural and objected oriented. A pure c program can be compiled and run on a c++ platform. At the same time, c++ also provides object oriented features like classes, polymorphism, encapsulation, abtraction, etc.


Why run time polymorphism is dynamic and compile time polymorphism is static?

The simple answer is that compile-time polymorphism occurs at compile time while runtime polymorphism occurs at runtime. The actual answer is that compile-time polymorphism results in the compiler generating source code on your behalf while runtime polymorphism relies on function pointers or virtual methods to determine the next instruction at runtime. Compile-time polymorphism therefore applies to template functions and classes since that is the only way the compiler can generate source code on your behalf. To achieve this, the runtime type for the template parameters must be fully-defined at compile time, even if those types have runtime polymorphic characteristics of their own. Runtime polymorphism applies to virtual methods and function pointers, both of which can be used to dynamically alter the execution path of your program. Virtual methods are made possible through virtual tables, which are essentially just arrays of function pointers. Each runtime type that derives from a base class with virtual methods provides its own virtual table, thus the runtime type determines which specific function overrides will be invoked at runtime, even if the runtime type cannot be determined at compile time. In this way you can generalise your code to work with the base type but still get the expected polymorphic behaviour whenever a derived type is passed instead.


What is the Practical application of a derived class object stored in base class pointer?

If you have base class derived object pointing by base class pointer, then you have the power of run time polymorphism in your hand, which gives you the ability to call the derived class implementation of the virtual member function. If we declare the member function as virtual in base class which needs to overridden in derived class, then you can decide at run time which implementation will be called at run time.

Related questions

What is Difference between dynamic polymorphism and static polymorphism with example?

Static polymorphism is used the concept of early binding or we can say compile time binding where as dynamic polymorphism used the concept of late binding or run time binding.


What is the difference between compile time and run time polymorphism?

Runtime prolymorphism means overriding compiletile polymorphism means overloading


How is run time polymorphism accomplished?

Through inheritance and virtual functions.


What is polymorphism and its types?

Polymorphism means multiple form of a function, variable or object. In Computer Science, polymorphism is a programming language feature that allows values of different data types to be handles using a common interface. There are three types : Ad-Hoc Polymosphism, Parametric Polymorphism, Subtype/Inclusion Polymorphism. Source: Wikipedia.


What is Dynamic Polymorphism?

Dynamic polymorphism is a programming method that makes objects with the same name behave differently in different situations. This type of programming is used to allow Java Scripts to run while playing a game on the computer, for example.


What is compile time polymorphism in java?

Compile Time Polymorphism in Java is when you have the several methods with same name and different parameters and compiler has to decide how to select which method has to run based on the arguments hence the name Compile time polymorphism or method overloading.


Why is c plus plus regarded as being a hybrid language?

C++ is regarded as hybrid because it is both procedural and objected oriented. A pure c program can be compiled and run on a c++ platform. At the same time, c++ also provides object oriented features like classes, polymorphism, encapsulation, abtraction, etc.


Why run time polymorphism is dynamic and compile time polymorphism is static?

The simple answer is that compile-time polymorphism occurs at compile time while runtime polymorphism occurs at runtime. The actual answer is that compile-time polymorphism results in the compiler generating source code on your behalf while runtime polymorphism relies on function pointers or virtual methods to determine the next instruction at runtime. Compile-time polymorphism therefore applies to template functions and classes since that is the only way the compiler can generate source code on your behalf. To achieve this, the runtime type for the template parameters must be fully-defined at compile time, even if those types have runtime polymorphic characteristics of their own. Runtime polymorphism applies to virtual methods and function pointers, both of which can be used to dynamically alter the execution path of your program. Virtual methods are made possible through virtual tables, which are essentially just arrays of function pointers. Each runtime type that derives from a base class with virtual methods provides its own virtual table, thus the runtime type determines which specific function overrides will be invoked at runtime, even if the runtime type cannot be determined at compile time. In this way you can generalise your code to work with the base type but still get the expected polymorphic behaviour whenever a derived type is passed instead.


How do you make two loops run at the same time in c plus plus?

With two threads.


Give example of run-time error?

Division by zero.


How fast does a cheertah run?

Fastest recorded time is 70 mph plus.


What is the Practical application of a derived class object stored in base class pointer?

If you have base class derived object pointing by base class pointer, then you have the power of run time polymorphism in your hand, which gives you the ability to call the derived class implementation of the virtual member function. If we declare the member function as virtual in base class which needs to overridden in derived class, then you can decide at run time which implementation will be called at run time.