Virtual function
Virtual function ppt for knowing about the virtual functions in brief
Program of static binding
#include <iostream.h>
#include<conio.h>
class Baseclass
{
protected:
int d;
public:
Baseclass()
{ }
Baseclass(int arg)
{
d=arg;
}
void display()
{
cout<<"Display method of Base class";
cout<<endl<<"d="<<d<<endl;
}
};
class Deriveclass;public Baseclass
{
private:
int k;
public:
Derivedclass()
{
}
Derivedclass(int arg)
{
k=arg;
}
void display()
{
cout<<"\nDisplay method of derived class";
cout<<endl<<"d="<<d<<endl;
cout<<endl<<"k="<<k<<endl;
}
};
void main()
{
//create instance of base class
Baseclass varB(10);
//call display function on instance of base class
varB.display();
Derivedclass varD(20);
vard.display();
}
#include <iostream.h>
#include<conio.h>
class Baseclass
{
protected:
int d;
public:
Baseclass()
{ }
Baseclass(int arg)
{
d=arg;
}
void display()
{
cout<<"Display method of Base class";
cout<<endl<<"d="<<d<<endl;
}
};
class Deriveclass;public Baseclass
{
private:
int k;
public:
Derivedclass()
{
}
Derivedclass(int arg)
{
k=arg;
}
void display()
{
cout<<"\nDisplay method of derived class";
cout<<endl<<"d="<<d<<endl;
cout<<endl<<"k="<<k<<endl;
}
};
void main()
{
//create instance of base class
Baseclass varB(10);
//call display function on instance of base class
varB.display();
Derivedclass varD(20);
vard.display();
}
Output
Display method of base class
d=10
Display method of derived c
Program of dynamic binding
Display method of base class
d=10
Display method of derived c
Program of dynamic binding
#include <iostream.h>
#include<conio.h>
class Baseclass
{
protected:
int d;
public:
Baseclass()
{ }
Baseclass(int arg)
{
d=arg;
}
virtual void display()
{
cout<<"Display method of Base class";
cout<<endl<<"d="<<d<<endl;
}
};
class Deriveclass;public Baseclass
{
private:
int k;
public:
Derivedclass()
{
}
Derivedclass(int arg)
{
k=arg;
}
void display()
{
cout<<"\nDisplay method of derived class";
cout<<endl<<"d="<<d<<endl;
cout<<endl<<"k="<<k<<endl;
}
};
void main()
{
Baseclass *baseptr; //declare pointer to base class
Baseclass varB(10); //create instance of base class
baseptr->display(); //assign address of base class to pointer
Derivedclass varD(20);
baseptr=&varD;
baseptr->display();
}
Output
Display method of base class
d=10
Baseclass varB(10); //create instance of base class
baseptr->display(); //assign address of base class to pointer
Derivedclass varD(20);
baseptr=&varD;
baseptr->display();
}
Output
Display method of base class
d=10
Program for abstract class
Example of Abstract base class
class Base //Abstract base class
{
public:
virtual void show() = 0; //Pure Virtual Function
};
{
public:
virtual void show() = 0; //Pure Virtual Function
};
class Derived:public Base
{
public:
void show()
{ cout << "Implementation of Virtual Function in Derived class"; }
};
int main()
{
Base obj; //Compile Time Error
Base *b;
Derived d;
b = &d;
b->show();
}
{
public:
void show()
{ cout << "Implementation of Virtual Function in Derived class"; }
};
int main()
{
Base obj; //Compile Time Error
Base *b;
Derived d;
b = &d;
b->show();
}
Output : Implementation of Virtual Function in Derived class
In the above example Base class is abstract, with pure virtual show() function, hence we cannot create object of base class.
In the above example Base class is abstract, with pure virtual show() function, hence we cannot create object of base class.
No comments:
Post a Comment