SlideShare a Scribd company logo
1 of 46
Chapter 10:
Classes and Data Abstraction
Objectives
In this chapter, you will:
– Learn about classes
– Learn about private, protected, and
public members of a class
– Explore how classes are implemented
– Become aware of accessor and mutator functions
– Examine constructors and destructors
2C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Objectives (cont’d.)
– Learn about the abstract data type (ADT)
– Explore how classes are used to implement ADTs
– Become aware of the differences between a
struct and a class
– Learn about information hiding
– Explore how information hiding is implemented in
C++
– Learn about the static members of a class
3C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Classes
• Object-oriented design (OOD): a problem
solving methodology
• Objects: components of a solution
• Class: a collection of a fixed number of
components
• Member: a component of a class
4C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Classes (cont’d.)
• Class definition:
– Defines a data type; no memory is allocated
– Don’t forget the semicolon after the closing brace
• Syntax:
5C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Classes (cont’d.)
• Class member can be a variable or a function
• If a member of a class is a variable
– It is declared like any other variable
– You cannot initialize a variable when you declare
it
• If a member of a class is a function
– Function prototype is listed
– Function members can (directly) access any
member of the class
6C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Classes (cont’d.)
• Three categories of class members:
– private (default)
• Member cannot be accessed outside the class
– public
• Member is accessible outside the class
– protected
7C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Unified Modeling Language
Class Diagrams
• Unified Modeling Language (UML) notation:
used to graphically describe a class and its
members
– +: member is public
– -: member is private
– #: member is protected
8C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Unified Modeling Language Class
Diagrams (cont’d.)
9C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Variable (Object) Declaration
• Once defined, you can declare variables of
that class type
clockType myClock;
• A class variable is called a class object or
class instance
10C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Accessing Class Members
• Once an object is declared, it can access the
public members of the class
• Syntax:
– The dot (.) is the member access operator
• If an object is declared in the definition of a
member function of the class, it can access
the public and private members
11C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Built-in Operations on Classes
• Most of C++’s built-in operations do not apply to
classes
– Arithmetic operators cannot be used on class objects
unless the operators are overloaded
– Cannot use relational operators to compare two class
objects for equality
• Built-in operations that are valid for class objects:
– Member access (.)
– Assignment (=)
12C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Assignment Operator and Classes
13C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Class Scope
• An object can be automatic or static
– Automatic: created when the declaration is
reached and destroyed when the surrounding
block is exited
– Static: created when the declaration is reached
and destroyed when the program terminates
• Object has the same scope as other variables
14C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Class Scope (cont’d.)
• A member of the class is local to the
class
• Can access a class member outside the
class by using the class object name and
the member access operator (.)
15C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Functions and Classes
• Objects can be passed as parameters to
functions and returned as function values
• As parameters to functions
– Objects can be passed by value or by reference
• If an object is passed by value
– Contents of data members of the actual
parameter are copied into the corresponding data
members of the formal parameter
16C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Reference Parameters and Class
Objects (Variables)
• Passing by value might require a large amount
of storage space and a considerable amount
of computer time to copy the value of the
actual parameter into the formal parameter
• If a variable is passed by reference
– The formal parameter receives only the address of
the actual parameter
17C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Reference Parameters and Class
Objects (Variables) (cont’d.)
• Pass by reference is an efficient way to pass a
variable as a parameter
– Problem: when passing by reference, the actual
parameter changes when formal parameter
changes
– Solution: use const in the formal parameter
declaration
18C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Implementation of Member
Functions
• Must write the code for functions defined as
function prototypes
• Prototypes are left in the class to keep the
class smaller and to hide the implementation
• To access identifiers local to the class, use the
scope resolution operator ::
19C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Implementation of Member
Functions (cont’d.)
20C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Implementation of Member
Functions (cont’d.)
21C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Implementation of Member
Functions (cont’d.)
• Once a class is properly defined and
implemented, it can be used in a program
– A program that uses/manipulates objects of a
class is called a client of that class
• When you declare objects of the class
clockType, each object has its own copy of
the member variables (hr, min, and sec)
• Called instance variables of the class
– Every object has its own instance of the data
22C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Accessor and Mutator Functions
• Accessor function: member function that only
accesses the value(s) of member variable(s)
• Mutator function: member function that modifies
the value(s) of member variable(s)
• Constant function:
– Member function that cannot modify member variables
– Use const in function heading
23C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Order of public and private
Members of a Class
• C++ has no fixed order in which to declare
public and private members
• By default, all members of a class are
private
• Use the member access specifier public to
make a member available for public access
24C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Constructors
• Use constructors to guarantee that member
variables of a class are initialized
• Two types of constructors:
– With parameters
– Without parameters (default constructor)
– Name of a constructor = name of the class
– A constructor has no type
25C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Constructors (cont’d.)
• A class can have more than one constructor
– Each must have a different formal parameter list
• Constructors execute automatically when a
class object enters its scope
• They cannot be called like other functions
• Which constructor executes depends on the
types of values passed to the class object
when the class object is declared
26C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Invoking a Constructor
• A constructor is automatically executed when
a class variable is declared
• Because a class may have more than one
constructor, you can invoke a specific
constructor
27C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Invoking the Default Constructor
• To invoke the default constructor:
• Example:
clockType yourClock;
28C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Invoking a Constructor with
Parameters
• Syntax:
• Number and type of arguments should match
the formal parameters (in the order given) of
one of the constructors
– Otherwise, C++ uses type conversion and looks for
the best match
– Any ambiguity causes a compile-time error
29C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Constructors and Default Parameters
• A constructor can have default parameters
– Rules for declaring formal parameters are the
same as for declaring default formal parameters in
a function
– Actual parameters are passed according to same
rules for functions
• Default constructor: a constructor with no
parameters or with all default parameters
30C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Classes and Constructors: A
Precaution
• If a class has no constructor(s), C++ provides
the default constructor
– However, object declared is still uninitialized
• If a class includes constructor(s) with
parameter(s), but not the default constructor
– C++ does not provide the default constructor
31C++ Programming: From Problem Analysis to Program Design, Seventh Edition
In-line initialization of Data Members
and the Default Constructor
• C++11 standard allows member initialization
in class declarations
• Called in-line initialization
• Can eliminate the need for a default
constructor
• Not all compilers recognize this feature
32C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Arrays of Class Objects (Variables)
and Constructors
• If you declare an array of class objects, the
class should have the default constructor
33C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Destructors
• Destructors are functions without any type
• The name of a destructor is the character '~'
followed by class name
– For example:
~clockType();
• A class can have only one destructor
– The destructor has no parameters
• Destructor automatically executes when the
class object goes out of scope
34C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Data Abstract, Classes,
and Abstract Data Types
• Abstraction
– Separating design details from usage
– Separating the logical properties from the
implementation details
• Abstraction can also be applied to data
• Abstract data type (ADT): data type that
separates the logical properties from the
implementation details
35C++ Programming: From Problem Analysis to Program Design, Seventh Edition
A struct Versus a class
• By default, members of a struct are
public
– private specifier can be used in a struct to
make a member private
• By default, the members of a class are
private
• classes and structs have the same
capabilities
36C++ Programming: From Problem Analysis to Program Design, Seventh Edition
A struct Versus a class (cont’d.)
• In C++, the definition of a struct was
expanded to include member functions,
constructors, and destructors
• If all member variables of a class are
public and there are no member functions
– Use a struct
37C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Information Hiding
• Information hiding: hiding the details of the
operations on the data
• Interface (header) file: contains the specification
details
• File extension is .h
• Implementation file: contains the
implementation details
• File extension is .cpp
• In header file, include function prototypes and
comments that briefly describe the functions
– Specify preconditions and/or postconditions
38C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Information Hiding (cont’d.)
• Implementation file must include header file
via include statement
• In include statement:
– User-defined header files are enclosed in double
quotes
– System-provided header files are enclosed
between angular brackets
39C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Information Hiding (cont’d.)
• Precondition: A statement specifying the
condition(s) that must be true before the
function is called
• Postcondition: A statement specifying what is
true after the function call is completed
40C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Executable Code
• To use an object in a program
– The program must be able to access the
implementation
• Visual C++ 2012 Express, Visual Studio 2012,
and C++ Builder put the editor, compiler, and
linker into a package
– One command (build, rebuild, or make) compiles
program and links it with the other necessary files
– These systems also manage multiple file programs
in the form of a project
41C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Static Members of a Class
• Use the keyword static to declare a
function or variable of a class as static
• A public static function or member of a
class can be accessed using the class name
and the scope resolution operator
• static member variables of a class exist
even if no object of that class type exists
42C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Static Members of a Class (cont’d.)
• Multiple objects of a class each have their
own copy of non-static member variables
• All objects of a class share any static member
of the class
43C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Summary
• Class: collection of a fixed number of
components
• Members: components of a class
– Accessed by name
– Classified into one of three categories:
• private, protected, and public
• Class variables are called class objects or,
simply, objects
44C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Summary (cont’d.)
• The only built-in operations on classes are
assignment and member selection
• Constructors guarantee that data members
are initialized when an object is declared
– Default constructor has no parameters
• Destructor automatically executes when a
class object goes out of scope
– A class can have only one destructor
– The destructor has no parameters
45C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Summary (cont’d.)
• Abstract data type (ADT): data type that
separates the logical properties from the
implementation details
• A public static member, function or
data, of a class can be accessed using the class
name and the scope resolution operator
• Static data members of a class exist even
when no object of the class type exists
• Instance variables: non-static data members
46C++ Programming: From Problem Analysis to Program Design, Seventh Edition

More Related Content

What's hot

9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12Terry Yoast
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02Terry Yoast
 
9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08Terry Yoast
 
9781285852744 ppt ch16
9781285852744 ppt ch169781285852744 ppt ch16
9781285852744 ppt ch16Terry Yoast
 
9781285852744 ppt ch14
9781285852744 ppt ch149781285852744 ppt ch14
9781285852744 ppt ch14Terry Yoast
 
Programming in c (importance of c)
Programming in c (importance of c)Programming in c (importance of c)
Programming in c (importance of c)ViswanathanS21
 
Reactive cocoa 101改
Reactive cocoa 101改Reactive cocoa 101改
Reactive cocoa 101改Jeff Lee
 
Object oriented programming 11 preprocessor directives and program structure
Object oriented programming 11 preprocessor directives and program structureObject oriented programming 11 preprocessor directives and program structure
Object oriented programming 11 preprocessor directives and program structureVaibhav Khanna
 
9781439035665 ppt ch05
9781439035665 ppt ch059781439035665 ppt ch05
9781439035665 ppt ch05Terry Yoast
 

What's hot (20)

9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
 
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
 
Lesson 4.2 5th and 6th step
Lesson 4.2 5th and 6th stepLesson 4.2 5th and 6th step
Lesson 4.2 5th and 6th step
 
Lesson 2 beginning the problem solving process
Lesson 2 beginning the problem solving processLesson 2 beginning the problem solving process
Lesson 2 beginning the problem solving process
 
Lesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving processLesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving process
 
Lesson 5.2 logical operators
Lesson 5.2 logical operatorsLesson 5.2 logical operators
Lesson 5.2 logical operators
 
Lesson 5 .1 selection structure
Lesson 5 .1 selection structureLesson 5 .1 selection structure
Lesson 5 .1 selection structure
 
Lesson 3.2 data types for memory location
Lesson 3.2 data types for memory locationLesson 3.2 data types for memory location
Lesson 3.2 data types for memory location
 
Programming in c (importance of c)
Programming in c (importance of c)Programming in c (importance of c)
Programming in c (importance of c)
 
Lesson 1 introduction to programming
Lesson 1 introduction to programmingLesson 1 introduction to programming
Lesson 1 introduction to programming
 
Lesson 3.1 variables and constant
Lesson 3.1 variables and constantLesson 3.1 variables and constant
Lesson 3.1 variables and constant
 
Ladc presentation
Ladc presentationLadc presentation
Ladc presentation
 
Reactive cocoa 101改
Reactive cocoa 101改Reactive cocoa 101改
Reactive cocoa 101改
 
Object oriented programming 11 preprocessor directives and program structure
Object oriented programming 11 preprocessor directives and program structureObject oriented programming 11 preprocessor directives and program structure
Object oriented programming 11 preprocessor directives and program structure
 
Lex & yacc
Lex & yaccLex & yacc
Lex & yacc
 
9781439035665 ppt ch05
9781439035665 ppt ch059781439035665 ppt ch05
9781439035665 ppt ch05
 

Similar to 9781285852744 ppt ch10

9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.pptLokeshK66
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan collegeahmed hmed
 
Learning C++ - Class 4
Learning C++ - Class 4Learning C++ - Class 4
Learning C++ - Class 4Ali Aminian
 
Computer Programming
Computer ProgrammingComputer Programming
Computer ProgrammingBurhan Fakhar
 
Oop(object oriented programming)
Oop(object oriented programming)Oop(object oriented programming)
Oop(object oriented programming)geetika goyal
 
9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.pptLokeshK66
 
clean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptxclean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptxsaber tabatabaee
 
B.sc CSIT 2nd semester C++ unit-1
B.sc CSIT  2nd semester C++ unit-1B.sc CSIT  2nd semester C++ unit-1
B.sc CSIT 2nd semester C++ unit-1Tekendra Nath Yogi
 
chapter 1 software design.pptx
chapter 1 software design.pptxchapter 1 software design.pptx
chapter 1 software design.pptxrecoveraccount1
 
CIS110 Computer Programming Design Chapter (11)
CIS110 Computer Programming Design Chapter  (11)CIS110 Computer Programming Design Chapter  (11)
CIS110 Computer Programming Design Chapter (11)Dr. Ahmed Al Zaidy
 
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 2Nicole Ryan
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsakreyi
 
Learn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsLearn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsEng Teong Cheah
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ Dev Chauhan
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1stConnex
 

Similar to 9781285852744 ppt ch10 (20)

9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan college
 
CS1Lesson13-IntroClasses.pptx
CS1Lesson13-IntroClasses.pptxCS1Lesson13-IntroClasses.pptx
CS1Lesson13-IntroClasses.pptx
 
Learning C++ - Class 4
Learning C++ - Class 4Learning C++ - Class 4
Learning C++ - Class 4
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
 
C++.ppt
C++.pptC++.ppt
C++.ppt
 
Object Oriented Programming using C++ - Part 1
Object Oriented Programming using C++ - Part 1Object Oriented Programming using C++ - Part 1
Object Oriented Programming using C++ - Part 1
 
Oop(object oriented programming)
Oop(object oriented programming)Oop(object oriented programming)
Oop(object oriented programming)
 
9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt
 
clean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptxclean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptx
 
B.sc CSIT 2nd semester C++ unit-1
B.sc CSIT  2nd semester C++ unit-1B.sc CSIT  2nd semester C++ unit-1
B.sc CSIT 2nd semester C++ unit-1
 
chapter 1 software design.pptx
chapter 1 software design.pptxchapter 1 software design.pptx
chapter 1 software design.pptx
 
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
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Learn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsLearn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & Methods
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 

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

MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 

Recently uploaded (20)

MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 

9781285852744 ppt ch10

  • 1. Chapter 10: Classes and Data Abstraction
  • 2. Objectives In this chapter, you will: – Learn about classes – Learn about private, protected, and public members of a class – Explore how classes are implemented – Become aware of accessor and mutator functions – Examine constructors and destructors 2C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 3. Objectives (cont’d.) – Learn about the abstract data type (ADT) – Explore how classes are used to implement ADTs – Become aware of the differences between a struct and a class – Learn about information hiding – Explore how information hiding is implemented in C++ – Learn about the static members of a class 3C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 4. Classes • Object-oriented design (OOD): a problem solving methodology • Objects: components of a solution • Class: a collection of a fixed number of components • Member: a component of a class 4C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 5. Classes (cont’d.) • Class definition: – Defines a data type; no memory is allocated – Don’t forget the semicolon after the closing brace • Syntax: 5C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 6. Classes (cont’d.) • Class member can be a variable or a function • If a member of a class is a variable – It is declared like any other variable – You cannot initialize a variable when you declare it • If a member of a class is a function – Function prototype is listed – Function members can (directly) access any member of the class 6C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 7. Classes (cont’d.) • Three categories of class members: – private (default) • Member cannot be accessed outside the class – public • Member is accessible outside the class – protected 7C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 8. Unified Modeling Language Class Diagrams • Unified Modeling Language (UML) notation: used to graphically describe a class and its members – +: member is public – -: member is private – #: member is protected 8C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 9. Unified Modeling Language Class Diagrams (cont’d.) 9C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 10. Variable (Object) Declaration • Once defined, you can declare variables of that class type clockType myClock; • A class variable is called a class object or class instance 10C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 11. Accessing Class Members • Once an object is declared, it can access the public members of the class • Syntax: – The dot (.) is the member access operator • If an object is declared in the definition of a member function of the class, it can access the public and private members 11C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 12. Built-in Operations on Classes • Most of C++’s built-in operations do not apply to classes – Arithmetic operators cannot be used on class objects unless the operators are overloaded – Cannot use relational operators to compare two class objects for equality • Built-in operations that are valid for class objects: – Member access (.) – Assignment (=) 12C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 13. Assignment Operator and Classes 13C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 14. Class Scope • An object can be automatic or static – Automatic: created when the declaration is reached and destroyed when the surrounding block is exited – Static: created when the declaration is reached and destroyed when the program terminates • Object has the same scope as other variables 14C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 15. Class Scope (cont’d.) • A member of the class is local to the class • Can access a class member outside the class by using the class object name and the member access operator (.) 15C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 16. Functions and Classes • Objects can be passed as parameters to functions and returned as function values • As parameters to functions – Objects can be passed by value or by reference • If an object is passed by value – Contents of data members of the actual parameter are copied into the corresponding data members of the formal parameter 16C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 17. Reference Parameters and Class Objects (Variables) • Passing by value might require a large amount of storage space and a considerable amount of computer time to copy the value of the actual parameter into the formal parameter • If a variable is passed by reference – The formal parameter receives only the address of the actual parameter 17C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 18. Reference Parameters and Class Objects (Variables) (cont’d.) • Pass by reference is an efficient way to pass a variable as a parameter – Problem: when passing by reference, the actual parameter changes when formal parameter changes – Solution: use const in the formal parameter declaration 18C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 19. Implementation of Member Functions • Must write the code for functions defined as function prototypes • Prototypes are left in the class to keep the class smaller and to hide the implementation • To access identifiers local to the class, use the scope resolution operator :: 19C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 20. Implementation of Member Functions (cont’d.) 20C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 21. Implementation of Member Functions (cont’d.) 21C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 22. Implementation of Member Functions (cont’d.) • Once a class is properly defined and implemented, it can be used in a program – A program that uses/manipulates objects of a class is called a client of that class • When you declare objects of the class clockType, each object has its own copy of the member variables (hr, min, and sec) • Called instance variables of the class – Every object has its own instance of the data 22C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 23. Accessor and Mutator Functions • Accessor function: member function that only accesses the value(s) of member variable(s) • Mutator function: member function that modifies the value(s) of member variable(s) • Constant function: – Member function that cannot modify member variables – Use const in function heading 23C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 24. Order of public and private Members of a Class • C++ has no fixed order in which to declare public and private members • By default, all members of a class are private • Use the member access specifier public to make a member available for public access 24C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 25. Constructors • Use constructors to guarantee that member variables of a class are initialized • Two types of constructors: – With parameters – Without parameters (default constructor) – Name of a constructor = name of the class – A constructor has no type 25C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 26. Constructors (cont’d.) • A class can have more than one constructor – Each must have a different formal parameter list • Constructors execute automatically when a class object enters its scope • They cannot be called like other functions • Which constructor executes depends on the types of values passed to the class object when the class object is declared 26C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 27. Invoking a Constructor • A constructor is automatically executed when a class variable is declared • Because a class may have more than one constructor, you can invoke a specific constructor 27C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 28. Invoking the Default Constructor • To invoke the default constructor: • Example: clockType yourClock; 28C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 29. Invoking a Constructor with Parameters • Syntax: • Number and type of arguments should match the formal parameters (in the order given) of one of the constructors – Otherwise, C++ uses type conversion and looks for the best match – Any ambiguity causes a compile-time error 29C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 30. Constructors and Default Parameters • A constructor can have default parameters – Rules for declaring formal parameters are the same as for declaring default formal parameters in a function – Actual parameters are passed according to same rules for functions • Default constructor: a constructor with no parameters or with all default parameters 30C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 31. Classes and Constructors: A Precaution • If a class has no constructor(s), C++ provides the default constructor – However, object declared is still uninitialized • If a class includes constructor(s) with parameter(s), but not the default constructor – C++ does not provide the default constructor 31C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 32. In-line initialization of Data Members and the Default Constructor • C++11 standard allows member initialization in class declarations • Called in-line initialization • Can eliminate the need for a default constructor • Not all compilers recognize this feature 32C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 33. Arrays of Class Objects (Variables) and Constructors • If you declare an array of class objects, the class should have the default constructor 33C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 34. Destructors • Destructors are functions without any type • The name of a destructor is the character '~' followed by class name – For example: ~clockType(); • A class can have only one destructor – The destructor has no parameters • Destructor automatically executes when the class object goes out of scope 34C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 35. Data Abstract, Classes, and Abstract Data Types • Abstraction – Separating design details from usage – Separating the logical properties from the implementation details • Abstraction can also be applied to data • Abstract data type (ADT): data type that separates the logical properties from the implementation details 35C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 36. A struct Versus a class • By default, members of a struct are public – private specifier can be used in a struct to make a member private • By default, the members of a class are private • classes and structs have the same capabilities 36C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 37. A struct Versus a class (cont’d.) • In C++, the definition of a struct was expanded to include member functions, constructors, and destructors • If all member variables of a class are public and there are no member functions – Use a struct 37C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 38. Information Hiding • Information hiding: hiding the details of the operations on the data • Interface (header) file: contains the specification details • File extension is .h • Implementation file: contains the implementation details • File extension is .cpp • In header file, include function prototypes and comments that briefly describe the functions – Specify preconditions and/or postconditions 38C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 39. Information Hiding (cont’d.) • Implementation file must include header file via include statement • In include statement: – User-defined header files are enclosed in double quotes – System-provided header files are enclosed between angular brackets 39C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 40. Information Hiding (cont’d.) • Precondition: A statement specifying the condition(s) that must be true before the function is called • Postcondition: A statement specifying what is true after the function call is completed 40C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 41. Executable Code • To use an object in a program – The program must be able to access the implementation • Visual C++ 2012 Express, Visual Studio 2012, and C++ Builder put the editor, compiler, and linker into a package – One command (build, rebuild, or make) compiles program and links it with the other necessary files – These systems also manage multiple file programs in the form of a project 41C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 42. Static Members of a Class • Use the keyword static to declare a function or variable of a class as static • A public static function or member of a class can be accessed using the class name and the scope resolution operator • static member variables of a class exist even if no object of that class type exists 42C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 43. Static Members of a Class (cont’d.) • Multiple objects of a class each have their own copy of non-static member variables • All objects of a class share any static member of the class 43C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 44. Summary • Class: collection of a fixed number of components • Members: components of a class – Accessed by name – Classified into one of three categories: • private, protected, and public • Class variables are called class objects or, simply, objects 44C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 45. Summary (cont’d.) • The only built-in operations on classes are assignment and member selection • Constructors guarantee that data members are initialized when an object is declared – Default constructor has no parameters • Destructor automatically executes when a class object goes out of scope – A class can have only one destructor – The destructor has no parameters 45C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 46. Summary (cont’d.) • Abstract data type (ADT): data type that separates the logical properties from the implementation details • A public static member, function or data, of a class can be accessed using the class name and the scope resolution operator • Static data members of a class exist even when no object of the class type exists • Instance variables: non-static data members 46C++ Programming: From Problem Analysis to Program Design, Seventh Edition