{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

T11_InPoly

# T11_InPoly - Tutorial 12 COMP152 Spring 2010 Inheritance...

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

Inheritance and Polymorphism Tutorial 12 COMP152 Spring 2010

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

View Full Document
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

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

View Full Document
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
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

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

View Full Document
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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online