how to use virual function in real time example



how to use virual function in real time example..

Answer / yuva

You use virtual functions when you want to override a
certain behavior(read method) for your derived class than
the one implemented for the Base class and you want to do so
at run-time through an pointer to Base class.

The classical example is when you have a base class called
Shape and concrete shapes(classes) which derive from it.
Each concrete class overrirdes(implements a virtual method)
called Draw().

The class hierarchy as follows:

Class hierarchy

The following snippet shows the usage of the example, it
creates an array of Shape class pointers wherein each points
to distinct derived class object. At run-time invoking
Draw() method results in calling of the method overriden by
that derived class and the particular Shape is drawn(rather
rendered).

Shape *basep[] = { &line_obj, &tri_obj,
&rect_obj, &cir_obj};
for (i = 0; i < NO_PICTURES; i++)
basep[i] -> Draw ();

The above program just uses the pointer to the Base class
storing addresses of the Derived class objects provides a
loose coupling in the way that the program does not have to
change drastically if a new concrete derived class of shape
is added anytime because the are minimal code segments which
actually use(depend) on the concrete Shape type.

The above is a good example of Open Closed Principle of the
famous SOLID design principles.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

Explain what is a pragma?

0 Answers  


What is string length in c?

0 Answers  


write a program to gat the digt sum of a number (et. 15= >1+5=6)

2 Answers  


What type of function is main ()?

0 Answers  


What are the 4 types of functions?

0 Answers  


Explain what are the different file extensions involved when programming in c?

0 Answers  


How do I access command-line arguments?

2 Answers   Bosch, Wipro,


What is meant by initialization and how we initialize a variable?

0 Answers  


player is good if the following conditions are satisfied: He is either from “India”, “Japan” or “Korea” He has a minimum of 3 years of experience He has won at least 3 major tournaments, and one of them must be "World Championship Tournament". He must know more than 10 techniques. (but see next question) If he knows less than 10 techniques, then he must be a world champion. His name contains at least 3 words. For example "Sachin Tendulkar" will not meet this condition. Write a method: boolean isGoodPlayer(String name, String country, int yearsExperience, String[] majorTournamentsWon, String[] techniques, boolean isWorldChampion) name country yearsExperience majorTournamentsWon techniques isWorldChampion

1 Answers  


"%u" unsigned integer print the a) address of variable b) value of variable c) name of a variable d) none of the above

0 Answers  


What is malloc() function?

0 Answers  


How arrays can be passed to a user defined function

0 Answers  


Categories