Chapter 9 Introduction of Object Oriented Programming: Lecturer: Mrs Rohani Hassan
Chapter 9 Introduction of Object Oriented Programming: Lecturer: Mrs Rohani Hassan
Oriented Programming
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
1
Objectives
To understand objects and classes, and use classes to model objects (§9.2).
To use UML graphical notations to describe classes and objects (§9.2).
To understand the role of constructors when creating objects (§9.3).
To learn how to declare a class and how to create an object of a class (§9.4).
To know how to separate a class declaration from a class implementation (§9.5).
To access object members using pointers (§9.6).
To create objects using the new operator on the heap (§9.7).
To declare private data fields with appropriate get and set functions for data field
encapsulation to make classes easy to maintain (§9.9).
To understand the scope of data fields (§9.10).
To reference hidden data field using the this pointer (§9.11).
To develop functions with object arguments (§9.12).
To store and process objects in arrays (§9.13).
To apply class abstraction to develop software (§§9.14-9.15).
To initialize data fields with a constructor initializer (§9.16).
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
2
OO Programming Concepts
Object-oriented programming (OOP) involves
programming using objects.
An object represents an entity in the real world that can be
distinctly identified. For example, a student, a desk, a
circle, a button, and even a loan can all be viewed as
objects.
An object has a unique identity, state, and behaviors.
• The state of an object consists of a set of data fields (also
known as properties) with their current values.
• The behavior of an object is defined by a set of functions.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
3
Objects
Class Name: Circle A class template
Data Fields:
radius is _______
Functions:
getArea
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
4
Classes
Classes are constructs that define objects of the same type.
A class uses variables to define data fields and functions to
define behaviors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
5
Classes
class Circle
{
public:
// The radius of this circle
double radius; Data field
// Construct a circle object
Circle()
{
radius = 1;
} Constructors
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
6
UML Class Diagram
UML Class Diagram Circle Class name
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
7
Constructors
The constructor has exactly the same name as the defining class. Like
regular functions, constructors can be overloaded (i.e., multiple
constructors with the same name but different signatures), making it
easy to construct objects with different initial data values.
A class normally provides a constructor without arguments (e.g.,
Circle()). Such constructor is called a no-arg or no-argument
constructor.
A class may be declared without constructors. In this case, a no-arg
constructor with an empty body is implicitly declared in the class.
This constructor, called a default constructor, is provided
automatically only if no constructors are explicitly declared in the
class.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
8
Constructors, cont.
A constructor with no parameters is referred to as
a no-arg constructor.
• Constructors must have the same name as the
class itself.
• Constructors do not have a return type—not
even void.
• Constructors play the role of initializing
objects.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
9
Object Names
In C++, you can assign a name when creating an
object. A constructor is invoked when an object is
created. The syntax to create an object using the
no-arg constructor is:
ClassName objectName;
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
10
Constructing with Arguments
The syntax to declare an object using a constructor with
arguments is
ClassName objectName(arguments);
Circle circle2(5.5);
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
11
Access Operator
After an object is created, its data can be accessed
and its functions invoked using the dot operator (.),
also known as the object member access operator:
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
12
A Simple Circle Class
TestCircle Run
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
13
Program Name: TestCircle.cpp
#include <iostream> int main()
class Circle {
{ Circle circle1(1.0);
public: Circle circle2(25.0);
double radius; // The radius of this circle
Circle() //Construct a circle object cout << "The area of the circle of radius "
{ << circle1.radius << " is " << circle1.getArea()
radius = 1; << endl;
} cout << "The area of the circle of radius "
Circle(double newRadius) //Construct object << circle2.radius << " is " << circle2.getArea()
<< endl;
{
radius = newRadius;
// Modify circle radius
}
circle2.radius = 100;
// Return the area of this circle
cout << "The area of the circle of radius "
double getArea()
<< circle2.radius << " is " << circle2.getArea()
{ << endl;
return radius * radius * 3.14159;
} return 0;
}; }
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
14
Naming Objects and Classes
When you declare a custom class, capitalize the first
letter of each word in a class name;.
For example,
The class names Circle, Rectangle, and Desk.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
15
Class is a DataType
You can use primitive data types to define variables.
You can also use class names to declare object
names. In this sense, a class is also a data type.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
16
Memberwise Copy
In C++, you can also use the assignment operator = to
copy the contents from one object to the other. By default,
each data field of one object is copied to its counterpart in
the other object.
For example: circle2 = circle1;
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
17
Constant Object Name
Object names are like array names. Once an object name
is declared, it references to an object. It cannot be
reassigned to reference another object.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
18
Anonymous Object
Most of the time, you create a named object and later access the
members of the object through its name. Occasionally, you may
create an object and use it only once. In this case, you don’t have to
name the object. Such objects are called anonymous objects.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
19
Class Replaces struct
The C language has the struct type for representing
records. For example, you may define a struct type for
representing students as shown in (a).
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
20
Separating Declaration from
Implementation
C++ allows you to separate class declaration from implementation.
The class declaration describes the contract of the class and the class
implementation implements the contract. The class declaration
simply lists all the data fields, constructor prototypes, and the
function prototypes. The class implementation implements the
constructors and functions. The class declaration and
implementation are in two separate files. Both files should have the
same name, but with different extension names. The class
declaration file has an extension name .h and the class
implementation file has an extension name .cpp.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
22
Inline Declaration class A
Example {
public:
A()
For example, in the
{
following declaration for
// do something;
class A, the constructor and
}
function f1 are automatically
double f1()
inline functions, but
function f2 is a regular {
function. // return a number
}
double f2();
};
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
23
Inline Functions in Implementation File
There is another way to declare inline functions for
classes. You may declare inline functions in the class’s
implementation file. For example, to declare function f2
as an inline function, precede the inline keyword in the
function header as follows:
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
25
Accessing Object Members via Pointers
Object names cannot be changed once they are declared.
However, you can create pointers for object names and
assign new object names to pointers whenever necessary.
For example,
Circle circle1;
Circle *pCircle = &circle1;
cout << "The radius is " << (*pCircle).radius << endl;
cout << "The area is " << (*pCircle).getArea() << endl;
(*pCircle).radius = 5.5;
cout << "The radius is " << (*pCircle).radius << endl;
cout << "The area is " << (*pCircle).getArea() << endl;
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
26
Creating Dynamic Objects on Heap
When you declare a circle object in a function, it is created
in the stack. When the function returns, the object is
destroyed.
To retain the object, you may create it dynamically on the
heap using the new operator.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
27
The Scope of Variables
Chapter 5, “Functions,” discussed the scope of global variables
and local variables.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
28
Data Field Encapsulation
The data fields radius in the Circle class in Listing 9.1
can be modified directly (e.g., circle1.radius = 5).
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
29
Accessor and Mutator
Colloquially, a get function is referred to as a getter (or accessor),
and a set function is referred to as a setter (or mutator).
A get function has the following signature:
returnType getPropertyName()
bool isPropertyName()
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
30
Example: New Circle Class
Circle
The - sign indicates
private modifier -radius: double The radius of this circle (default: 1.0).
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
32
The Scope of Variables
Local variables are declared and used inside a function
locally.
If a local variable has the same name as a data field, the
local variable takes precedence and the data field with
the same name is hidden.
HideDataField Run
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
33
The this Pointer
Sometimes you need to reference a class’s hidden data field in a
function.
For example, a property name is often used as the parameter name
in a set function for the property. In this case, you need to
reference the hidden property name in the function in order to set a
new value to it.
A hidden data field can be accessed by using the this keyword,
which is a special built-in pointer that references to the calling
object. You can rewrite the Circle class implementation in Listing
9.9 using the this pointer.
Circle3
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
34
Passing Objects to Functions
You can pass objects by value or by reference.
c: Circle c is an alias for myCircle c: address of
myCircle
radius: 5.0
Copy myCircle to c
PassObjectByValue Run
PassObjectByReference Run
PassObjectToPointer Run
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
35
Array of Objects
Circle circleArray[3] = {Circle(3), Circle(4),
Circle(5)};
TotalArea Run
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
36
Class Abstraction and Encapsulation
Class abstraction means to separate class implementation
from the use of the class.
The creator of the class provides a description of the class
and let the user know how the class can be used.
The user of the class does not need to know how the class
is implemented. The detail of implementation is
encapsulated and hidden from the user.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
37
Example: The Loan Class
Loan
-annualInterestRate: double The annual interest rate of the loan (default: 2.5).
-numberOfYears: int The number of years for the loan (default: 1)
-loanAmount: double The loan amount (default: 1000).
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
39
The Role of Default Constructor
If a data field is an object type, the default constructor is
automatically invoked to construct an object for the data
field.
If a default constructor does not exist, a compilation error
will be reported.
For example, the code in Listing 9.17 has an error, because
the time data field (line 24) in the Action class is of the
Time class that does not have a default constructor.
DefaultConstructor1 NoDefaultConstructor2
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
40