PART2:Virtual Member Function With Pointers
//RULE
//RUlE2:pointer to object of derived class are compatible with pointer to object of base class
//RULE1:Compiler will choose membr func according to content of ponter not the type
#include<iostream>
using namespace std;
class base
{
public:
virtual void show()
{cout<<"Base";
}
};
class derv1:public base
{
void show()
{
cout<<"Derived1";
}
};
class derv2:public base
{
void show()
{
cout<<"Derived2";
}
};
int main()
{
derv1 obj1;
derv2 obj2;
base *ptr;
ptr=&obj1;
ptr->show();
ptr=&obj2;
ptr->show();
return 0;
}
"Nice One Program"
//RUlE2:pointer to object of derived class are compatible with pointer to object of base class
//RULE1:Compiler will choose membr func according to content of ponter not the type
#include<iostream>
using namespace std;
class base
{
public:
virtual void show()
{cout<<"Base";
}
};
class derv1:public base
{
void show()
{
cout<<"Derived1";
}
};
class derv2:public base
{
void show()
{
cout<<"Derived2";
}
};
int main()
{
derv1 obj1;
derv2 obj2;
base *ptr;
ptr=&obj1;
ptr->show();
ptr=&obj2;
ptr->show();
return 0;
}
No comments:
Post a Comment