T11_InPoly - Tutorial 12 COMP152 Spring 2010 Inheritance...

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

View Full Document Right Arrow Icon
Inheritance and Polymorphism Tutorial 12 COMP152 Spring 2010
Background image of page 1

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

View Full DocumentRight Arrow Icon
Outline Inheritance and Object-Oriented Design Motivation of inheritance and building derived classes Order of Construction and Destruction Access control and Types of inheritance Multiple Inheritance Polymorphism Virtual functions Abstract Base Class (ABC) 2
Background image of page 2
Motivation of Inheritance enum Department { accounting, business, engineering, mathematics, unknown }; class Student { private: string name; string address; Department dept; Course enrolled; int num courses; public: Student(string n, string a, Department d) : name(n), address(a), dept(d), enrolled(NULL), num courses(0) { }; void set_name(const char* name); void set_address(const char* adr); void set_department(Department dept); string get_name() const; string get_address() const; Department get_department() const; bool enroll_course(const string &); bool drop_course(const Course &); }; enum Rank { instructor, assistant prof, associate prof, professor, dean }; class Teacher { private: string name; string address; Department dept; Rank rank; public: Teacher(string n, string a, Department d, Rank r) : name(n), address(a), dept(d), rank(r) { }; void set_name(const char* mame); void set_address(const char* adr); void set_department(Department dept); string get_name() const; string get_address() const; Department get_department() const; void set_rank(Rank rank); Rank get_rank() const; }; 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
Motivation of Inheritance (cont.) Method 1: Re-use by copying Method 2: By Inheritance enum Department { accounting, business, engineering, mathematics, unknown }; class Person { private: string name; string address; Department dept; public: Person(string n, string a, Department d) : name(n), address(a), dept(d) { }; void set_name(const char* name); void set_address(const char* adr); void set_department(Department dept); string get_name() const; string get_address() const; Department get_department() const; }; 4
Background image of page 4
Motivation of Inheritance (cont.) class Student : public Person { private: Course *enrolled; int num_courses; public: Student(string n, string a, Department d) : Person(n, a, d) , enrolled(NULL), num_courses(0) { } bool enroll_course(const string &); bool drop_course(const Course &); }; enum Rank { instructor, assistant prof, associate prof, professor, dean }; class Teacher : public Person { private: Rank rank; public: Teacher(string n, string a, Department d, Rank r) : Person(n, a, d) , rank(r) { } void set_rank(Rank rank); Rank get_rank() const; }; 5 Initializing Base Class Object
Background image of page 5

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

View Full DocumentRight Arrow Icon
Inheritance Enables code reuse. Ability to define a new class based on an existing class with a hierarchy. The derived class inherits the data members and member methods of the base class . Student and Teacher are the derived class of Person (base class) and inherit all the data members and member methods of Person. All data members and member methods of Person are reused. New members and methods can be added to the derived class.
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 08/25/2010 for the course COMP COMP152 taught by Professor D.y.yeung during the Spring '10 term at HKUST.

Page1 / 32

T11_InPoly - Tutorial 12 COMP152 Spring 2010 Inheritance...

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