Casting Class Pointer S and Member Function A Base Class Pointer Can Also Point To Objects of Base and Derived Classes
Casting Class Pointer S and Member Function A Base Class Pointer Can Also Point To Objects of Base and Derived Classes
A base class pointer can also point to objects of base and derived classes
A pointer to base class object can point to objects of derived classes whereas a pointer to derived
class object cannot point to objects of base class object
Pgm:
Class Base
{
Public:
Void put()
{
Cout <<Function put in Base class Called;
}};
Class derived:public base
{
Public:
Void display ()
{
Cout<<Function Display in Derived class
}};
Void main()
{
Base b,*bp;
clrscr();
cout<<Pgm for Accessing object using base class pointer
clrscr();
bp=&b;
OOPS
Page 1
OOPS
Page 2
Class base
{
int i;
public:
base(int a)
{
i=a;
cout<<Base class the value of a:<<a;
}
~base()
{
cout<<Destructor Base class;
}};
class derived:public base
{
int j;
Public:
derived(int a,int b): base(a)
{
J=b;
cout<<Derived class value J<<j;
}
~derived ()
{
cout<<destructor in derived class called;
}};
OOPS
Page 3
void main()
{
clrscr();
cout<<Constructor & Destructor Inheritence;
{
derived d(10,20);
}
getch();
}
**********____________________************
Single Inheritance in Private Mode:
Syntax:
Class derived_class_name:private base_class_name
{
..
}
Program:
Class base
{
int a;
public:
int b;
void get()
{
cout<,Enter value of A;
cin>>a;
OOPS
Page 4
cout<<Enter value of B;
cin>>b;
}
int reta()
{
return a;
}}:
Class derived:private base
{
int c;
public:
void put()
{
Get();
c=reta()+b;
cout<<sum=<<c;
}};
void main()
{
derived d;
clrscr();
d.put();
getch();
}
OOPS
Page 5
Page 6
int c;
public:
void put()
{
Get();
c=reta()+b;
cout<<sum=<<c;
}};
void main()
{
derived d;
clrscr();
d.put();
getch();
}
OOPS
Page 7