SlideShare a Scribd company logo
1 of 38
Chapter 11:
Inheritance and Composition
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 the destructors of base and derived
classes work
2C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Objectives (cont’d.)
– Learn how to construct the header file of a
derived class
– Become aware of stream classes hierarchy
– Explore three types of inheritance: public,
protected, and private
– Learn about composition (aggregation)
– Become familiar with the three basic principles of
object-oriented design
3C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Introduction
• Two common ways to relate two classes in a
meaningful way are:
– Inheritance (“is-a” relationship)
– Composition, or aggregation: (“has-a” relationship)
4C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Inheritance
5C++ Programming: From Problem Analysis to Program Design, Seventh Edition
• Inheritance: “is-a” relationship
– Example: “every employee is a person”
• Inheritance allows creation of new classes
from existing classes
– Derived classes: new classes created from the
existing class
– Base class: the original class
• Derived class inherits the properties of its
base classes
Inheritance (cont’d.)
• Inheritance helps reduce software complexity
• Single inheritance: derived class has a single
base class
• Multiple inheritance: derived class has more
than one base class
• Public inheritance: all public members of base
class are inherited as public members by
derived class
6C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Inheritance (cont’d.)
• Inheritance can be viewed as a tree-like, or
hierarchical, structure between the base class
and its derived classes
7C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Inheritance (cont’d.)
• Syntax of a derived class:
– memberAccessSpecifier is public, protected,
or private (default)
• private members of a base class are
private to the base class
– Derived class cannot directly access them
8C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Inheritance (cont’d.)
• public members of base class can be
inherited as public or private members
• Derived class can include additional members
(data and/or functions)
• Derived class can redefine public member
functions of the base class
– Applies only to the objects of the derived class
• All member variables of the base class are also
member variables of the derived class
9C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Redefining (Overriding) Member
Functions of the Base Class
• To redefine a public member function:
– Corresponding function in derived class must have
same name/number/types of parameters
• 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 appropriate parameter list
10C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Redefining Member Functions of the
Base Class (cont’d.)
11C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Redefining Member Functions of the
Base Class (cont’d.)
• boxType is derived from rectangleType,
and it is a public inheritance
– Also overrides print and area
12C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Constructors of Derived and Base
Classes
• Derived class constructor cannot directly
access private members of the base class
– It 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
13C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Destructors in a Derived Class
• Destructors: used to deallocate dynamic
memory allocated by the objects of a class
• When a derived class object goes out of scope
– Automatically invokes its destructor
• When the destructor of the derived class
executes
– Automatically invokes the destructor of the base
class
14C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Header File of a Derived Class
• To define new classes, create new header files
• To create new derived classes, include
commands that specify where the base class
definitions can be found
• Definitions of the member functions can be
placed in a separate file
15C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Multiple Inclusions of a Header File
• Use the preprocessor command (#include)
to include a header file in a program
– 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
16C++ Programming: From Problem Analysis to Program Design, Seventh Edition
C++ Stream Classes
• ios is the base class for all stream classes
– Contains formatting flags and member functions
to access/modify the flag settings
17C++ Programming: From Problem Analysis to Program Design, Seventh Edition
C++ Stream Classes (cont’d.)
• istream and ostream provide operations for
data transfer between memory and devices
– istream defines the extraction operator (>>) and
functions 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 for
these
18C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Protected Members of a Class
• Derived class cannot directly access private
members of it base class
• To give it direct access, declare that member as
protected
19C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Inheritance as public, protected,
or private
• Assume class B is derived from class A with
• If memberAccessSpecifier is public:
–public members of A are public in B, and can be
directly accessed in class B
–protected members of A are protected in 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 only through public or protected
members of A
20C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Inheritance as public, protected,
or private (cont’d.)
• 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 only through public or
protected members of A
21C++ Programming: From Problem Analysis to Program Design, Seventh Edition
• 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 only through public/protected
members of A
22
Inheritance as public, protected, or
private (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Composition (Aggregation)
• In composition, one or more member(s) of a
class are objects of another class type
• Composition (aggregation): “has-a” relation
• Arguments to the constructor of a member-
object are specified in the heading part of the
definition of the constructor
23C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Composition (Aggregation) (cont’d.)
• Member-objects of a class are constructed in
the order they are declared
– Not in the order listed in the constructor’s
member initialization list
• They are constructed before the containing
class objects are constructed
24C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Object-Oriented Design (OOD) and Object-
Oriented Programming (OOP)
• The fundamental principles of object-oriented
design (OOD) are:
– Encapsulation: combines data and operations on
data in a single unit
– Inheritance: creates new objects (classes) from
existing objects (classes)
– Polymorphism: the ability to use the same
expression to denote different operations
25C++ Programming: From Problem Analysis to Program Design, Seventh Edition
OOD and OOP (cont’d.)
• In OOD:
– Object is a fundamental entity
– Debug at the class level
– A program is a collection of interacting objects
• OOD encourages code reuse
• Object-oriented programming (OOP)
implements OOD
26C++ Programming: From Problem Analysis to Program Design, Seventh Edition
OOD and OOP (cont’d.)
• C++ supports OOP through the use of classes
• Function name and operators can be
overloaded
• Polymorphic function or operator: has many
forms
– Example: division with floating point and division
with integer operands
27C++ Programming: From Problem Analysis to Program Design, Seventh Edition
OOD and OOP (cont’d.)
• Templates provide parametric polymorphism
• C++ provides virtual functions to implement
polymorphism in an inheritance hierarchy
– Allows run-time selection of appropriate member
functions
• Objects are created when class variables are
declared
• Objects interact with each other via function
calls
28C++ Programming: From Problem Analysis to Program Design, Seventh Edition
OOD and OOP (cont’d.)
• 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
29C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Identifying Classes, Objects, and
Operations
• To find 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
30C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Identifying Classes, Objects, and
Operations (cont’d.)
• State this problem as follows:
– Write a program to input the dimensions of a
cylinder and calculate and print the surface area
and volume
– Nouns are bold and verbs are italic
– From the list of nouns, can visualize a cylinder as a
class (cylinderType) from which we can
create many cylinder objects of various
dimensions
31C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Identifying Classes, Objects, and
Operations (cont’d.)
• These nouns are characteristics of a cylinder,
so they will not be classes:
– Dimensions
– Surface area
– Volume
• Next, determine three pieces of information
about this class:
– Operations that an object can perform
– Operations that can be performed on an object
– Information that an object must maintain
32C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Identifying Classes, Objects, and
Operations (cont’d.)
• From the verbs, list possible operations that
an object of that class can perform, or have
performed, on itself
– For the cylinderType class:
• Input
• Calculate
• Print
– Dimensions of the cylinder represent the class’s
data
33C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Identifying Classes, Objects, and
Operations (cont’d.)
• Identifying classes via nouns and verbs from
problem descriptions is not the only technique
possible
• There are several other OOD techniques in the
literature
34C++ Programming: From Problem Analysis to Program Design, Seventh Edition
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
35C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Summary (cont’d.)
• 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
36C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Summary (cont’d.)
• 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 (aggregation):
– 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
37C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Summary (cont’d.)
• Three basic principles of OOD:
– Encapsulation
– Inheritance
– Polymorphism
• To find classes:
– Describe the problem
– Choose classes from the list of nouns
– Choose operations from the list of verbs
38C++ Programming: From Problem Analysis to Program Design, Seventh Edition

More Related Content

What's hot

What's hot (20)

9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12
 
9781285852744 ppt ch01
9781285852744 ppt ch019781285852744 ppt ch01
9781285852744 ppt ch01
 
9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08
 
9781285852744 ppt ch16
9781285852744 ppt ch169781285852744 ppt ch16
9781285852744 ppt ch16
 
9781285852744 ppt ch14
9781285852744 ppt ch149781285852744 ppt ch14
9781285852744 ppt ch14
 
Lesson 13 object and class
Lesson 13 object and classLesson 13 object and class
Lesson 13 object and class
 
Chapter 1 - An Overview of Computers and Programming Languages
Chapter 1 - An Overview of Computers and Programming LanguagesChapter 1 - An Overview of Computers and Programming Languages
Chapter 1 - An Overview of Computers and Programming Languages
 
Chapter 13 - Recursion
Chapter 13 - RecursionChapter 13 - Recursion
Chapter 13 - Recursion
 
9781111530532 ppt ch07
9781111530532 ppt ch079781111530532 ppt ch07
9781111530532 ppt ch07
 
Yacc
YaccYacc
Yacc
 
Lex & yacc
Lex & yaccLex & yacc
Lex & yacc
 
9781439035665 ppt ch05
9781439035665 ppt ch059781439035665 ppt ch05
9781439035665 ppt ch05
 
Can programming be liberated from the von neumann style?
Can programming be liberated from the von neumann style?Can programming be liberated from the von neumann style?
Can programming be liberated from the von neumann style?
 
Matlab brochure
Matlab  brochureMatlab  brochure
Matlab brochure
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manual
 
Basics of reflection
Basics of reflectionBasics of reflection
Basics of reflection
 
Lex Tool
Lex ToolLex Tool
Lex Tool
 
10. sub program
10. sub program10. sub program
10. sub program
 
08 subprograms
08 subprograms08 subprograms
08 subprograms
 
LEX & YACC
LEX & YACCLEX & YACC
LEX & YACC
 

Viewers also liked

Biology capitulo8- Una introducción al metabolismo
Biology capitulo8- Una introducción al metabolismoBiology capitulo8- Una introducción al metabolismo
Biology capitulo8- Una introducción al metabolismoerovira1
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10Terry Yoast
 
Lec 30.31 - inheritance
Lec 30.31 -  inheritanceLec 30.31 -  inheritance
Lec 30.31 - inheritancePrincess Sam
 
Lec 47.48 - stream-files
Lec 47.48 - stream-filesLec 47.48 - stream-files
Lec 47.48 - stream-filesPrincess Sam
 
Managing console input and output
Managing console input and outputManaging console input and output
Managing console input and outputgourav kottawar
 
File handling in C++
File handling in C++File handling in C++
File handling in C++Hitesh Kumar
 
file handling c++
file handling c++file handling c++
file handling c++Guddu Spy
 
Object-Oriented Design: Multiple inheritance (C++ and C#)
Object-Oriented Design: Multiple inheritance (C++ and C#)Object-Oriented Design: Multiple inheritance (C++ and C#)
Object-Oriented Design: Multiple inheritance (C++ and C#)Adair Dingle
 

Viewers also liked (10)

Biology capitulo8- Una introducción al metabolismo
Biology capitulo8- Una introducción al metabolismoBiology capitulo8- Una introducción al metabolismo
Biology capitulo8- Una introducción al metabolismo
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
Lec 30.31 - inheritance
Lec 30.31 -  inheritanceLec 30.31 -  inheritance
Lec 30.31 - inheritance
 
Lec 47.48 - stream-files
Lec 47.48 - stream-filesLec 47.48 - stream-files
Lec 47.48 - stream-files
 
Managing console input and output
Managing console input and outputManaging console input and output
Managing console input and output
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 
File handling
File handlingFile handling
File handling
 
file handling c++
file handling c++file handling c++
file handling c++
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
Object-Oriented Design: Multiple inheritance (C++ and C#)
Object-Oriented Design: Multiple inheritance (C++ and C#)Object-Oriented Design: Multiple inheritance (C++ and C#)
Object-Oriented Design: Multiple inheritance (C++ and C#)
 

Similar to 9781285852744 ppt ch11

Similar to 9781285852744 ppt ch11 (20)

Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++
 
Oop(object oriented programming)
Oop(object oriented programming)Oop(object oriented programming)
Oop(object oriented programming)
 
Summer Training Project On C++
Summer Training Project On  C++Summer Training Project On  C++
Summer Training Project On C++
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Learning C++ - Class 4
Learning C++ - Class 4Learning C++ - Class 4
Learning C++ - Class 4
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
 
CIS110 Computer Programming Design Chapter (11)
CIS110 Computer Programming Design Chapter  (11)CIS110 Computer Programming Design Chapter  (11)
CIS110 Computer Programming Design Chapter (11)
 
Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2
 
9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt
 
Csharp introduction
Csharp introductionCsharp introduction
Csharp introduction
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
OOP-Advanced_Programming.pptx
OOP-Advanced_Programming.pptxOOP-Advanced_Programming.pptx
OOP-Advanced_Programming.pptx
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Unit 5.ppt
Unit 5.pptUnit 5.ppt
Unit 5.ppt
 
Classes
ClassesClasses
Classes
 
CS1Lesson13-IntroClasses.pptx
CS1Lesson13-IntroClasses.pptxCS1Lesson13-IntroClasses.pptx
CS1Lesson13-IntroClasses.pptx
 
Oops ppt
Oops pptOops ppt
Oops ppt
 

More from Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01Terry Yoast
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18Terry Yoast
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17Terry Yoast
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry Yoast
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14Terry Yoast
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11Terry Yoast
 

More from Terry Yoast (20)

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 

Recently uploaded

fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 

Recently uploaded (20)

fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 

9781285852744 ppt ch11

  • 2. 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 the destructors of base and derived classes work 2C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 3. Objectives (cont’d.) – Learn how to construct the header file of a derived class – Become aware of stream classes hierarchy – Explore three types of inheritance: public, protected, and private – Learn about composition (aggregation) – Become familiar with the three basic principles of object-oriented design 3C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 4. Introduction • Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship) – Composition, or aggregation: (“has-a” relationship) 4C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 5. Inheritance 5C++ Programming: From Problem Analysis to Program Design, Seventh Edition • Inheritance: “is-a” relationship – Example: “every employee is a person” • Inheritance allows creation of new classes from existing classes – Derived classes: new classes created from the existing class – Base class: the original class • Derived class inherits the properties of its base classes
  • 6. Inheritance (cont’d.) • Inheritance helps reduce software complexity • Single inheritance: derived class has a single base class • Multiple inheritance: derived class has more than one base class • Public inheritance: all public members of base class are inherited as public members by derived class 6C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 7. Inheritance (cont’d.) • Inheritance can be viewed as a tree-like, or hierarchical, structure between the base class and its derived classes 7C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 8. Inheritance (cont’d.) • Syntax of a derived class: – memberAccessSpecifier is public, protected, or private (default) • private members of a base class are private to the base class – Derived class cannot directly access them 8C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 9. Inheritance (cont’d.) • public members of base class can be inherited as public or private members • Derived class can include additional members (data and/or functions) • Derived class can redefine public member functions of the base class – Applies only to the objects of the derived class • All member variables of the base class are also member variables of the derived class 9C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 10. Redefining (Overriding) Member Functions of the Base Class • To redefine a public member function: – Corresponding function in derived class must have same name/number/types of parameters • 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 appropriate parameter list 10C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 11. Redefining Member Functions of the Base Class (cont’d.) 11C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 12. Redefining Member Functions of the Base Class (cont’d.) • boxType is derived from rectangleType, and it is a public inheritance – Also overrides print and area 12C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 13. Constructors of Derived and Base Classes • Derived class constructor cannot directly access private members of the base class – It 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 13C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 14. Destructors in a Derived Class • Destructors: used to deallocate dynamic memory allocated by the objects of a class • When a derived class object goes out of scope – Automatically invokes its destructor • When the destructor of the derived class executes – Automatically invokes the destructor of the base class 14C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 15. Header File of a Derived Class • To define new classes, create new header files • To create new derived classes, include commands that specify where the base class definitions can be found • Definitions of the member functions can be placed in a separate file 15C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 16. Multiple Inclusions of a Header File • Use the preprocessor command (#include) to include a header file in a program – 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 16C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 17. C++ Stream Classes • ios is the base class for all stream classes – Contains formatting flags and member functions to access/modify the flag settings 17C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 18. C++ Stream Classes (cont’d.) • istream and ostream provide operations for data transfer between memory and devices – istream defines the extraction operator (>>) and functions 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 for these 18C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 19. Protected Members of a Class • Derived class cannot directly access private members of it base class • To give it direct access, declare that member as protected 19C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 20. Inheritance as public, protected, or private • Assume class B is derived from class A with • If memberAccessSpecifier is public: –public members of A are public in B, and can be directly accessed in class B –protected members of A are protected in 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 only through public or protected members of A 20C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 21. Inheritance as public, protected, or private (cont’d.) • 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 only through public or protected members of A 21C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 22. • 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 only through public/protected members of A 22 Inheritance as public, protected, or private (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 23. Composition (Aggregation) • In composition, one or more member(s) of a class are objects of another class type • Composition (aggregation): “has-a” relation • Arguments to the constructor of a member- object are specified in the heading part of the definition of the constructor 23C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 24. Composition (Aggregation) (cont’d.) • Member-objects of a class are constructed in the order they are declared – Not in the order listed in the constructor’s member initialization list • They are constructed before the containing class objects are constructed 24C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 25. Object-Oriented Design (OOD) and Object- Oriented Programming (OOP) • The fundamental principles of object-oriented design (OOD) are: – Encapsulation: combines data and operations on data in a single unit – Inheritance: creates new objects (classes) from existing objects (classes) – Polymorphism: the ability to use the same expression to denote different operations 25C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 26. OOD and OOP (cont’d.) • In OOD: – Object is a fundamental entity – Debug at the class level – A program is a collection of interacting objects • OOD encourages code reuse • Object-oriented programming (OOP) implements OOD 26C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 27. OOD and OOP (cont’d.) • C++ supports OOP through the use of classes • Function name and operators can be overloaded • Polymorphic function or operator: has many forms – Example: division with floating point and division with integer operands 27C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 28. OOD and OOP (cont’d.) • Templates provide parametric polymorphism • C++ provides virtual functions to implement polymorphism in an inheritance hierarchy – Allows run-time selection of appropriate member functions • Objects are created when class variables are declared • Objects interact with each other via function calls 28C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 29. OOD and OOP (cont’d.) • 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 29C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 30. Identifying Classes, Objects, and Operations • To find 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 30C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 31. Identifying Classes, Objects, and Operations (cont’d.) • State this problem as follows: – Write a program to input the dimensions of a cylinder and calculate and print the surface area and volume – Nouns are bold and verbs are italic – From the list of nouns, can visualize a cylinder as a class (cylinderType) from which we can create many cylinder objects of various dimensions 31C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 32. Identifying Classes, Objects, and Operations (cont’d.) • These nouns are characteristics of a cylinder, so they will not be classes: – Dimensions – Surface area – Volume • Next, determine three pieces of information about this class: – Operations that an object can perform – Operations that can be performed on an object – Information that an object must maintain 32C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 33. Identifying Classes, Objects, and Operations (cont’d.) • From the verbs, list possible operations that an object of that class can perform, or have performed, on itself – For the cylinderType class: • Input • Calculate • Print – Dimensions of the cylinder represent the class’s data 33C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 34. Identifying Classes, Objects, and Operations (cont’d.) • Identifying classes via nouns and verbs from problem descriptions is not the only technique possible • There are several other OOD techniques in the literature 34C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 35. 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 35C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 36. Summary (cont’d.) • 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 36C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 37. Summary (cont’d.) • 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 (aggregation): – 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 37C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 38. Summary (cont’d.) • Three basic principles of OOD: – Encapsulation – Inheritance – Polymorphism • To find classes: – Describe the problem – Choose classes from the list of nouns – Choose operations from the list of verbs 38C++ Programming: From Problem Analysis to Program Design, Seventh Edition