Chapter 13:
Introduction to
Classes
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
13.1
Procedural and Object-Oriented
Programming
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Procedural and Object-Oriented
Programming
Procedural programming focuses on the
process/actions that occur in a program
Object-Oriented programming is based on the data
and the functions that operate on it. Objects are
instances of ADTs that represent the data and its
functions.
For example, a procedural program for a university will have
functions to take courses names , take names of students in
each year, their grades, …etc. The same program in OOP will
have students, staff, courses, functions to be performed on a
course could be setting course name, max student registrations,
add course assessments, and so on.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Limitations of Procedural
Programming
If the data structures change, many
functions must also be changed
Programs that are based on complex
function hierarchies are:
difficult to understand and maintain
difficult to modify and extend
easy to break
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Object-Oriented Programming
Terminology
class: like a struct (allows bundling of
related variables), but variables and
functions in the class can have different
properties than in a struct
object: an instance/variable of a class, in
the same way that a variable can be an
instance of a struct
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Classes and Objects
A Class is like a blueprint (template) and
objects are like houses built from the
blueprint
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Object-Oriented Programming
Terminology
attributes: members of a class
Also called properties/data
Differs from object to another
methods or behaviors: member functions
of a class
Also called actions/procedures
Same functions shared among all
objects
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
More on Objects
Data Hiding: restricting access to certain
members of an object.
Public Interface: members of an object that are
available outside of the object. This allows the
object to provide access to some data and
functions without sharing its internal details and
design, and provides some protection from data
corruption.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
13.2
Introduction to Classes
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Introduction to Classes
Objects/Instances are created from a
class
Format:
class ClassName
{
declaration;
declaration;
};
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Class Example
Rectangle r1, rt2, rct3, rctngle4, rectangle;
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Access Specifiers
Used to control access to members (attributes
and/or functions) of the class
public: attributes/functions can be accessed
by functions outside of the class
private: attributes/functions can only be
called by or accessed by functions that are
members of the class
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Class Example
Private Members
Public Members
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
More on Access Specifiers
Can be listed in any order in a class
Can appear multiple times in a class
If not specified, the default is private
VERY COMMON ERROR AT THE BEGINNING
OF OOP, IN ADDITION TO FORGETTING
THE ; AFTER THE CLASS
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Using const With Member Functions
const appearing after the parentheses in
a member function declaration specifies
that the function will not change any data
in the calling object.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Defining a Member Function
When defining a member function:
Put a prototype in class declaration
Define function using class name and scope
resolution operator (::)
The setWidth function belongs to
which class???
void Rectangle::setWidth(double w)
{
width = w;
}
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Accessors and Mutators
Mutator:
A member function that stores a value in a private
member variable, or changes its value in some way
Also named setter
Accessor:
A function that retrieves a value from a private
member variable.
Accessors do not change an object's data, so they
should be marked const.
Also called getter
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Class Example
Private Members
setters
getters Public Members
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
13.3
Defining an Instance of a Class
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Defining an Instance of a Class
• An object is an instance of a class
• Defined like struct variables:
Rectangle r;
• Access members using dot operator:
r.setWidth(5.2);
cout << r.getWidth();
• Compiler error if attempt to access
private member using dot operator
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Example
The Rectangle class
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Program 13-1 (Continued)
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Program 13-1 (Continued)
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Program 13-1 (Continued)
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Avoiding Stale Data
Some data is the result of a calculation.
In the Rectangle class the area of a rectangle is
calculated.
length * width
If we were to use an area variable here in the
Rectangle class, its value would be dependent on the
length and the width.
If we change length or width without updating area,
then area would become stale.
To avoid stale data, it is best to calculate the value of
that data within a member function rather than store it in
a variable.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Pointer to an Object
Can define a pointer to an object:
Rectangle *rPtr = 0;
Can access public members via pointer:
rPtr = &otherRectangle;
rPtr->setLength(12.5);
cout << rPtr->getLength() << endl;
Do you know another equivalent form???
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Dynamically Allocating an
Object
We can also use a pointer to dynamically
allocate an object.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
13.4
Why Have Private Members?
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Why Have Private Members?
Making data members private provides
data protection
Data can be accessed only through public
functions
public functions can check for invalid data
before setting the private members.
Public functions define the class’s public
interface
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Code outside the class must use the class's
public member functions to interact with the
object.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Write a Circle class
Circle class has radius as a private data
member and 2 public methods getArea
and getPerimeter.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.