C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 13: Inheritance and Composition.

Slides:



Advertisements
Similar presentations
Chapter 12: Inheritance and Composition
Advertisements

Chapter 14 Inheritance Pages ( ) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance.
OOP: Inheritance By: Lamiaa Said.
Inheritance and Composition (aka containment) Textbook Chapter –
C++ Inheritance Systems Programming.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
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.
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.
Software Engineering Principles and C++ Classes
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 12: Inheritance and Composition.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 13: Overloading.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
Chapter 10: Inheritance and Polymorphism
Data Structures Using C++1 Chapter 1 Software Engineering Principles and C++ Classes.
Chapter 15: Operator Overloading
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
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 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
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 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
An Object-Oriented Approach to Programming Logic and Design
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Chapter 13: Inheritance and Composition
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Data Structures Using C++1 Chapter 1 Software Engineering Principles and C++ Classes.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 12: Inheritance and Composition.
Object Oriented Programming
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CS 132 Spring 2008 Chapter 2 Object-Oriented Design (OOD) and C++ Ideas: * Inheritance (and protected members of a class) ** Operator overloading Pointer.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
CITA 342 Section 1 Object Oriented Programming (OOP)
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
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)
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
Data Structures Using Java1 Chapter 1 Software Engineering Principles and Java Classes.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
A First Book of C++ Chapter 12 Extending Your Classes.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Chapter 13: Overloading and Templates
Object-Oriented Design (OOD) and C++
About the Presentations
Chapter 15: Overloading and Templates
Chapter 11: Inheritance and Composition
C++ Object Oriented 1.
Presentation transcript:

C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 13: Inheritance and Composition

C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 Objectives In this chapter, you will: Learn about inheritance Learn about derived and base classes Explore how to redefine the member functions of a base class Examine how the constructors of base and derived classes work Learn how to construct the header file of a derived class

C++ Programming: From Problem Analysis to Program Design, Fourth Edition3 Objectives (continued) Become familiar with the C++ stream hierarchy Explore three types of inheritance: public, protected, and private Learn about composition Become familiar with the three basic principles of object-oriented design

C++ Programming: From Problem Analysis to Program Design, Fourth Edition4 Inheritance and Composition The two common ways to relate two classes in a meaningful way are: −Inheritance (“is-a” relationship) −Composition (“has-a” relationship)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition5 Inheritance Inheritance is an “is-a” relationship −Example: “every employee is a person” Inheritance lets us create new classes from existing classes −New classes are called the derived classes −Existing classes are called the base classes Derived classes inherit the properties of the base classes

Examples Base class Student Shape Employee Derived classes Graduate Student Undergraduate student Circle Rectangle Faculty Member Staff member

Inheritance (continued) Single inheritance: derived class has a single base class −Ex. Circle class from Shape class Multiple inheritance: derived class has more than one base class …will not be discussed in this chapter. −Ex. Son class from Mother class and Father class Public inheritance: all public members of base class are inherited as public members by derived class

C++ Programming: From Problem Analysis to Program Design, Fourth Edition8 Inheritance (continued) Inheritance can be viewed as a tree-like, or hierarchical, structure wherein a base class is shown with its derived classes

C++ Programming: From Problem Analysis to Program Design, Fourth Edition9 Inheritance (continued) General syntax of a derived class: −Where memberAccessSpecifier is public, protected, or private (default) The private members of a base class are private to the base class −Derived class cannot directly access them −Ex. Class SonClass: public FatherClass { };

objA x y z printA setA objB var1 printB setB var2 x y z printA setA A Base derived

objB var1 printB setB printA setA var2 z x y objA x y z printA setA Can not access x, y

objB var1 printB setB var2 z x y objA x y z printA setA printA setA

objB var1 printB setB printA setA var2 z x y

C++ Programming: From Problem Analysis to Program Design, Fourth Edition14 Inheritance (continued) public members of base class can be inherited as public or private members The derived class can include additional members--data and/or functions The derived class can redefine the public member functions of the base class All members of the base class are also member variables of the derived class

Redefining (Overriding) Member Functions of the Base Class To redefine (override) a public member function of a base class −Corresponding function in the derived class must have the same name, number, and types of parameters. Redefined −If the function has a different signature, this would be function overloading.

Redefining (Overriding) Member Functions of the Base Class (continued) If derived class overrides a public member function of the base class, then to call the base class function, specify: −Name of the base class −Scope resolution operator ( :: ) −Function name with the appropriate parameter list

var1 print setB A::print setA var2 z x y

C++ Programming: From Problem Analysis to Program Design, Fourth Edition18 Redefining Member Functions of the Base Class (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition19 Redefining Member Functions of the Base Class (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition20 Redefining Member Functions of the Base Class (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition24 Redefining Member Functions of the Base Class (continued) boxType is derived from rectangleType, and it is a public inheritance −Also overrides print and area

C++ Programming: From Problem Analysis to Program Design, Fourth Edition26 Constructors of Derived and Base Classes Derived class constructor cannot directly access private members of the base class Derived class can directly initialize only public member variables of the base class When a derived object is declared −It must execute one of the base class constructors Call to base class constructor is specified in heading of derived class constructor definition

Example

C++ Programming: From Problem Analysis to Program Design, Third Edition 29 Example

C++ Programming: From Problem Analysis to Program Design, Fourth Edition30 Constructors of Derived and Base Classes (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition31 Constructors of Derived and Base Classes (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition33 Constructors of Derived and Base Classes (continued)

3 objB A 4 x y

C++ Programming: From Problem Analysis to Program Design, Fourth Edition36 Header File of a Derived Class To define new classes −Create new header files To create new classes based on previously defined classes −Header files of the new classes contain commands that specify where to look for the definitions of the base classes The definitions of the member functions can be placed in a separate file

C++ Programming: From Problem Analysis to Program Design, Fourth Edition38 Multiple Inclusions of a Header File Use the preprocessor command ( #include ) to include a header file in a program The preprocessor processes the program before it is compiled To avoid multiple inclusion of a file in a program −Use certain preprocessor commands in the header file (“file guard”)

Problem: Solution: Note that test.h included twice ONE and TWO Declared twice

File1.h File.cpp error Without errors

Examples error

C++ Programming: From Problem Analysis to Program Design, Fourth Edition42 C++ Stream Classes ios is the base class for all stream classes −Contains formatting flags and member functions to access/modify the flag settings

C++ Programming: From Problem Analysis to Program Design, Fourth Edition43 C++ Stream Classes (continued) istream and ostream provide operations for data transfer between memory and devices − istream defines the extraction operator ( >> ) and functions such as get and ignore − ostream defines the insertion operator ( << ), which is used by cout ifstream / ofstream objects are for file I/O −Header file fstream contains the definitions

C++ Programming: From Problem Analysis to Program Design, Fourth Edition44 Protected Members of a Class Private members of a class cannot be directly accessed outside the class For a base class to give derived class access to a private member −Declare that member as protected The accessibility of a protected member of a class is in between public and private −A derived class can directly access the protected member of the base class

C++ Programming: From Problem Analysis to Program Design, Fourth Edition45 Inheritance as public, protected, or private If memberAccessSpecifier is public : − public members of A are public members of B and can be directly accessed in class B − protected members of A are protected members of B and can be directly accessed by member functions (and friend functions) of B − private members of A are hidden in B and can be accessed by member functions of B through public or protected members of A

C++ Programming: From Problem Analysis to Program Design, Fourth Edition46 Inheritance as public, protected, or private (continued) If memberAccessSpecifier is protected : − public members of A are protected members of B and can be accessed by the member functions (and friend functions) of B − protected members of A are protected members of B and can be accessed by the member functions (and friend functions) of B − private members of A are hidden in B and can be accessed by member functions of B through public or protected members of A

C++ Programming: From Problem Analysis to Program Design, Fourth Edition47 Inheritance as public, protected, or private (continued) If memberAccessSpecifier is private : − public members of A are private members of B and can be accessed by member functions of B − protected members of A are private members of B and can be accessed by member functions (and friend functions) of B − private members of A are hidden in B and can be accessed by member functions of B through public / protected members of A

C++ Programming: From Problem Analysis to Program Design, Fourth Edition49 Composition In composition, one or more member(s) of a class are objects of another class type Composition is a “has-a” relation Arguments to the constructor of a member- object are specified in the heading part of the definition of the constructor

C++ Programming: From Problem Analysis to Program Design, Fourth Edition50 Composition (continued) Member-objects of a class are constructed: −In the order they are declared Not in the order they are listed in the constructor’s member initialization list −Before the enclosing class objects are constructed

Example objB objA z x y 5 3 4

Composition (continued) Member-objects of a class are constructed −In the order they are declared Not in the order they are listed in the constructor’s member initialization list −Before the enclosing class objects are constructed

Example objC objB objA

Example

C++ Programming: From Problem Analysis to Program Design, Fourth Edition61 Object-Oriented Design and Object-Oriented Programming The fundamental principles of object-oriented design (OOD) are: −Encapsulation: combine data and operations on data in a single unit −Inheritance: create new objects from existing objects −Polymorphism: the ability to use the same expression to denote different operations

C++ Programming: From Problem Analysis to Program Design, Fourth Edition62 OOD and OOP (continued) OOD −Object is a fundamental entity −Debug objects −Program is a collection of interacting objects −Programmer is object-oriented −OOD encourages code reuse

C++ Programming: From Problem Analysis to Program Design, Fourth Edition63 OOD and OOP (continued) Structured programming −Function is a fundamental entity −Debug functions −Program is a collection of interacting functions −Programmer is action-oriented

C++ Programming: From Problem Analysis to Program Design, Fourth Edition64 OOD and OOP (continued) Object-oriented programming (OOP) implements OOD C++ supports OOP through the use of classes Polymorphic function or operator has many forms Function name and operators can be overloaded

C++ Programming: From Problem Analysis to Program Design, Fourth Edition65 OOD and OOP (continued) Templates provide parametric polymorphism C++ provides virtual functions as a means to implement polymorphism in an inheritance hierarchy Objects are created when class variables are declared Objects interact via function calls

C++ Programming: From Problem Analysis to Program Design, Fourth Edition66 OOD and OOP (continued) Every object has an internal state and external state Private members form the internal state Public members form the external state Only the object can manipulate its internal state

C++ Programming: From Problem Analysis to Program Design, Fourth Edition67 Identifying Classes, Objects, and Operations Finding classes: begin with a problem description and identify all nouns and verbs −From the list of nouns choose the classes −From the list of verbs choose the operations Suppose we want to write a program that calculates and prints the volume and surface area of a cylinder

C++ Programming: From Problem Analysis to Program Design, Fourth Edition68 Identifying Classes, Objects, and Operations (continued) We can state this problem as follows: −Write a program to input the dimensions of a cylinder and calculate and print the surface area and volume −The nouns are bold and the verbs are italic −From the list of nouns we visualize a cylinder as a class ( cylinderType ) from which we can create many cylinder objects of various dimensions

C++ Programming: From Problem Analysis to Program Design, Fourth Edition69 Identifying Classes, Objects, and Operations (continued) The nouns are characteristics of a cylinder: −Dimensions −Surface area −Volume After identifying a class, determine three pieces of information about its objects: −Operations that an object can perform −Operations that can be performed on an object −Information that an object must maintain

C++ Programming: From Problem Analysis to Program Design, Fourth Edition70 Identifying Classes, Objects, and Operations (continued) From the verbs, choose a list of possible operations that an object of that class can perform, or has performed, on itself −For the cylinderType class: Input Calculate Print −Dimensions represent the data

C++ Programming: From Problem Analysis to Program Design, Fourth Edition71 Identifying Classes, Objects, and Operations (continued) The center of the base, radius of the base, and height of the cylinder are the characteristics of the dimensions Calculate: determine volume and surface area − cylinderVolume − cylinderSurfaceArea Print: display the volume and the surface area on an output device

C++ Programming: From Problem Analysis to Program Design, Fourth Edition72 Identifying Classes, Objects, and Operations (continued) Identifying classes via the nouns and verbs from the descriptions to the problem is not the only technique possible There are several other OOD techniques in the literature

C++ Programming: From Problem Analysis to Program Design, Fourth Edition73 Summary Inheritance and composition are meaningful ways to relate two or more classes Inheritance is an “is-a” relation −Single inheritance: a derived class is derived from one class, called the base class −Multiple inheritance: a derived class is derived from more than one base class Composition is a “has-a” relation

C++ Programming: From Problem Analysis to Program Design, Fourth Edition74 Summary (continued) Private members of a base class are private to the base class Public members of a base class can be inherited either as public or private Derived class can redefine function members of a base class −Redefinition applies only to objects of derived class

C++ Programming: From Problem Analysis to Program Design, Fourth Edition75 Summary (continued) A call to a base class constructor (with parameters) is specified in the heading of the definition of the derived class constructor When initializing object of a derived class, the base class constructor is executed first In composition −Class member is an object of another class −Call to constructor of member objects is specified in heading of the definition of class’s constructor

C++ Programming: From Problem Analysis to Program Design, Fourth Edition76 Summary (continued) Three basic principles of OOD are: −Encapsulation −Inheritance −Polymorphism Finding classes: −Describe the problem −Choose classes from the list of nouns −Choose operations from the list of verbs