C++ Language



Admission Enquiry Form

  

Pointer to Base Class in C++ Inheritance


Pointer to Base Class in C++

//pointer to base class in C++
#include<iostream>
using namespace std;
class Base
{
public:
void fun1()
{
cout<<endl<<"I am fun1 of Base class";
}
void fun2()
{
cout<<endl<<"I am fun2 of Base class";
}
void display()
{
cout<<endl<<"I am display of Base class";
}
};//end of Base class
class Derived : public Base
{
public:
void fun3()
{
cout<<endl<<"I am fun3 of derived class";
}
void fun4()
{
cout<<endl<<"I am fun4 of derived class";
}
void display()
{
cout<<endl<<"I am display of Derived class";
}

};//end of Derived class
int main()
{
Base b,*bptr;//object and pointer to Base class
Derived d;//object of derived class
bptr=&d;//pointing to derived class
bptr->fun1();// is the function of Base class
bptr->fun2();// is the function of Base class
bptr->display();// is the function of Base class

}//end of main
/*Output:
I am fun1 of Base class
I am fun2 of Base class
I am display of Base class*/ 

int main()
{
Base b,*bptr;//object and pointer to Base class
Derived d;//object of derived class
bptr=&d;//pointing to derived class
bptr->fun1();// is the function of Base class
bptr->fun2();// is the function of Base class
bptr->display();// is the function of Base class
bptr->fun3();//is the function of Derived class.

bptr->fun3() Will display error not member of class

}//end of main