Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.

Similar presentations


Presentation on theme: "OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4."— Presentation transcript:

1 OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4

2 OOP Spring 2007 – Recitation 42 Inheritance Basis of Object-Oriented Programming

3 OOP Spring 2007 – Recitation 43 Where It Comes From? As we try to model the “real world” in C++, we run into the need to model hierarchical relationships between classes – to express commonality. A Student and a Teacher have something in common – they are both Persons.

4 OOP Spring 2007 – Recitation 44 Terminology The mechanism that allows this in C++ is called inheritance. We say that –Student inherits from Person. –Person is a base class and Student is a derived class. –Person is a superclass and Student is a subclass. The public inheritance models the is-a relationship.

5 OOP Spring 2007 – Recitation 45 What Does It Mean? Any place a base can be used, a derived can be used. A derived inherits base’s methods and data members (but not all). In practice, a derived class extends the base class, or specializes it. –A Student can do anything a Person can and more. –A Person is more general than a Student. Student is-a kind of Person.

6 OOP Spring 2007 – Recitation 46 Syntax class B { public: … protected: … private: … }; class D : public B { public: … private: … }; Derived class name Inheritance type Base class name

7 OOP Spring 2007 – Recitation 47 Syntax Explained The syntax for saying that D inherits from B is: : public B before class’s opening brace. The public keyword is essential (this is called public inheritance). protected: is a new access level – anything in the protected zone can be accessed by the derived classes only.

8 OOP Spring 2007 – Recitation 48 Student s and Person s class Person { public: void print_name(); protected: char* get_name(); private: char* name; }; class Student : public Person { public: void print_univ(); private: char* univ; };

9 OOP Spring 2007 – Recitation 49 Their Use int main() { Student s; s.print_name(); s.print_univ(); cout << s.univ; cout << s.name; cout << s.get_name(); } // Prints the name // Prints the university // Can't access

10 OOP Spring 2007 – Recitation 410 Their Implementation void Student::print_univ() { cout << univ; cout << name; cout << get_name(); } void Person::print_name() { cout << name; } // Student’s member // Can't access // Inherited protected member

11 OOP Spring 2007 – Recitation 411 Class Hierarchy Base doesn’t know about derived classes. Person should not “think about” Student in particular BUT Person should “think about” things common to all derived classes.

12 OOP Spring 2007 – Recitation 412 The (Limited) Benefit void introduce (Person p) { cout << "Hello, my name is "; p.print_name(); } int main() { Person p; Student s; introduce(p); introduce(s); return 0; }

13 OOP Spring 2007 – Recitation 413 Constructing and Assigning Derived Classes

14 OOP Spring 2007 – Recitation 414 Constructors and Inheritance An object of a derived class “includes” members of the base class. When constructing an object all members should be initialized. Therefore derived’s constructors needs to make sure the members inherited from base are initialized correctly. It does it by “calling” base’s constructor.

15 OOP Spring 2007 – Recitation 415 Example Person::Person(char* n) { … } Student::Student(char* name, char* u) : Person(n) { … }

16 OOP Spring 2007 – Recitation 416 Construction Order Note the derived’s construction order: –First all base classes are constructed. –Then all data members are constructed. –Then the constructor’s code is executed. The destruction is in reversed order. –Destructor’s code is executed. –Data members are destructed. –Base classes are destructed. If we do not “call” base’s constructor, it’s default constructor will be called.

17 OOP Spring 2007 – Recitation 417 Who Should Do It? These “calls” to base’s constructor should be made in all derived’s constructors (including copy constructor). Other derived’s functions may need to “work with” base’s members. For example, operator= must assign to base’s members too. It’s easy by calling appropriate method from base. Compiler generated methods do this by default.

18 OOP Spring 2007 – Recitation 418 operator= Example Student& Student::operator=(const Student& rhs) { Person::operator=(rhs);// Calls base's operator= univ = rhs.univ; return *this; }


Download ppt "OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4."

Similar presentations


Ads by Google