0% found this document useful (0 votes)
74 views7 pages

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 point to objects of both the base class and derived classes. However, a derived class pointer can only point to objects of the derived class, not the base class. The document then provides an example program demonstrating using a base class pointer to call functions on both base and derived class objects. It also summarizes the order of execution of constructors and destructors when a derived class object is created - the base class constructor is called first, followed by member initializers and the derived class constructor body, and in reverse order for destructors. Finally, it provides syntax and an example of single inheritance using the private and protected access specifiers.

Uploaded by

BabuKannan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views7 pages

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 point to objects of both the base class and derived classes. However, a derived class pointer can only point to objects of the derived class, not the base class. The document then provides an example program demonstrating using a base class pointer to call functions on both base and derived class objects. It also summarizes the order of execution of constructors and destructors when a derived class object is created - the base class constructor is called first, followed by member initializers and the derived class constructor body, and in reverse order for destructors. Finally, it provides syntax and an example of single inheritance using the private and protected access specifiers.

Uploaded by

BabuKannan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Casting Class Pointer s and Member Function

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

Cout<Calling Put() function Base pointer pointing Base object;


bp->put();
derived d;
bp=&d;
Cout<<Calling put() using Base Pointer pointing Derived objects;
bp->put();
getch();
}
**********____________________************
Constructors and Destructors in Derived Classes:
Derived Class Constructor:
*Used to create and initialize derived class object
*Invoke base class constructor before invoking the derived class initialize list & constructor
body
*It invokes base class constructor either
-> Implicitly (Via base class member initializer)
->Explicitly (by calling the base classes default constructor)
* When a program creates a derived class object ,the order of execution for initializing
constructors in base and member constructors is given in the following list.
Derived class constructor immediately calls the base class constructor
Then Base class constructors body will be executed
Derived classs member intializers will be executed and finally derived class constructors body
will be executed
Derived class constructor
Derived classs destructors body is executed first.
Then destructors of derived class members will be executed and then
Destructor of base class will be called and the object is removed from memory

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

Single Inheritance- Protected Mode Derivation


Syntax:
Class derived_class_name:protected base_class_name
{
..
}
Program:
Class base
{
int a;
public:
int b;
void get()
{
cout<,Enter value of A;
cin>>a;
cout<<Enter value of B;
cin>>b;
}
int reta()
{
return a;
}}:
Class derived:private base
{
OOPS

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

You might also like