Sunteți pe pagina 1din 42

Introduction to Programming in C++

Seventh Edition

Chapter 15:
Classes and Objects

Objectives
Differentiate between procedure-oriented and objectoriented programming
Define the terms used in object-oriented programming
Create a class definition
Instantiate an object from a class that you define
Create a default constructor
Create a parameterized constructor
Include methods other than constructors in a class
Overload the methods in a class
An Introduction to Programming with C++, Seventh Edition

Object-Oriented Terminology
In a procedure-oriented program, the primary
components are the tasks to accomplish
In an object-oriented program, the primary components
are objects
An object is anything that can be seen, touched, or used.

When writing an object-oriented program, the


programmer focuses on the objects (rather than the
tasks) that the program can use to accomplish its goal

An Introduction to Programming with C++, Seventh Edition

Object-Oriented Terminology (contd.)


Every object in an object-oriented program is created
from a class, which is a pattern or blueprint that the
computer uses to create the object
Using object-oriented programming (OOP) terminology,
objects are instantiated (created) from a class, and
each object is referred to as an instance of the class
Every object has attributes, which are characteristics
that describe the object
An objects behaviors are either actions that the object
is capable of performing or actions to which the object
can respond
An Introduction to Programming with C++, Seventh Edition

Object-Oriented Terminology (contd.)


A class contains, or encapsulates, all of the attributes
and behaviors of the object it instantiates
Abstraction refers to the hiding of the internal details of
an object from the user
Hiding the internal details helps prevent the user from
making inadvertent changes to the object
Attributes and behaviors that are not hidden are
considered to be exposed
Inheritance refers to the fact that you can create one
class from another class
An Introduction to Programming with C++, Seventh Edition

Object-Oriented Terminology (contd.)


When you create one class from another class, the new
class is said to be the derived class
The derived class inherits the attributes and behaviors
of the original class, called the base class
Polymorphism is the object-oriented feature that
allows the same instruction to be carried out differently
depending upon the object

An Introduction to Programming with C++, Seventh Edition

Defining a Class in C++


Previously, you instantiated objects using existing
classes, such as the string and ofstream classes
You used the instantiated objects in a variety of ways in
many different programs

In C++, you can also define you own classes and create
instances (objects) from those classes
Use a class definition to specify the attributes and
behaviors

An Introduction to Programming with C++, Seventh Edition

Defining a Class in C++ (contd.)

Figure 15-1 How to define a class


An Introduction to Programming with C++, Seventh Edition

Defining a Class in C++ (contd.)


The class definition contains two sections: a declaration
section and an optional implementation section
The declaration section contains the C++ class
statement
Within the class statement, list the attributes and
behaviors of the objects that the class will create

An Introduction to Programming with C++, Seventh Edition

Defining a Class in C++ (contd.)


The implementation section contains one definition for
each prototype listed in the declaration section
In most cases, the attributes (called data members) are
represented by method prototypes
A method is simply a function that is defined in a class
definition

An Introduction to Programming with C++, Seventh Edition

10

Instantiating an Object and Referring


to a Public Member

Figure 15-2 How to instantiate an object

An Introduction to Programming with C++, Seventh Edition

11

Instantiating an Object and Referring


to a Public Member (contd.)

Figure 15-3 How to refer to a public member of an objects class

An Introduction to Programming with C++, Seventh Edition

12

Example 1 Private Data Member and


Public Member Methods
The class definition contains private data members and
public member methods
Enables data security by regulating access to the data
members through the member methods

The public member methods of a class define the tasks


that an object can perform

An Introduction to Programming with C++, Seventh Edition

13

Example 1 Private Data Member and


Public Member Methods (contd.)

Figure 15-4 Square class definition


An Introduction to Programming with C++, Seventh Edition

14

Example 1 Private Data Member and


Public Member Methods (contd.)
A constructor is a class method whose instructions the
computer automatically processes each time an object
is instantiated from the class
Every class should have at least one constructor
A constructor that has no formal parameters is called
the default constructor
A class can have only one default constructor
The sole purpose of a constructor is to initialize the
classs private variables

An Introduction to Programming with C++, Seventh Edition

15

Example 1 Private Data Member and


Public Member Methods (contd.)

Figure 15-5 Patio area program


An Introduction to Programming with C++, Seventh Edition

16

Example 1 Private Data Member and


Public Member Methods (contd.)

Figure 15-5 Patio area program (contd.)

An Introduction to Programming with C++, Seventh Edition

17

Example 1 Private Data Member and


Public Member Methods (contd.)

Figure 15-6 Sample run of the patio area program

An Introduction to Programming with C++, Seventh Edition

18

Header Files
Although you can enter a class definition in the program
that uses the class, most programmers enter a class
definition in a separate text file called a header file
Header filenames end with .h
Use an #include directive to include the header file in a
program

An Introduction to Programming with C++, Seventh Edition

19

Header Files (contd.)

Figure 15-7 Square class definition entered in the Square.h header file
An Introduction to Programming with C++, Seventh Edition

20

Header Files (contd.)

Figure 15-8 Modified patio area program


An Introduction to Programming with C++, Seventh Edition

21

Example 2 A Class that Contains a


Parameterized Constructor
Constructors that contain at least one parameter are
called parameterized constructors
A methods name combined with its optional parameter
list is called the methods signature

An Introduction to Programming with C++, Seventh Edition

22

Example 2 A Class that Contains a


Parameterized Constructor (contd.)

Figure 15-9 Modified Square class definition entered in


the Modified Square.h header file
An Introduction to Programming with C++, Seventh Edition

23

Example 2 A Class that Contains a


Parameterized Constructor (contd.)

Figure 15-9 Modified Square class definition entered in


the Modified Square.h header file (contd.)

An Introduction to Programming with C++, Seventh Edition

24

Example 2 A Class that Contains a


Parameterized Constructor (contd.)

Figure 15-10 Modified patio area program using the


parameterized constructor

An Introduction to Programming with C++, Seventh Edition

25

Example 2 A Class that Contains a


Parameterized Constructor (contd.)

Figure 15-10 Modified patio area program using the


parameterized constructor (contd.)

An Introduction to Programming with C++, Seventh Edition

26

Example 3 Reusing a Class


In examples 1 and 2, you used the Square class to
create an object that represented a square patio
In this example, use the Square class to create objects
that represent a square pizza and a square pizza slice
What are the advantages in using the Square class?

An Introduction to Programming with C++, Seventh Edition

27

Example 3 Reusing a Class (contd.)

Figure 15-11 Pizza slices program

An Introduction to Programming with C++, Seventh Edition

28

Example 3 Reusing a Class (contd.)

Figure 15-12 Sample run of the pizza slices program

An Introduction to Programming with C++, Seventh Edition

29

Example 4 A Class that Contains


Overloaded Methods
When two or more methods have the same name but
different parameter lists, the methods are overloaded
The two constructors in Figure 15-13 are overloaded
methods because both have the same name but a
different parameter list

An Introduction to Programming with C++, Seventh Edition

30

Example 4 A Class that Contains


Overloaded Methods (contd.)

Figure 15-13 Square class definition entered in the Overloaded


Square.h file
An Introduction to Programming with C++, Seventh Edition

31

Example 4 A Class that Contains


Overloaded Methods (contd.)

Figure 15-13 Square class definition entered in the Overloaded


Square.h file (contd.)

An Introduction to Programming with C++, Seventh Edition

32

Example 4 A Class that Contains


Overloaded Methods (contd.)

Figure 15-14 Modified pizza slices program


An Introduction to Programming with C++, Seventh Edition

33

Example 4 A Class that Contains


Overloaded Methods (contd.)

Figure 15-15 Another sample run of the pizza slices program

An Introduction to Programming with C++, Seventh Edition

34

Summary
A class encapsulates all of an objects attributes and
behaviors
Attributes: the characteristics that describe the object
Behaviors: the actions that the object can perform or to
which the object can respond

Abstraction: the hiding of an objects internal details


from the user
expose the user only to attributes and behaviors that are
necessary to use the object; hide everything else to
prevent the user from making inadvertent changes to the
object
An Introduction to Programming with C++, Seventh Edition

35

Summary (contd.)
Polymorphism: the object-oriented feature that allows
the same instruction to be carried out differently
depending on the object
A class definition contains two sections: declaration and
implementation
Another name for creating an object is instantiation

An Introduction to Programming with C++, Seventh Edition

36

Summary (contd.)
Most C++ programmers enter class definitions in header
files, which have a file extension of .h
You can use a constructor to initialize the data
members in a class when an object is instantiated
Each constructor in a class has the same name, but its
formal parameters (if any) must be different from any
other constructor in the class
A constructor does not have a data type, because it
cannot return a value
You can overload the methods in a class
An Introduction to Programming with C++, Seventh Edition

37

Lab 15-1: Stop and Analyze


Study the code in Figure 15-16 and then answer the
questions

An Introduction to Programming with C++, Seventh Edition

38

Lab 15-2: Plan and Create

Figure 15-17 Problem specification for Lab 15-2

An Introduction to Programming with C++, Seventh Edition

39

Lab 15-3: Modify


Make a copy of Lab 15-2 to modify
Follow the modification instruction on pages 660-661
Test the program appropriately

An Introduction to Programming with C++, Seventh Edition

40

Lab 15-4: Desk-Check


Desk-check the code in Figure 15-24
What will the code display on the screen?

An Introduction to Programming with C++, Seventh Edition

41

Lab 15-5: Debug


Run the program in the Lab15-5.cpp file
Debug the program

An Introduction to Programming with C++, Seventh Edition

42

S-ar putea să vă placă și