Lec5 - Lecture 5 Polymorphism PIC 10C Todd Wittman Example...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lecture 5: Polymorphism PIC 10C Todd Wittman Example: r Last lecture we introduced 2 classes. Base Class--Employee Derived Class--Manager name name salary salary dept r For this lecture, let's assume the classes have basic constructor and accessor functions. Base Class--Employee Derived Class--Manager Employee() Manager() Employee(name,salary) Manager(name,salary,dept) get_name() get_name() get_salary() get_salary() get_dept() r The two get_salary() functions may be different! inherited from Employee as protected variables
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 r What happens if we mix the classes together in a STL container? r Case 1 : If we try to put an Employee into a vector of Managers, we get an error. We cannot static cast the base class into the derived class ( downcast ). vector<Manager> v; Employee e; v.push_back(e); //Error! r Case 2 : If we try to put a Manager into a vector of Employees, C++ automatically casts the derived class into the base class ( upcast ). This means we "slice away" the extra variable dept. vector<Employee> v; Manager m; v.push_back(m); //Works fine. v[0].get_dept(); //Error! v[0] is now Employee 8.4 : Polymorphism Poly morph many forms r Polymorphism refers to the art of handling a set of different objects. Specifically, it means
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 6

Lec5 - Lecture 5 Polymorphism PIC 10C Todd Wittman Example...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online