Lecture09-Inheritance_Part_2

Lecture09-Inheritanc - Inheritance Part II 1 Computer Programming II Learning Objectives l l l l l To understand the call order of constructor and

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

View Full Document Right Arrow Icon
Computer Programming II 1 Inheritance Part II
Background image of page 1

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

View Full DocumentRight Arrow Icon
Computer Programming II 2 Learning Objectives l To understand the call order of constructor and destructor in inheritance l To understand method overriding in inheritance l To understand protected access privilege l To understand private, public and protected Inheritance l To understand upcasting & downcasting
Background image of page 2
Computer Programming II 3 Constructor vs. Destructor Call Order in Inheritance l When creating instances of a subclass (or derived class), the constructors are called starting at the highest point in the inheritance hierarchy (the most general superclass/base class) and moving downwards towards the subclass l When destroying instances of a subclass , the destructors are called starting at the subclass itself and moving upwards through the superclass(es) l The call order of destructors is the reverse of the call order of constructors
Background image of page 3

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

View Full DocumentRight Arrow Icon
Computer Programming II 4 Constructor vs. Destructor Call Order class Human { string name; public: Human (string name) : name(name) { cout << "Human " << name << " created.\n"; } ~Human () { cout << "Human " << name << " destroyed.\n"; } }; Output: Human Michael created. Student 111 created. Human Kelly created. Student 222 created. Student 222 destroyed. Human Kelly destroyed. Student 111 destroyed. Human Michael destroyed. class Student : public Human { int id; public: Student (int id, string name) : Human(name), id(id) { cout << "Student " << id << " created.\n"; } ~Student () { cout << "Student " << id << " destroyed.\n"; } }; int main() { // create s1 Student s1 (111, "Michael"); Student *s2 = new Student (222, "Kelly"); // create s2 delete s2; // destroy s2 } // destroy s1
Background image of page 4
Computer Programming II Function Overriding l A subclass can override a superclass method (member function) by supplying a new version of that method with the same signature. l When the method is mentioned by name in the subclass, the subclass version is automatically selected.
Background image of page 5

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

View Full DocumentRight Arrow Icon
Computer Programming II 6 Function Overriding class Human { public: void eat () { cout << "Eating\n"; } void speak () { cout << "I'm a human\n"; } }; class Student : public Human { public: void speak () { // override superclass
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/03/2012 for the course IT 1241 taught by Professor Pang during the Spring '11 term at Multimedia University, Cyberjaya.

Page1 / 20

Lecture09-Inheritanc - Inheritance Part II 1 Computer Programming II Learning Objectives l l l l l To understand the call order of constructor and

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

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