V B C & V F: Irtual ASE Lass Irtual Unctions
V B C & V F: Irtual ASE Lass Irtual Unctions
C++ INTRODUCTION
History of C
Extension of C Early 1980s: Bjarne Stroustrup (Bell Laboratories) Provides capabilities for object-oriented programming
Objects: reusable software components Model items in real world Object-oriented programs Easy to understand, correct and modify
An element of ambiguity can be introduced onto a C++ program when multiple base classes are inherited. For example, consider this incorrect program:
public :
int b;
CONTINUED
Void main( ) { D3 ob; Ob.a=25; Ob.b=50; Ob.c=75; Ob.total = ob.a+ob.b+ob.c; Cout<<ob.a<<ob.b<<ob.c<<ob.total<<\n; }
As both D1 and D2 inherit Base, They both have copies of Base. However D3 inherits both D1 and D2. This means that there are two copies of Base present in an object of type D3. Therefore in an expression like ob.a = 25; Which a is being referred to the one in D1 or the one in D2 ? Because there are two copies of Base present in the object ob, there are two ob.a1s. Thus, the statement is inherently ambiguous
To apply the scope resolution operator (::) to a and manually select one a. For example, if we use the following main( ) function, rest of the program remains same.
As you can see, because the :: operator the program has manually selected the D1s version of Base. However ,this solution raises a deeper issue: What if only one copy of Base is actually required ? Is there someway to prevent two copies from being included in D3 ? The answer is Virtual Base Classes.
When two or more objects are derived from a common base class ,you can prevent multiple copies of base class from being present in an object derived from those objects by declaring the base class as virtual when it is inherited. This is accomplished by putting keyword virtual before the base class name when it is inherited. For example, here is another version of the example program in which D3 contains only one copy of Base.
NOTE :
The only different a virtual difference between normal base class and a virtual one is when an object inherits the base more than once. If virtual base classes are used ,then only one base class is present in the object. Otherwise , multiple copies will be found.
VIRTUAL FUNCTIONS
Virtual, as the name implies, is something that exists in effect but not in reality. The concept of virtual function is the same as a function, but it does not really exist although it appears in needed places in a program. The object-oriented programming language C++ implements the concept of virtual function as a simple member function, like all member functions of the class. The functionality of virtual functions can be over-ridden in its derived classes. The programmer must pay attention not to confuse this concept with function overloading.
Function overloading is a different concept and will be explained in later sections of this tutorial. Virtual function is a mechanism to implement the concept of polymorphism (the ability to give different meanings to one function).
NEED
o
The vital reason for having a virtual function is to implement a different functionality in the derived class. For example: a Make function in a class Vehicle may have to make a Vehicle with red color. A class called Four-wheeler, derived or inherited from Vehicle, may have to use a blue background and 4 tires as wheels. For this scenario, the Make function for Four-wheeler should now have a different functionality from the one at the class called Vehicle. This concept is called Virtual Function.
Virtual functions are member functions declared with the keyword virtual. For example, the general syntax to declare a Virtual Function uses:
class classname //This denotes the base class of C++ virtual function { public: virtual void memberfunctionname() //This denotes the C++ virtual function { ............. ............ } };
In the previous example, it is evidenced that after declaring the member functions Make() as virtual inside the base class Vehicle, class FourWheeler is derived from the base class Vehicle. In this derived class, the new implementation for virtual function Make() is placed.
The programmer might be surprised to see the function call differs and the output is then printed as above. If the member function has not been declared as virtual, the base class member function is always called because linking takes place during compile time and is therefore static.
THANK YOU