Showing posts with label Virtual Function and Polymorphism. Show all posts
Showing posts with label Virtual Function and Polymorphism. Show all posts

Tuesday, 24 February 2015

Virtual Function and Polymorphism

There are two types of binding

1.   Early Binding
2.   Late Binding

#include<iostream.h>
#include<conio.h>

class baseA      //class name
{
public:
void display()         //member function
{
cout<<”Base class display() function”<<endl;
}
};
class deriA:public baseA         //Inherits the class
{
public:          //access specifier
void display()          //member function
{
cout<<”Derived class display function”<<endl;
}
};
void main()
{
baseA b1;        //Early binding
baseA *b2;      //Late binding
clrscr();
b2=new deriA;
b1.display();           //Early binding
b2->display();          //Late binding
getch();
}


#include<iostream.h>
#include<conio.h>

class baseA          //class name
{
public:        //access specifier
virtual void display()       //virtual member function
{
cout<<”Base class display function”<<endl;
}
};
class deriA:public baseA    //Inherits the class
{
public:         //access specifier
void display()       //member function
{
cout<<”Derived class display function”<<endl;
}
};
void main()
{
baseA *b1;         //Late binding
baseA *b2;      //Late binding
clrscr();
b1=new baseA;
b2=new deriA;
b1-> display();       //Late binding
b2-> display();        //Late binding
getch():
}

#include<iostream.h>
#include<conio.h>

class baseA       //class name
{
public:         //access specifier
virtual void vir_fun();      //virtual member function
void nonvir_fun();          //member function
};
void baseA::vir_fun()    //class name member
 function scope resolution operator will come
{
cout<<”Virtual function of base class”<<endl;
}
void baseA::nonvir_fun()     //class name member
 function scope resolution operator will come
{
cout<<”Non virtual function of base class”<<endl;
}
class deriA:public baseA         //Inherits the class
{
public:            //access specifier
void vir_fun();               //member function
void nonvir_fun();         //member function
};
void deriA::vir_fun()         //class name member
 function scope resolution operator will come
{
cout<<”Virtual function of deri class”<<endl;
}
void deriA::nonvir_fun()   //class name member
 function scope resolution operator will come
{
cout<<”Non virtual function of deri class”<<endl;
}
void main()
{
deri d;  //object
deriA *dptr;        //Late binding
baseA *bptr;        //Late binding
clrscr();
bptr=&d;
dptr=&d;
bptr->vir_fun();          //Late binding
bptr->nonvir_fun();      //Late binding
dptr->vir_fun();          //Late binding
dptr->nonvir_fun();       //Late binding
getch();
}

#include<iostream.h>
#include<conio.h>

class rectangle       //class name
{
private:        //access specifier
int length,breadth;
public:       //access specifier
rectangle();        //constructor
rectangle(int,int);         //overloading constructor
rectangle operator+(rectangle); //operator overloading
void show_sides();             //member function
};
rectangle::rectangle()        //constructor name member
 function scope resolution operator will come
                     constructor
{
length=breadth=0;
}
rectangle::rectangle(int a,int b)        //constructor name member
 function scope resolution operator will come
       constructor overloading
{
length=a;
breadth=b;
}
rectangle rectangle::operator+(rectangle t)   //constructor  name member
 function scope resolution operator will come
                     operator overloading
{
rectangle temp;
temp.length=length+t.length;
temp.breadth=breadth+t.breadth;
return temp;
}
void rectangle::show_sides()     //constructor name member
 function scope resolution operator will come
{
cout<<”Length:”<<length<<” Breadth:” <<breadth <<endl;
}
void main()
{
rectangle r1(10,20),r2(20,30),r3;      //object
clrscr();
r3=r1+r2;
r1.show_sides();
r2.show_sides();
r3.show_sides();
getch();
}