Upcasting

The assignment of child class object to a parent class reference in C++ inheritance is known as up-casting.

CODE

EG 1 . Creating a child class, parent class and using Upcasting

#include <iostream>
using namespace std;

class Rectangle2D {
  public:
    double length, width;
    double Area() {
        return length * width;
    }
    double Perimeter() {
        return 2 * (length+width);
    }
};

class Rectangle3D : public Rectangle2D {
  public:
    double height;
    double Volume() {
        return length * width * height;
    }
};

int main() {
    //UPCASTING
    Rectangle2D* r1 = new Rectangle3D();
}