AlexLecture_Polymorphism

AlexLecture_Polymorphism - Operation vs. Method...

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

View Full Document Right Arrow Icon
1 Polymorphism in C++ Operation vs. Method empl.print(cout); } int main(void){ Employee anEmployee(“John”, “Doe”, 235); Manager aManager(“Charles”, “Howell”, 235, 3); Question: Which method invoked when actual parameter is Manager? 2 Motivation: we should be able to write functions that invoke an operation on an object, rather than invoking a particular method In C++, this is accomplished by declaring a function member to be virtual myPrint(anEmployee); myPrint(aManager); } Virtual function class Employee { private: string first_name, last_name; short department; public: virtual void print() const { cout << “Name:” << first_name << “ “ << last_name<< endl << “Dept: “ << department; class Manager : public Employee { private: list<Employee*> group; short level; public: virtual void print() const{ Employee::print(os); os << “Level: “ << level; } 3 << Dept: << department; }; }; }; }; Observe: operation declared virtual in base class. Observe: no need to “redeclare” print as virtual in derived class. But it’s good to make it clear. void myPrint( Employee empl ){ empl.print(cout); }; int main(void){ Employee aEmployee(“John”, “Doe”, 235); Manager aManager(“Charles”, “Howell”, 235, 3); aEmployee.print(cout); aManager.print(cout); // invokes Employee::print() // invokes Manager::print() 4 Upcasting: objects of a derived class can be assigned to pointers and references to a base class. Employee * ePtr = &aManager; ePtr->print(cout); myPrint(aEmployee); myPrint(aManager); } // invokes Manager::print() // invokes Manager::print() // invokes Employee::print() void myPrint( Employee empl ){ empl.print(cout); }; int main(void){ Employee aEmployee(“John”, “Doe”, 235); Manager aManager(“Charles”, “Howell”, 235, 3); aEmployee.print(cout); // invokes Employee::print() aManager.print(cout); // invokes Manager::print() 5 Upcasting: cast from a derived class to a base class In this assignment, only a part, or a slice, of the object aManager is copied to the object empl. All new data and function members from the derived class are sliced off. Slicing is dangerous because it makes access to the derived data and function members impossible. Employee * ePtr = &aManager; ePtr->print(cout); // invokes Manager ::print() myPrint(aEmployee); myPrint(aManager); } // invokes Employee::print() // invokes Employee::print() Exercise: heterogeneous containers We want to store different geometric objects in a list and compute their area. Develop a class hierarchy with classes Shape, Circle, and Rectangle. It should be possible to declare: list<Shape*> myShapes; Circle* circ = new Circle(10); Rectangle* rect = new Rectangle(20, 40); myShapes.push_back(circ); Sh h b k( t) 6 Heterogeneous containers: inheritance and virtual functions useful for implementing containers that can hold different types of elements
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

AlexLecture_Polymorphism - Operation vs. Method...

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

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