Wednesday 17 July 2013

Know about virtual functions in c++

// Base class is going to add a virtual pointer
// Every class having virtual functions have a vtable
// vtable contains the list of functions pointers which object of that class can call
// vptr points to the function defination of the class it points to.




* Class Test show the concept of virtual tables
// Derived class having overridden functions
// VPTR And the concept of virtual table which contains static array created at compile time listing all function pointer to
// virtual functions that can be called by the object of the class .

 FUNCTION IS MAPPED TO THE CORRESPONDING FUNCTION OF THE CLASS

class test
{
int val;
public:
test()
{
cout<<"Default Constructor of base class"<<endl;
}
virtual void fun1()
{
cout<<"Base class function fun1"<<endl;
}
test(int x)
{
val=x;
}
virtual void fun2()
{
cout<<"Base class function fun2"<<endl;
}
};class Housetest:public test
{
int houseval;
public:
Housetest()
{
cout<<"I AM DEFAUTL CONSTRUCNTOR FOR HOUSE TEST"<<endl;
cout<<"House Test dont have fun1"<<endl;
}
Housetest(int x):test(x)
{
houseval=x;
cout<<"Value of x"<<houseval;
}
void fun1()
{
cout<<" I am fun1 from house test"<<endl;
}
};
class classtest:public test
{
int classval;

public:
classtest()
{
cout<<"Class test dont have fun1"<<endl;
}
void fun2()
{
cout<<" I AM FUN2 FROM CLASS TEST"<<endl;
}
};

int _tmain(int argc, _TCHAR* argv[])
{
test *x= new classtest;

x->fun1();
x->fun2();
cout<<"----------------------------------------------------\n"<<endl;
cout<<"I am creating a new object of House test now"<<endl;
cout<<"----------------------------------------------------\n"<<endl;
x=new Housetest;
x->fun1();
x->fun2();
return 0;
}

No comments:

Post a Comment

Apache Beam Learning In Java Tutorials

 Wow  https://youtu.be/9kGETU63rkc