![]() Modify base class Person by adding a virtual function: virtual void foo() If you want to use a dynamic cast for downcasting, the base class should be polymorphic – it must have at least one virtual function. What will happen, if we try to downcast a base class pointer that is pointing to an object of the base class and not to an object of derived class? Try to compile and run this code: Employee e1("Peter", "Green", 1400) This code compiles and runs without any problem because emp points to an object of Manager class. explicit downcasting from Employee to Manager You have to use an explicit cast for downcasting: //pointer to base class object For example, a Manager is always a Person But a Person is not always a Manager. You know that a derived class object can be always treated as a base class object. It means that you have to specify explicit typecast.ĭowncasting is not as safe as upcasting. It converts base class pointer to derived class pointer. DOWNCASTINGĭowncasting is an opposite process for upcasting. Elements of the derived class (yellow area) are not accessible when you use a base class pointer. ![]() However, it represents the fact that when you use a base class pointer to point up an object of the derived class, you can access only elements that are defined in the base class (green area). Of course, this model is a simplified view of memory layout for objects. Look on the memory layout of the Employee and Manager classes: It means that derived class has properties (data members and member functions) of the base class and defines new data members and member functions. Memory layoutĪs you know, the derived class extends the properties of the base class. When you call it with objects of type Manager and Person, they will be automatically upcasted to Employee class: //automatic upcastingĪn example of how to use upcasting with virtual functions is described in the “ C++ Polymorphism” topic. This function will work with all the classes that are derived from the Employee class. Look at an example: void congratulate(Employee* emp) One of the biggest advantages of upcasting is the capability of writing generic functions for all the classes that are derived from the same base class. Nevertheless, when you upcast an object, you will be able to access only member functions and data members that are defined in the base class: //It's okĮmp->getComm() Example of upcasting usage When you use upcasting, the object is not changing. You just need to assign derived class pointer (or reference) to base class pointer: //pointer to base class object ![]() Upcasting is a process of creating a pointer or a reference of the derived class object as a base class pointer. When you use upcasting or downcasting you just “label” an object in different ways. You have to understand it before we start learning upcasting and downcasting.īoth upcasting and downcasting do not change the object by itself. Therefore, the Manager and Clerk are Persons too. The Manager takes a commission fee for every contract, and the Clerk has information about his Manager:Įmployee(string fName, string lName, double sal) ![]() We have to specify these properties only once in the Employee class:Īt the same time, the Manager and Clerk classes are different. What does it mean? It means that Manager and Clerk classes inherit properties of Employee class, which inherits properties of Person class.įor example, we don’t need to specify that both Manager and Clerk are identified by First and Last name, have a salary you can show information about them and add a bonus to their salaries. In this topic, we will use the following hierarchy of classes:Īs you can see, Manager and Clerk are both Employee. This is upcasting.ĭowncasting is an opposite process, which consists of converting base class pointer (or reference) to derived class pointer.Ĭ++ Upcasting and Downcasting should not be understood as a simple casting of different data types. It can be achieved by using Polymorphism.Ĭ++ allows that a derived class pointer (or reference) to be treated as a base class pointer. Upcasting and downcasting give a possibility to build complicated programs with a simple syntax. Upcasting and downcasting are an important part of C++.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |