C++ Tutorial

Class and Object

Reference

Inheritance and Derivation

Polymorphism and Virtual Functions

Operator Overloading

Template

Exception

Object Oriented Advanced

Input/Output Stream

File Operations

C++ Assign Derived Class To Base Class (Upcasting)

In this tutorial, we will learn about assigning a derived class object to a base class object, also known as upcasting, in C++. Upcasting is a common operation in object-oriented programming, allowing you to treat objects of derived classes as objects of their base class. Upcasting is generally safe because a derived class object is guaranteed to have all the members of its base class, so no information is lost in the process.

  • Basic example of upcasting:

Here's a simple example demonstrating upcasting:

#include <iostream>

class Base {
public:
    void print() {
        std::cout << "Base class" << std::endl;
    }
};

class Derived : public Base {
public:
    void print() {
        std::cout << "Derived class" << std::endl;
    }
};

int main() {
    Derived derivedObj;
    Base baseObj = derivedObj; // Assigning a derived class object to a base class object (upcasting)

    baseObj.print(); // Output: Base class
    return 0;
}

In this example, we define a Base class and a Derived class that inherits from Base. Both classes have a print() member function. In the main() function, we create a Derived object and assign it to a Base object. This assignment performs upcasting.

  • Upcasting with pointers:

Upcasting can also be done with pointers:

#include <iostream>

class Base {
public:
    virtual void print() {
        std::cout << "Base class" << std::endl;
    }
};

class Derived : public Base {
public:
    void print() override {
        std::cout << "Derived class" << std::endl;
    }
};

int main() {
    Derived* derivedPtr = new Derived();
    Base* basePtr = derivedPtr; // Upcasting derived class pointer to base class pointer

    basePtr->print(); // Output: Derived class

    delete derivedPtr;
    return 0;
}

In this example, we use virtual functions to allow for runtime polymorphism. We create a pointer to a Derived object, then assign it to a pointer to a Base object. This is an example of upcasting with pointers. When we call the print() function through the basePtr, it correctly calls the Derived class's implementation of the function.

That's it for our C++ upcasting tutorial. Upcasting is a powerful technique that allows you to work with objects of derived classes through base class pointers or references. This enables you to write more generic and reusable code, while still taking advantage of the object-oriented programming features provided by C++.

  1. C++ upcasting examples:

    #include <iostream>
    
    class Base {
    public:
        virtual void display() const {
            std::cout << "Base class display" << std::endl;
        }
    };
    
    class Derived : public Base {
    public:
        void display() const override {
            std::cout << "Derived class display" << std::endl;
        }
    };
    
    int main() {
        Derived derivedObj;
        Base* basePtr = &derivedObj; // Upcasting from Derived to Base
    
        basePtr->display(); // Calls the display method of Derived class
    
        return 0;
    }
    
    • Upcasting involves converting a derived class pointer or reference to a base class pointer or reference.
  2. How to assign derived class object to base class pointer in C++:

    #include <iostream>
    
    class Base {
    public:
        virtual void display() const {
            std::cout << "Base class display" << std::endl;
        }
    };
    
    class Derived : public Base {
    public:
        void display() const override {
            std::cout << "Derived class display" << std::endl;
        }
    };
    
    int main() {
        Derived derivedObj;
        Base* basePtr = &derivedObj; // Assigning Derived class object to Base class pointer
    
        basePtr->display(); // Calls the display method of Derived class
    
        return 0;
    }
    
  3. Dynamic_cast for upcasting in C++:

    • dynamic_cast is commonly used for safe upcasting in polymorphic classes.
    #include <iostream>
    
    class Base {
    public:
        virtual void display() const {
            std::cout << "Base class display" << std::endl;
        }
    };
    
    class Derived : public Base {
    public:
        void display() const override {
            std::cout << "Derived class display" << std::endl;
        }
    };
    
    int main() {
        Base* basePtr = new Derived();
    
        if (Derived* derivedPtr = dynamic_cast<Derived*>(basePtr)) {
            // Upcasting using dynamic_cast
            derivedPtr->display();
        }
    
        delete basePtr;
    
        return 0;
    }
    
    • dynamic_cast checks the actual type of the object and performs safe upcasting.