Define virtual function in C++

Define virtual function in C++

Virtual function in C++
A virtual function a member function which is declared within a base class and is re-defined(Overriden) by a derived class.

How make virtual function ?
using the keyword 'virtual' we make virtual function

program:

#include< iostream>
using namespace std;
class base {
public:
virtual void print () {
cout<< "print base class" << endl;
}
void show () {
cout<< "show base class" << endl;
}
};
class derived:public base {
public:
void print () {
cout<< "print derived class" << endl;
}
void show () {
cout<< "show derived class" << endl;
}
};
int main() {
base *bptr;
derived d;
bptr = &d;
//virtual function, binded at runtime
bptr->print();
// Non-virtual function, binded at compile time
bptr->show();
}


output-

print derived class
show base class




Read More →
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!