Inheritance: Part I.

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Advertisements

Outline 1 Introduction 2 Base Classes and Derived Classes 3 protected Members 4 Relationship between Base Classes and Derived Classes 5 Case Study: Three-Level.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Derived Classes in C++CS-2303, C-Term Derived Classes in C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
C++ Inheritance Systems Programming.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance Deitel &Deitel Java SE 8.
 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance Multiple inheritence –Derived class has several base classes –Powerful, but.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Slide 1 Inheritance: Part I. Slide 2 Structuring ‘classes’ class Employee { string firstName; string familyName; … } class Manager { Employee e; list.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Object Oriented Programming: Inheritance Chapter 9.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
More on Object-Oriented Programming: Inheritance 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Object Oriented Programming in C++ Chapter 6 Inheritance.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
Slide 1 Polymorphism: Part I. Slide 2 Summary: Derived Class * ‘is-a’ relationship n Different from ‘has-a’ or ‘uses-a’, or … by class-in-class * Public.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Inheritance.
Object Oriented Programming
CSCI 383 Object-Oriented Programming & Design Lecture 19 Martin van Bommel.
Inheritance ndex.html ndex.htmland “Java.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object Oriented Programming: Inheritance Chapter 9.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Inheritance CMSC 202, Version 4/02.
Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Inheritance & Polymorphism
Polymorphism & Virtual Functions
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming: Inheritance
Chapter 22 - Other Topics Outline 22.1 Introduction
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Understanding Inheritance
Introduction to Classes
Chapter 9 Object-Oriented Programming: Inheritance
Lecture 22 Inheritance Richard Gesick.
Virtual Functions Department of CSE, BUET Chapter 10.
Object-Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Object-Oriented Programming: Inheritance
Polymorphism: Part I.
Chapter 19 - Inheritance Outline 19.1 Introduction
Fundaments of Game Design
Recitation Course 0610 Speaker: Liu Yu-Jiun.
Object-Oriented Programming: Inheritance
Final and Abstract Classes
COP 3330 Object-oriented Programming in C++
C++ Object Oriented 1.
Computer Science II for Majors
Presentation transcript:

Inheritance: Part I

The classes A class is the description of a family of objects having the same structure and behaviors. Instantiation: instances or objects of the physical representations of a class

Structuring ‘classes’ class wheel { } class car { wheel w; // a car has a wheel class vehicle { car c; // ??? class X { … } class Y { X x; a car has a wheel a car (does not have an vehicle), but is an vehicle! Y has a X as a component, but a y of Y is NOT a x of X

Relationship Between Classes Composition (class in class) “has-a” relationship Object contains one or more objects of other classes as members Example: A car has a wheel Inheritance (derived class) “is-a” relationship Derived class object can be treated as base class object Example: A car is a vehicle

Example 1

Example 2

Derived Class Employee Manager a ‘Manager’ is a ‘Employee’! string firstName; string familyName; … } class Manager : public Employee { list<Employee*> group; Manager Employee a ‘Manager’ is a ‘Employee’! a derived class object can be treated as a base class object.

Class hierarchy Employee Manager Director class Employee { string firstName; string familyName; … } class Manager : public Employee { list<Employee*> group; class Director : public Manager { Employee Manager Director

(Public) Inheritance Base class (super-class), derived class (sub-class) Derived class object is a specialization and is a ‘superset’ of the data of its base class object Derived class inherits all members of base class (and members of all its ancestor classes) Public members of the base class Inherited, so accessible Private members of the base class Not accessible directly: comes as a surprise, but ‘privacy’ would have been rendered meaningless if allowed by deriving a new class Accessed through public member functions Friend functions are not inherited

Respecting privacy … Yes. No, because of privacy! class Employee { public: string fullName(); private: string firstName; string familyName; … } class Manager : public Employee { void print() const; list<Employee*> group; void Manager::print() const { cout << fullName() << endl; Void Manager::print() const { cout << firstName << familyName << endl; Yes. No, because of privacy!

Class-in-Class vs. Inheritance A class declares another class as its data member, hence creating an object within another object Inheritance and class-in-class are two quite different things and concepts in implementation and OOP. Inheritance has a "is-a" relationship between derived class and base class, while class-in-class is a "has-a" relationship Generally, we can decide whether to use inheritance or class-in-class by common sense. If we can find some common relationship between two or more things, we should use inheritance. For example, Citizen and Student with Citizen as the base class. It makes no sense to implement a Citizen class inside a Student class. In class-in-class, the inner class is a standalone object. Thus, the inner class and the outer class do not share the powerful features in inheritance (such as polymorphism and dynamic binding).

Constructors of Derived Classes class Y : public X { public Y::Y(); }; Y::Y() : X() { …

If no constructor, use the default base constructor. class Employee { … Employee(); string firstName; string familyName; } class Manager : public Employee { private: list<Employee*> group; int level; Employee e; Manager m; // use default ‘employee’ constructor If no constructor, use the default base constructor.

class Employee { public: Employee(); private: string firstName; string familyName; … } class Manager : public Employee { Manager(); list<Employee*> group; int level; Manager::Manager() : Employee() { level = … group = … Employee e; Manager m; // use ‘manager’s constructor

Passing ‘Arguments’ between ‘Base’ and ‘Derived’ Class Constructors class Employee { public: Employee(const string& first, const string& family); private: string firstName; string familyName; … } class Manager : public Employee { list<Employee*> group; int level; Employee::Employee(const string& first, const string& family) { Manager::Manager(const string& first, const string& family, int l) : Employee(first,family) , level(l) { If a base class has a constructor with arguments, then it is compulsary for the derived class to have a constructor and pass the arguments to the base class constructor.

Order of Construction Class objects are constructed from the bottom up: First the base, Then the members, And then the derivated class itself. Members and bases are constructed in order of declaration. When a program creates a derived-class object: The derived-class constructor immediately calls the base-class constructor The base-class constructor’s body (i.e., within {}) executes Then the derived class’s member initializer list execute Finally the derived-class constructor’s body executes This process cascades up the hierarchy if the hierarchy contains more than two levels in a recursive manner

Destructor class Y : public X { public Y::~Y(); }; Y::~Y() { …

Order of Destruction Class objects are destroyed in the opposite order of the construction: First the derived class itself, Then the members, And then the base. Members and bases are destroyed in the reverse order of declaration.

Over-riding (not over-loading): Reusing Operations of Base Classes Derived class may extend or replace base class function of the same name (homonym, homonymous) Therefore, it hides (or overrides) the base-class version of the function Still possible to call the base class function with scope resolution operator class X { public: f(); }; Class Y : public X {}; void Y::f() { … X::f(); }

class X { public: void print(){cout << "base\n";} }; class Y : public X { void print( int i ){ cout << i << " Derived\n";} void print( char ch ){ cout << ch << " Derived\n";} int main(){ Y y; y.print( 2 ); // print 2 Derived y.print(‘y'); // print y Derived // y.print(); Not O.K.: no matching function for Y::print() }

Remarks Beware of ‘infinite recursion’ in function over-riding, if the scope :: operator is not prefixed with the name of the base class when referencing the base class’s member function causes infinite recursion which version of the function to call or function resolution is a major issue!  static or dynamic binding!!!

‘protected’ members The private data members in the base class cannot be accessed by its derived classes The protected data members in the base class can be accessed by its derived classes, but not other non-derived classes The ‘protected’ is the ‘shared privacy’ between base and derived classes !

class Employee { protected: string firstName; string familyName; … } class Manager : public Employee { public: void print() const; private: list<Employee*> group; void Manager::print() const { cout << firstName << familyName << endl;

Protection control: public, protected, private Intermediate level of protection between public and private For both data members and function members Want the derived class to directly access members while forbid other classes to access them directly protected members in the Base class are accessible to Base class members Base class friends Derived class members Derived class friends

Pros/Cons of ‘Protected’ Advantages Derived class can modify values directly Avoid set/get method call overhead  Slight increase in performance Disadvantages No validity checking: Derived class can assign illegal value to protected members Implementation-dependent on the base class Derived class functions are usually very likely dependent on base class implementation Using protected access, base class implementation changes may result in derived class modifications, e.g., a change to a different printing format in the base class may cause the printing function to be changed in the derived class This leads to fragile (brittle) software

Example CommissionEmployee BasePlusCommissionEmployee First name, last name, SSN (Social Security Number, i.e., ID), commission rate, gross sale amount BasePlusCommissionEmployee CommissionEmployee: First name, last name, SSN, commission rate, gross sale amount And also base salary Class BasePlusCommissionEmployee Much of the code is similar to CommissionEmployee Additions private data member baseSalary Methods setBaseSalary and getBaseSalary

Derived from class CommissionEmployee Is a CommissionEmployee Inherits all public members Use base-class initializer syntax to initialize base-class data member Has data member baseSalary Base class implementation CommissionEmployee1.h, CommissionEmployee1.cpp Derived class implementation BasePlueCommissionEmployee1.h, BasePlusCommissionEmployee1.cpp Compilation error because derived class cannot directly access private members of CommissionEmployee class in print() and earnings()

tester2.cpp Sample Output Employee information obtained by get functions:   First name is Bob Last name is Lewis Social security number is 333-33-3333 Gross sales is 5000.00 Commission rate is 0.04 Base salary is 300.00 Updated employee information output by print function: base-salaried commission employee: Bob Lewis social security number: 333-33-3333 gross sales: 5000.00 commission rate: 0.04 base salary: 1000.00 Employee's earnings: $1200.00

order.cpp Sample Output (1/2) CommissionEmployee constructor: commission employee: Bob Lewis social security number: 333-33-3333 gross sales: 5000.00 commission rate: 0.04   CommissionEmployee destructor: base-salaried commission employee: Lisa Jones social security number: 555-55-5555 gross sales: 2000.00 commission rate: 0.06 CommissionEmployee constructor called for object in block; destructor called immediately as execution leaves scope Base-class CommissionEmployee constructor executes first when instantiating derived-class BasePlusCommissionEmployee object BasePlusCommissionEmployee constructor: base-salaried commission employee: Lisa Jones social security number: 555-55-5555 gross sales: 2000.00 commission rate: 0.06 base salary: 800.00   CommissionEmployee constructor: commission employee: Mark Sands social security number: 888-88-8888 gross sales: 8000.00 commission rate: 0.15 Derived-class BasePlusCommissionEmployee constructor body executes after base-class CommissionEmployee’s constructor finishes execution Base-class CommissionEmployee constructor executes first when instantiating derived-class BasePlusCommissionEmployee object

order.cpp Sample Output (2/2) BasePlusCommissionEmployee constructor: base-salaried commission employee: Mark Sands social security number: 888-88-8888 gross sales: 8000.00 commission rate: 0.15 base salary: 2000.00 BasePlusCommissionEmployee destructor:   CommissionEmployee destructor: commission employee: Mark Sands Derived-class BasePlusCommissionEmployee constructor body executes after base-class CommissionEmployee’s constructor finishes execution Destructors for BasePlusCommissionEmployee object called in reverse order of constructors BasePlusCommissionEmployee destructor: base-salaried commission employee: Lisa Jones social security number: 555-55-5555 gross sales: 2000.00 commission rate: 0.06 base salary: 800.00   CommissionEmployee destructor: commission employee: Lisa Jones Destructors for BasePlusCommissionEmployee object called in reverse order of constructors

Summary ‘is-a’ relationship Public inheritance ‘protected’ Different from ‘has-a’ or ‘uses-a’, or … by class-in-class Public inheritance Public Private ‘protected’ Constructors/destructors Redefinition (over-riding)  which version for which object ? Not inherited: Friend functions

Inheritance: Part II

A More General Derived Class class Y : public X {…} class Y : protected X {…}; class Y : private X {…};

Increasing Access Restrictions public inheritance (written as class derived: public base) Base class public members  derived class public members Base class protected members  derived class protected members All classes can directly access the public members Only the derived classes can directly access the protected members protected inheritance (written as class derived: protected base) Base class public and protected members  derived class protected members Classes in the inheritance hierarchy can still access the members (because they are protected members), but not for other classes private inheritance (written as class derived: private base) Base class public and protected members  derived class private members Classes in the downstream inheritance hierarchy can no longer access the members (and neither can all the other classes) public protected private Base class Derived class

Types of Inheritance and Member Access

public_derived pub_d; protected_derived prot_d; class Base { public: void f() {cout << "Base::f()" << endl;} protected: void g() {cout << "Base::g()" << endl;} }; class public_derived: public Base { class protected_derived: protected Base { class private_derived: private Base { int main(){ Base b; public_derived pub_d; protected_derived prot_d; private_derived priv_d; b.f(); // b.g(); error: 'void Base::g()' is protected pub_d.f(); // pub_d.g(); error: 'void Base::g()' is protected // prot_d.f(); error: 'void Base::f()' is inaccessible // prot_d.g(); error: 'Base' is not an accessible base of 'protected_derived' // priv_d.f(); similar error as above // priv_d.g(); similar error as above return 1; }

Class hierarchy Direct base class Indirect base class Inherited explicitly (one level up hierarchy) E.g., driver licenses and license Indirect base class Inherited two or more levels up hierarchy E.g., car license and license Single inheritance Inherits from one base class E.g., the above license example Multiple inheritance Inherits from multiple base classes Base classes possibly unrelated E.g., A “university student” is both a “hard-working person” and a “clever person”

A B C D E F The inheritance graph for simple inheritance is a tree. The transitive closure of the inheritance relationship of the tree is a relation of total order.

p A B C D p A B C D A B C D p p p No conflict for A of p Conflict between B and C for A of p Not a tree, only a partial ordering, some objects are not comparable!

B C p p A Conflict between B and C for A of p, So to redefine p in A

Multiple Inheritance When a derived class inherits members from two or more base classes Provide comma-separated list of base classes after the colon following the derived class name Can cause ambiguity problems Should be used only by experienced programmers Newer languages do not allow multiple inheritance A common issue occurs if more than one base class contains a member with the same name Solved by using the scope resolution operator (by the user!)

Multiple Inheritence (Cont.) Should be used when an “is a” relationship exists between a new type and two or more existing types i.e. type A “is a” type B and type A “is a” type C Can introduce complexity into a system Great care is required in the design of a system to use multiple inheritance properly Should not be used when single inheritance and/or composition will do the job

Example: Base1.h, Base2.h, Derived.h, Derived.cpp, multiple.cpp

Class Base1 declares member function getData Base1.h class Base1 { public: Base1( int parameterValue ) value = parameterValue; } int getData() const return value; protected: int value; }; Class Base1 declares member function getData

Class Base2 also declares member function getData, same name as Base1 Base2.h class Base2 { public: Base2( char characterData ) letter = characterData; } char getData() const return letter; protected: char letter; }; Class Base2 also declares member function getData, same name as Base1

Derived.h #include "Base1.h" #include "Base2.h" class Derived : public Base1, public Base2 { friend ostream& operator<<( ostream&, const Derived&); public: Derived(int, char, double); double getReal() const; private: double real; }; Class Derived inherits from both class Base1 and class Base2 through multiple inheritance

Base1 base1(10); Base1* base1Ptr = 0; Base2 base2('Z'); Base2* base2Ptr = 0; Derived derived( 7, 'A', 3.5 ); // print data members of base-class objects cout << "Object base1 contains integer " << base1.getData() << "\nObject base2 contains character " << base2.getData() << "\nObject derived contains:\n" << derived << "\n\n"; // print data members of derived-class object // scope resolution operator resolves getData ambiguity cout << "Data members of Derived can be accessed individually:" << "\n Integer: " << derived.Base1::getData() << "\n Character: " << derived.Base2::getData() << "\nReal number: " << derived.getReal() << "\n\n"; cout << "Derived can be treated as an object of either base class:\n"; // treat Derived as a Base1 object base1Ptr = &derived; cout << "base1Ptr->getData() yields " << base1Ptr->getData() << '\n'; // treat Derived as a Base2 object base2Ptr = &derived; cout << "base2Ptr->getData() yields " << base2Ptr->getData() << endl;

multiple.cpp Sample Output Note the use of base-class pointer pointing to a derived-class objects Invoking the member function of the derived object Object base1 contains integer 10 Object base2 contains character Z Object derived contains: Integer: 7 Character: A Real number: 3.5   Data members of Derived can be accessed individually: Derived can be treated as an object of either base class: base1Ptr->getData() yields 7 base2Ptr->getData() yields A

Software Engineering: Software Customization with Inheritance Inheriting from existing classes Can include additional members Can redefine base-class members No direct access to base class’s source code Only links to object code Good for those independent software vendors (ISVs) Develop proprietary code for sale/license Available in object-code format Users derive new classes Without accessing ISV proprietary source code