Virtual Functions – Friend Functions, Static Functions| OOP in C++

Virtual Functions

When two functions with the same name are present in base class and its derived class, and we try to call the copy of derived class function using the pointer of base class, what happens, normally, a base class pointer always calls the copy of the function present in base class(it doesn't matter whether a base Class pointer is made to point to the object of derived class).
To resolve such type of problem, c++ provides "virtual" keyword, a function which is preceded by virtual keyword is known as virtual function. The benefit is, when a function is made virtual, c++ determines which function to use at runtime based on the type of object pointed to by the base pointer, rather than the type of the pointer.




class Base
void display(void)
cout<<"\n display Base";}
virtual void show(show)
cout<<"\n show base";}
class Derived: public Base
void display(void)
cout<<"\n display derived";}
void show(void)
cout<<"\n show derived ";}
int main()
Base B;
Derived D;
Base *bptr;
bptr->display(); //calls base version
bptr->show(); //calls base version
bptr->display(); //calls base version
bptr->show(); //calls derived version due to virtual declaration of show() in Base class
return 0;}


Rules for virtual Function

1. Virtual function must be member of some class.
2. Virtual function cannot be a static member.
3. They are accessed by using object pointer.
4. A virtual function can be a friend function.
5. A virtual function in a base class must be defined.
6. All the prototypes of a virtual function must be same, otherwise, c++ does function overloading.
7. Constructors can not be virtual, but Destructor can.
8. A base class pointer can point to the object of the derived class, but reverse is Not true.
9. When a base class pointer points to the object of its derived class, an increment of pointer doesn't make it to point to the next object of derived class.
10. If a virtual function is defined in its base class, it is not necessary to redefine it in it's derived classes.

Pure Virtual Function

Functions like "Virtual void display(void)=0;" is called pure virtual function. A pure virtual function doesn't have any definition in base class. Compiler requires each derived class to either define the function or re-declare it as pure virtual function. A class which contains pure virtual function cannot be used to declare it's objects.

Friend Function

A friend function is a non-member function of a class which can access private members of a class with the help of friend keyword we can declare a non-member function for a class as friend.
Characteristics of a friend function
1. It is not in the scope of the class to which it is declared as a friend so it cannot be called using the object of that class.
2. It is invoked like a normal c++ function without using any object.
3. It can access private members of a class using the object of that class.
4. It can be declared either as public or private.
5. A friend function can be declared as a friend in more than one classes.

Static Members

1. Static member variable (class variables)
a. It is initialized to zero when the first object of it's class is created. No other initialization is permitted.
b. Only one copy of that member is created for the class and is shared by all the objects.
c. It is visible only within class, but its lifetime is the entire program.
2. Static member function
a. A static function can access only other static members(variables and fumctions).
b. A static member function can be called using the class name instead if it's object.



class test
int x;
static int count;
void getdata(int a)
count= count +1;
Static void showcount(void)
cout<<"\n count="<<count;
void showdata(void)
cout<<"\n X="<<x;
int test::count;
int main()
test t1, t2;
return 0;}

Custom Search

%d bloggers like this: