Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.

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.
Inheritance Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors.
Inheritance and Composition (aka containment) Textbook Chapter –
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
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.
C++ data types. Structs vs. Classes C++ Classes.
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.
Chapter 13: Overloading.
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++
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.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
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.
1 Inheritance Chapter 9. 2 What You Will Learn Software reusability (Recycling) Inheriting data members and functions from previously defined classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Data Structures Using C++1 Chapter 1 Software Engineering Principles and C++ Classes.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Data Structures Using C++ 2E Chapter 1 Software Engineering Principles and C++ Classes.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 13: Inheritance and Composition.
CITA 342 Section 1 Object Oriented Programming (OOP)
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.
Classes, Interfaces and Packages
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
TK1924 Program Design & Problem Solving Session 2011/2012
Chapter 13: Overloading and Templates
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Object-Oriented Design (OOD) and C++
Chapter 15: Overloading and Templates
Java Programming Language
Chapter 11: Inheritance and Composition
C++ data types.
Presentation transcript:

Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  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

Department of Computer Science 2 Data Structures Using C++ 2E Objectives (cont’d.)  Explore three types of inheritance: public, protected, and private  Learn about composition  Become familiar with the three basic principles of object-oriented design  Learn about overloading  Become aware of the restrictions on operator overloading

Department of Computer Science 3 Data Structures Using C++ 2E Objectives (cont’d.)  Examine the pointer this  Learn about friend functions  Explore the members and nonmembers of a class  Discover how to overload various operators  Learn about templates  Explore how to construct function templates and class templates

Department of Computer Science 4 Example We will examine 3 docx files In the directory personType_text_files This is the base class Then we will look at other Docx files in the directory partTimeEmployeeType_text_files For the inherited class

Department of Computer Science 5 Data Structures Using C++ 2E Inheritance  An “is-a” relationship  Example: “every employee is a person”  Allows new class creation from existing classes  Base class: the existing class  Derived class: new class created from existing classes  Inherits base classes’ properties  Reduces software complexity  Becomes base class for future derived class  Inheritance types  Single inheritance and multiple inheritance

Department of Computer Science Data Structures Using C++ 2E6 Inheritance (cont’d.)  Viewed as treelike or hierarchical  Base class shown with its derived classes  Derived class general syntax  No memberAccessSpecifier specified  Assume private inheritance FIGURE 2-1 Inheritance hierarchy public private protected

Department of Computer Science 7 Data Structures Using C++ 2E Inheritance (cont’d.)  Facts to keep in mind  private base class members  private to the base class  public base class member inheritance  public members or private members  Derived class  Can include additional members  Can redefine public member base class functions  All base class member variables  Derived class member variables

Department of Computer Science 8 Data Structures Using C++ 2E Redefining (Overriding) Member Functions of the Base Class  Base class public member function included in a derived class  Same name, number, and types of parameters as base class member function  Function overloading  Same name for base class functions and derived class functions  Different sets of parameters

Department of Computer Science Example of Operator Overloading Rational Numbers In directory: rationalType_text_files

Department of Computer Science 10 Data Structures Using C++ 2E Constructors of Derived and Base Classes  Derived class with own private member variables  Explicitly includes its own constructors  Constructors  Initialize member variables  Declared derived class object inherits base class members  Cannot directly access private base class data  Same is true for derived class member functions

Department of Computer Science 11 Data Structures Using C++ 2E Constructors of Derived and Base Classes (cont’d.)  Derived class constructors can only directly initialize inherited members (public data)  Derived class object must automatically execute base class constructor  Triggers base class constructor execution  Call to base class constructor specified in heading of derived class constructor definition

Department of Computer Science 12 Data Structures Using C++ 2E Constructors of Derived and Base Classes (cont’d.)  Example: class rectangleType contains default constructor  Does not specify any constructor of the class boxType  Write the definitions of constructors with parameters

Department of Computer Science Data Structures Using C++ 2E13  Consider the following statements Constructors of Derived and Base Classes (cont’d.)

Department of Computer Science 14 Data Structures Using C++ 2E Header File of a Derived Class  Required to define new classes  Base class already defined  Header files contain base class definitions  New class header files contain commands  Tell computer where to look for base classes’ definitions

Department of Computer Science 15 Data Structures Using C++ 2E Multiple Inclusions of a Header File  Preprocessor command include  Used to include header file in a program  Preprocessor processes the program  Before program compiled  Avoid multiple inclusions of a file in a program  Use preprocessor commands in the header file

Department of Computer Science Data Structures Using C++ 2E16 Multiple Inclusions of a Header File (cont’d.)  Preprocessor commands and meaning

Department of Computer Science 17 Data Structures Using C++ 2E Protected Members of a Class  private class members  private to the class  Cannot be directly accessed outside the class  Derived class cannot access private members  Solution: make private member public  Problem: anyone can access that member  Solution: declare member as protected  Derived class member allowed access  Prevents direct access outside the class

Department of Computer Science Data Structures Using C++ 2E18 Inheritance as public, protected, or private  Consider the following statement  MemberAccessSpecifier: public, protected, or private

Department of Computer Science 19 Data Structures Using C++ 2E Inheritance as public, protected, or private (cont’d.)  public MemberAccessSpecifier  public members of A, public members of B: directly accessed in class B  protected members of A, protected members of B: can be directly accessed by B member functions and friend functions  private members of A, hidden to B: can be accessed by B member functions and friend functions through public or protected members of A

Department of Computer Science 20 Data Structures Using C++ 2E Inheritance as public, protected, or private (cont’d.)  protected MemberAccessSpecifier  public members of A, protected members of B: can be accessed by B member functions and friend functions  protected members of A, protected members of B: can be accessed by B member functions and friend functions  private members of A hidden to B: can be accessed by B member functions and friend functions through the public or protected members of A

Department of Computer Science 21 Data Structures Using C++ 2E Inheritance as public, protected, or private (cont’d.)  private MemberAccessSpecifier  public members of A, private members of B: can be accessed by B member functions and friend functions  protected members of A, private members of B: can be accessed by B member functions and friend functions  private members of A, hidden to B: can be accessed by B member functions and friend functions through the public or protected members of A