Converting a value from one data type to another is called typecasting or casting for short. Casting an object from one class type to another makes sense when the original class type is a subclass of the destination type. For example, if Circle is a subclass of Shape (see Figure 4 below), a Circle "is a" Shape, so it makes sense to think of a Circle as a Shape or to convert an instance of Circle to an instance of Shape. However, it doesn't make sense to convert an Employee to Shape. Although we didn't call it "upcasting" at the time, we've used this operation previously.
friend ostream& operator<<(ostream& out, Actor& me)
{
	out << (Person &)me << " " << me.agent << endl;
	return out;
}
Upcasting, as its name suggests, combines two concepts. First, a casting operation converts a value represented by one data type to an appropriate value represented by a different type. Second, when we cast objects, we always do so in the context of inheritance. If we arrange the inheritance hierarchy with each superclass above its subclasses, casting from a subclass to its superclass always takes place upwards in an inheritance hierarchy. (However, you should keep in mind that as UML class diagrams become large and complex, it's not always possible to place superclasses above their subclasses; we use "upwards" to mean "in the direction of the inheritance arrows.")
|  | 
 | 
| (a) | (b) | 
| D my_d; A* a_ptr = &D; | A* a_ptr = new D; | 
| (a) | (b) | 
In actual programs, upcasting is not as overt as in the above examples. Programs typically perform an upcast when calling a function and passing an instance of a subclass as an argument to a parameter of superclass type. It's far more difficult to recognize upcasting implemented this way.
|   | void render(Shape* s)
{
	.
	.
	.
}
    .
    .
    .
Circle* c = new Circle;
render(c); | 
| (a) | (b) | 
Shape* s = new Circle;. The ability to replace or substitute an instance of a subclass (in the function call) for an instance of a superclass (in the function definition) is an important feature of inheritance called Substitutability.
It is also possible and sometimes necessary to cast "downwards" in an inheritance hierarchy. (Recall that it's not always possible to place superclasses above their subclasses, so we use "downwards" to mean "in the opposite direction of the inheritance arrows.") However, downcasting is somewhat riskier than upcasting and requires an explicit cast operation. So, upcasting is safe and the compiler performs it automatically, but downcasting is risky and the compiler requires us to perform it explicitly. Chapter 2 introduced the casting operator and we extend it to classes and objects here.
Shape* s = new Circle(0xff0000, 10); // safe upcast Circle* c = (Circle *)s; // unsafe downcast