Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism

Casting Class Pointers and Member Functions

Data Structures using C++ Program

Questions: 1. Write short note on Down casting. 2. Explain Class Object to Base and Base to Class Object conversions using C++ with suitable example.

Casting Class Pointers and Member Functions

• Casting means converting a value from one data type to another.

• The class pointer can be cast by the base class or by the derived class.

• If the base class pointer is cast by the derived class then it is called up‒casting

• If the derived class pointer is cast by the base class then it is called down‒casting

• The member functions can then be accessed using these pointers. Let us discuss these techniques with the help of programming examples ‒


1. Casting Operator

 

• The cast operators are used to convert one data type element into another.

• As an operator cast is unary and has same precedence as unary operator.

• The most general form of cast operator supported by C++ is as

(type) expression

Where type is the desired data type.

Various cast operators supported in C++ are


1) const_cast<type>(expression) :

This type of cast operator is used to cast away the constantness of object. In this case we can declare the variable as const and can use const_cast when we need to alter the value of this variable.

Example Program

#include<iostream>

using namespace std;

int main()

{

   const int a = 10;

   int b = 20;

   const int *ptr1=&a;

   cout << "\n Const Value: "<<*ptr1;

   int *ptr2 = const_cast<int *>(ptr1);

   ptr2 = &b;

   cout << "\n Const Value after using const cast: "<<*ptr2;

   return 0;

}

Output

Const Value : 10

Const Value after using const_cast : 20


2) dynamic_cast<type>(expression) :

The dynamic_cast is used to perform safe downcasting, i.e., to determine whether an object is of a particular type in an inheritance hierarchy.

Example Program

#include <iostream>

using namespace std;

class Base

{

   virtual void display() {}

};

class Derived: public Base

{

   int a

};

int main()

{

   Base ptr1 = new Derived;

   Base * ptr2 = new Base;

   Derived *pd;

   pd = dynamic_cast<Derived">(ptr1);

   if (pd = = 0)

         cout << "Null pointer on first type‒cast" << endl;

   pd = dynamic_cast<Derived">(ptr2);

   if (pd = = 0)

        cout << "Null pointer on second type‒cast" << endl;

   return 0;

}

Output

Null pointer on second type‒cast

 

3) static_cast<type>(expression) :

The static_cast can be used to force implicit conversions such as non‒const object to const, int to double. It can be also be used to perform the reverse of many conversions such as void* pointers to typed pointers, base pointers to derived pointers. But it cannot cast from const to non‒const object.

Example Program

#include<iostream>

using namespace std;

class Base {};

class Derived: public Base {};

int main()

{

   Base base;

   Derived derived;

   Base *pBase = static_cast<Base *>(&derived); //upcast

   Derived. *pDerived = static_cast<<Derived *> (&base); //downcast

}

 

4) reinterprete_cast<type>(expression) :

The reinterpret_cast converts any pointer type to any other pointer type, even of unrelated classes. The operation result is a simple binary copy of the value from one pointer to the other. It is not guaranteed to be portable. In fact, it is best to assume that reinterpret_cast is not portable at all.

Example Program

#include<iostream>

using namespace std;

class A {};

class B {};

int main()

{

   A* pA = new A;

   B* pB = reinterpret_cast<B*>(pA);

}

 

2. Up Casting (Implicit Derived Class Object to Base Class Object Conversion)

 

• When we create an object of a derived class, C++ allows us to automatically convert it into a base class object, without requiring any explicit cast.

• This is called implicit conversion (or upcasting). That means a derived class contains everything the base class has.

• This type of conversion is safe.

#include<iostream>

using namespace std;

class Base {

public:

void show(){

   cout<<"This function is in base class!!!";

}

};

class Derived:public Base {

public:

void display(){

   cout<<"This function is in derived class!!!";

}

};

int main(){

   Derived d;

   Base b;

   b=d;

   b.show();

   b.display();//error

   return 0;

}

Output

error: 'class Base' has no member named 'display'

b.display();

Program Explanation : In above program,

• We have created a base class and from this base class the class named Derived is derived.

• The Derived class has access to both the functions show() and display().

• Then we have b=d, due to this assignment only Base part of d is copied into b. The derived part is sliced off. This is also called as object slicing.


3. Down Casting

 

• A downcast is a cast from a base class to a class derived from that base class. The upcast is a cast from derived class to the base class. The upcast is safe and implicit but the down cast needs to be done carefully. In the following C++ program we are performing downcasting and then invoking the methods of corresponding classes.

#include<iostream>

using namespace std;

class Base{

public:

void show(){

   cout<<"This function is in base class!!!";

}

};

class Derived:public Base {

public:

void display(){

   cout<<"This function is in derived class!!!";

}

};

int main()

{

   Base b;

   Derived* dptr= (Derived*)&b;//downcast

   dptr‒>display(); //undefined behaviour

   return 0;

}

• The above program will result into undefined behaviour because b is a Base object, not a Derived object. Forcing a downcast makes C++ think it is Derived, which is false and leads to runtime errors.

 


Review Questions

1. Write short note on Down casting.

2. Explain Class Object to Base and Base to Class Object conversions using C++ with suitable example.

 

Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism : Tag: Data Structure, C++, C Programing : Data Structures using C++ Program - Casting Class Pointers and Member Functions


Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism



Under Subject


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Electron Devices

EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Circuits and Network Analysis

EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Engineering Drawing - Laboratory

ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus - Laboratory

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Devices and Circuits Laboratory

EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation