static_cast c++

#include <iostream>

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

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

int main() {
    Base baseObj;
    Derived derivedObj;

    // Using static_cast to cast from Base to Derived
    Base* basePtr = &baseObj;
    Derived derivedPtr = static_cast<Derived>(basePtr);

    // Using static_cast to cast from Derived to Base
    Derived* derivedPtr2 = &derivedObj;
    Base basePtr2 = static_cast<Base>(derivedPtr2);

    // Accessing virtual function through basePtr after casting
    basePtr->display();

    // Accessing overridden function through derivedPtr2 after casting
    derivedPtr2->display();

    return 0;
}