Rossella Lau Lecture 7, DCO10105, Semester B,2005-6 DCO10105 Object-Oriented Programming and Design  Lecture 7: OOP: Inheritance  Inheritance and composition.

Slides:



Advertisements
Similar presentations
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Rossella Lau Lecture 11, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 11: Template and Operator overload  Template.
Rossella Lau Lecture 6, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 6: More on class construction UML and an.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 13: Inheritance and Composition.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 12: Inheritance and Composition.
Rossella Lau Lecture 5, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 5: Class construction  Encapsulation 
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 12: Inheritance and Composition.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
LECTURE 07 Programming using C# Inheritance
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 13: Inheritance and Composition.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Chapter 12: Adding Functionality to Your Classes.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
Chapter 8 More Object Concepts
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Inheritance in the Java programming language J. W. Rider.
Chapter 13: Inheritance and Composition
C# F 1 CSC 298 Object Oriented Programming (Part 1)
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Chapter 10 Inheritance and Polymorphism
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 13: Inheritance and Composition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Classes (Part 1) Lecture 3
Inheritance Basics Programming with Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
Computer Programming with JAVA
Presentation transcript:

Rossella Lau Lecture 7, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 7: OOP: Inheritance  Inheritance and composition Basic OOD concept “is-a” relationship & “has-a” relationship  Implementation of Inheritance Syntax in C++ Construction from the UML Scope referencing The sample application: Grade -- By Rossella Lau

Rossella Lau Lecture 7, DCO10105, Semester B,  Three basic principles  Encapsulation Pack data and operations into a class to hide its details from the outside world (class construction)  Inheritance Classes can be derived from existing classes to save programmers’ effort – code reuse  Polymorphism Same class can play as different types (of ancestors) Same expression can perform different operations  OOP implements OOD Basic OOD concept

Rossella Lau Lecture 7, DCO10105, Semester B, Construction from an UML class notation  Malik’s example: Ch 12:personType.h & personTypeImp.cpp

Rossella Lau Lecture 7, DCO10105, Semester B, Instantiation of objects  Each object instantiated from a class will have its own data members with some values lastName firstName personType Chan Terence person1 Tse Cyril person2 Wong Patrick person3 Lau Rossella person4

Rossella Lau Lecture 7, DCO10105, Semester B, Instantiation but not static member  E.g., DayType includes a static member weekDays  Instead of a weekDays for each instance, there is only one single value residence somewhere of the program weekDay weekDays DayType Mon day1 Tue day2 Wed day3 weekDays

Rossella Lau Lecture 7, DCO10105, Semester B, Application: Grade  Programs under the folder “GradeReport” of Chapter 13

Rossella Lau Lecture 7, DCO10105, Semester B, Composition  Inside studentType, courseEnrolled is in type of courseType[]  I.e., studentType is constructed with data members of the type of another class courseType  This is called composition

Rossella Lau Lecture 7, DCO10105, Semester B, “has-a” relationship  Composition can be interpreted as a “has-a” relationship  A student has a number of courses enrolled; an orderItem has a “Product”; a retail system has a “Catalog” and some “Cashiers ”  Composition allows a class to be constructed with data members of the type of another class

Rossella Lau Lecture 7, DCO10105, Semester B, Construction with aggregation  When there is an aggregation in an application, the instance of the class ended at the diamond should be declared as a container (an array or a vector)  courseEnrolled in studentType  studentList in the main program

Rossella Lau Lecture 7, DCO10105, Semester B, Construction with generalization  When there is a specialization (vs generalization), the class definitions should be written as a sub class of the generalization class – the concept of inheritance

Rossella Lau Lecture 7, DCO10105, Semester B, Inheritance  Inheritance allows new classes to be created, derived from existing classes  The existing class is called “base class” or “parent class”  The new derived class is called “sub class” or “child class”  Derived classes inherit, carry or own, all the properties of the base class

Rossella Lau Lecture 7, DCO10105, Semester B, Syntax of Inheritance class SubClass : [AccessSpecifier] BaseClass { };  : (colon) indicates the relationship of inheritance  The left hand side is the new class, the sub class, and is going to be derived, i.e., to own all the properties (data members, function members, and the type), from the base class

Rossella Lau Lecture 7, DCO10105, Semester B, Access Specifier AccessSpecifier can be one of these three: public, private, and protected  It specifies the ability of an outsider (things other than the members of SubClass ) on how to access the members in BaseClass  public allows an outsider to access every public member in BaseClass through an instance of SubClass  private does not allow an outsider to access any members, even if it is a public member, in BaseClass  protected allows a child class of SubClass to access public members in BaseClass but does not allow other outsiders to access any members, even if it is a public member, in BaseClass  In our course, we will focus on public inheritance

Rossella Lau Lecture 7, DCO10105, Semester B, An example:  Malik’s example: Ch 13:studentType.h & studentTypeImp.h

Rossella Lau Lecture 7, DCO10105, Semester B, Instantiation of a student  Each object instantiated from studentType will include not only the values of studentType’s data but also values of personType’s data sID numberOfCourses isTuitionPaid courseEnrolled[] courseGrades studentType Chan Judy True {(OOP,DCO10105,3),…} {A,…} student1

Rossella Lau Lecture 7, DCO10105, Semester B, Inheritance is “is-a” relationship  Inheritance can be interpreted as an “is-a” relationship  “Coffee” is a “Product”; “Coffee Brewer” is a “Product”  “Cash payment” is a “Payment”, “Octopus payment” is a “Payment”

Rossella Lau Lecture 7, DCO10105, Semester B, Protected members of a class  With inheritance, members of a class can also be protected in areas other than public and private  Protected members are public to all sub classes of the class the members belong to but private to classes not belonging to the family of the class including the protected members  In this course, we will focus on private or public members

Rossella Lau Lecture 7, DCO10105, Semester B, Scope referencing  For a user program which has: stuendType student;  student.getFirstName() uses the member defined in personType  student.setInfo() uses the member defined in studentType  How about student.print() ?

Rossella Lau Lecture 7, DCO10105, Semester B, When two functions are identical ……  For student.print(), it uses the print() in studentType since the compiler finds studentType first then its base class(es) personType  Indeed, both print() are identical, i.e., they have the same signatures  print() of studentType redefines, or overrides, the print() in personType

Rossella Lau Lecture 7, DCO10105, Semester B, More about scope resolution operation ::  When a sub class uses an identical function (with same signature) in the base class, :: should be used  In personType, void print(){ outF << getFirstName() << " " << getLastName() << endl; }  In studentType, output of a student’s name can become: outF << "Student Name: " << personType::print();

Rossella Lau Lecture 7, DCO10105, Semester B, Avoid identical codes – code reuse  Therefore, identical codes in sub-classes should be avoided:  E.g., on line in studentTypeImp.cpp : outF << "Student Name: " << getFirstName() << " " << getLastName() << endl;  part of the statement is the same as in personType::print()  It can be changed, for the sake of code reuse or redundant code elimination, as follows: outF << "Student Name: " << personType::print();

Rossella Lau Lecture 7, DCO10105, Semester B, A sub class can play as its base class  As a sub class would have all the properties of its base class – it can act as its base class  It can be in the type of its base class  One more example on scope referencing class: DonaldFamily.h and scopePlayer.cpp  Donald boxD(dewey) : dewey plays as a Donald object  deduct7(huey) : huey plays as a Donald object for calling

Rossella Lau Lecture 7, DCO10105, Semester B, static_cast ( variable )  Temporary change a variable’s type to ClassName e.g., line 33, 48, 50 in DonaldFamily.h  Formal C uses ( ClassName ) variable

Rossella Lau Lecture 7, DCO10105, Semester B, Function overload and override  In DonaldFamily.h :  In class Dewey and Huey, withdraw() is overloaded Indeed, withdraw(int) in Dewey seemed redundant because usually it can be referenced automatically by type matching; however, double is a type compatible to int and cause calling to withdraw(double) even if the actual parameter is an integer from dewey.withdraw(static_cast (5));  In class Donald, getMoney(), withdraw(int), and print() are overridden by both Dewey and Huey

Rossella Lau Lecture 7, DCO10105, Semester B, Initialization of data members in base class  Since data members in the base class are also data members of the sub class, initialization of those members should use a constructor of the base class  Dewey(double) uses Donald(int)  Huey(double) uses Donald(), the default constructor, even if it is not explicitly written there; i.e., the compiler will automatically generate a call to the default constructor to initialize data members in the base class if no such initialization in the constructor of the sub class

Rossella Lau Lecture 7, DCO10105, Semester B, Summary  Members of a class can be public, private, and protected  Inheritance is an “is-a” relationship while composition is a “has-a” relationship  A derived class inherits all its base class’ members + type  Inheritance increase code reuse  C++ allows for three access types of inheritance, we have examples for public inheritance  When identical functions, with same id and signatures, in sub class and base class, instance of sub class access the function in sub class and this is called function override

Rossella Lau Lecture 7, DCO10105, Semester B, Reference  Malik: 12.5, 13  -- END --