Static,Dynamic Binding & Virtual Function in C++

Static binding in C++

  • In function overloading or operator overloading there will be more than one function with the same name.
  • The functions are invoked by matching arguments.
  • The complier selects the appropriate function for particular call at the compilation time itself. This is called early binding or static binding.

 

Dynamic Binding in C++

  • Selecting the appropriate member function while the program is running is known as run time polymorphism.
  • This process is known as late binding .It is known as dynamic binding as the selection of the function is done dynamically at run time.
  • Dynamic binding requires use of pointer to objects

void fun1(int x)

{

{

void fun1(float x)

{

}

void main()

{

fun1(10);

fun1(1.5);

}

The feature of runtime polymorphism is the ability to refer to objects without regard to their classes using a single pointer.

We can use a pointer to a base class to refer all the derived objects.

When we use the same function name in both base and derived classes a base class pointer executes the base class function only even when is assigned with derived class object address.

 

Virtual Function in C++

  • To invoke the derived class overriding member function b base class pointer when i t is assigned with derived class address the base class member function has to made virtual.
  • The keyword virtual has to be preceded the normal declaration.

 

Rules for virtual functions:

  • The virtual function must be members of some class.
  • They cannot be static members.
  • They accessed by using object pointers.
  • A virtual function can be a friend of another class.
  • A virtual function in a base class must be defined ,even though it may not be used.

Syntax:

Virtual return-type

function_name([parameters])

{

statements;

}

Screenshot_28

 

E.g:

# include<iostream.h>

class animal

{

public:

virtual void walk()

cout<<”\n Inside animal walk”;

}};

class dog:public animal

{

public:

void walk()

{

cout<<”\n Inside dog walk”;

}};

class cat: public animal

{

public:

void walk()

{

cou<<”\n Inside cat walk”;

}};

void play(animal *a)

{

(*a).walk();

}

void main()

{

cat c;

dog d;

play(&c);

play (&d);

}

O/p:

Inside cat walk

Inside dog walk

 

Eg:

# include<iostream.h>

class SIM

{

public:

void connect( ) {

cout<<”Inside connect”;

}};

class airelsim:public sim

{

Public:

Void connect( )

{

Cout<<”In connected to airtel network”;

}};

class bsnlsim:public sim

{

Public:

Void connect( )

{

Cout<<”In connected to bsnl network”;

}};

Class mobile

{

Public:

Static void insert(Sim *s)

{

(*s).connect( );

}};

void main( )

{

Airtle sim airtel;

bsnl sim bsnl;

Mobile::insert(&airtel);

Mobile::insert(&bsnl);

}

o/p:

Inside connect

Inside connect

 

Pure virtual function:

  • A function declared in base class and has no definition relative to the base class.
  • They are redefined in derived classes.
  • They are called as do-nothing functions.

Abstract Base class:

  • A class containing pure virtual functions is called as Abstract Base class
  • They cannot be used to declare any objects.
  • It is purely for inheriting purpose only.
  • They are used to create a base pointer required to achieve runtime polymorphism.

Syntax:

Virtual return type function – name (parameters)

 

Abstract class

 

Screenshot_29

 

# include<iostream.h>

# include<conio.h>

class shape

{

protected;

float dim 1,dim 2;

public:

void read_dim( )

{

cout<<”Input two dim”;

cin>>dim1>>dim2;

}

Virtual void final_area( )=o;

};

Class triangle:public shape

{

Public:

void final_area( )

{

float area=0.5*dim1*dim2;

cout<<”In area of triangle is”<<area;

}};

class rectangle:public shape

{

public:

void final_area( )

float area=dim1*dim2;

cout<<”In area of rectangle is”<<area;

}};

Void main( )

{

Triangle t1;

t1.read_dim( );

Rectangle r1;

r1.read_dim( );

t1.final_area( );

r1.final_area( );

}

 

 

O/P:

Input two dim 2

3

Input two dim5 4

Area of triangle is 3

Area of triangle is 20

  • Abstract class is called as an abstract data type
  • A data type which allows to build a similar data type is called as abstract data type.

 

Hybrid Inheritance:

If there are two on more types of inheritances to design a program it is called as hybrid inheritance.

Ex:

class student{ };

class test:public student

{ };class sports:public student

{ };

Class result:public test,public sports;

This is also called as Multi path inheristance as the class student inherited vice test and also via sports

 

Screenshot_30

Class alpha

{

};

Class beta:public alpha

{

}

 

Virtual Class

You might want to make a class if it is a base class it has been passed to more than one derived class,as might happen with multiple inheritance.

  • A base class cant be specified more than once in a derived class:

Eg: class B {….}

Class D:B,B{….};//ILLEGAL

However a base class can be indirectly passed to the derived class more than once:

 

Eg:

Class X:public B {….}

Class Y:public B {….}

Class Z:public X,public Y{….}

In this case, each object of class Z will have sub objects of class B.

If this causes problems, you can add the key word” virtual “to a base class specifier.

For example:

# include<iostream.h>

Class A

{

Public:

Void fun1( )

{

Cout<<”In Inside function1 of A”;

}};

Class B:public virtual A

{

public:

Void fun2( )

{

Cout<<”In Inside function2 of A”;

}};

 

Class C:public virtual A

{

public:

Void fun 3( )

{

Cout<<”In Inside function3 of C”;

}};

Class D:public B,public C

{

Public:

Void fun4( )

{

cout<<”In Inside function4 of D”;

}};

Void main( )

{

D obj1;

Obj1.fun1( );

Obj1.fun2( );

Obj1.fun3( );

Obj1.fun4( );

}

o/p:

Inside function 1 of A

Inside function 2 of A

Inside function 3 of C

Inside function 4 of D

 

MULTIPLE INHERITANCE

If class is derived from more than one base class it is called as multiple inheritance.

Ex: class  base1

{ };

class base2

{ };

class derived:public base1,public base2

{ };

Example:

Screenshot_31

 

# include<iostream.h>

Class person

{

Protected;

Char name[10];

Public;

Void read_name( )

{

Cout<<”\n Input name”;

Cin>>name;

}};

Class employee

{

Protected;

Int empno;

public;

void read_empno( )

{

Cout<<”\n Input empno”;

cin>>empno;

}};

Classworker:public person,public employee

{

Float wage;

Public;

Void read_wage( )

{

Cout<<” \n Input wage”;

Cin>>wage;

}

Void print_details( )

{

Cout<<”In Name”<<name;

Cout<<”In Empno”<<empno;

Cout<<”In Wage”<<wage;

}};

Void main( )

{

Worker  worker1;

Worker1.read_name( );

Worker1.read_empno( );

Worker1.read_wage( );

Worker1.print_details( );

}