0% found this document useful (0 votes)
28 views21 pages

Chapter 2

Uploaded by

Israa M Alqatow
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views21 pages

Chapter 2

Uploaded by

Israa M Alqatow
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Chapter 2

Object-Oriented Design (OOD) and C++

 Learn about inheritance


 Data Structures Using
Learn about derived and base classes
C++
2Ethe member functions
 Explore how to redefine
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


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

Data Structures Using C++ 2E


Department of Computer Science 2
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

Data Structures Using C++ 2E


Department of Computer Science 3
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 4


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
Data Structures Using C++ 2E
Department of Computer Science 5
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

public
FIGURE 2-1
private
Inheritance hierarchy
protected

Data Structures Using C++ 2E 6


Department of Computer Science
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

Data Structures Using C++ 2E


Department of Computer Science 7
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

Data Structures Using C++ 2E


Department of Computer Science 8
Example of Operator
Overloading

Rational Numbers

In directory:
rationalType_text_files

Department of Computer Science


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
Data Structures Using C++ 2E
Department of Computer Science 10
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

Data Structures Using C++ 2E


Department of Computer Science 11
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

Data Structures Using C++ 2E


Department of Computer Science 12
Constructors of Derived and
Base Classes (cont’d.)
 Consider the following statements

Data Structures Using C++ 2E 13


Department of Computer Science
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

Data Structures Using C++ 2E


Department of Computer Science 14
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

Data Structures Using C++ 2E


Department of Computer Science 15
Multiple Inclusions of a Header
File (cont’d.)
 Preprocessor commands and meaning

Data Structures Using C++ 2E 16


Department of Computer Science
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
Data Structures Using C++ 2E
Department of Computer Science 17
Inheritance as public,
protected, or private
 Consider the following statement
 MemberAccessSpecifier: public,
protected, or private

Data Structures Using C++ 2E 18


Department of Computer Science
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

Data Structures Using C++ 2E


Department of Computer Science 19
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
Data Structures Using C++ 2E
Department of Computer Science 20
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
Data Structures Using C++ 2E
Department of Computer Science 21

You might also like