Classes
Classes
Object-Oriented Programming
Using C++
Chapter 4
Using Classes
Objectives
Create classes
Learn about encapsulating class components
Implement class functions
Use private functions and public data
Use the scope resolution operator with class
fields and functions
Use static class members
Learn about the this pointer
Understand the advantages of polymorphism
2
Creating Classes
A class is a category of objects; it is a new data
type
Classes provide a description of an object
Classes provide a convenient way to group related
data and the functions that use the data
When you create an object from the class, you
automatically create all the related fields
You think about them and manipulate them as real-life
classes and objects
Abstract data type (ADT): a type that you
define
3
Creating Classes (continued)
Student aSophomore;
aSophomore.idNum = 7645; Error! By default, all members of a
cout<<aSophomore.idNum; class are private
4
Creating Classes (continued)
Access modifier
5
Encapsulating Class Components
6
Designing Classes
If you need a class for students, you should ask:
What shall we call it?
What are its attributes?
What methods are needed by Student?
Any other methods?
In most cases, you declare both fields and
functions
You declare a field using a data type and an identifier
You declare a function by writing its prototype, which
serves as the interface to the function
7
Designing Classes
9
Implementing Class Functions (continued)
10
Using Public Functions to Alter Private Data
11
Using Public Functions to Alter Private Data (continued)
12
Using Private Functions and Public Data
13
14
Considering Scope when Defining Member Functions
15
Considering Scope when Defining Member Functions
(continued)
16
Using Static Class Members
When a class field is static, only one memory location is
allocated
All members of the class share a single storage location for a
static data member of that same class
18
Defining Static Data Members (continued)
20
Using Static Functions (continued)
21
Understanding the this Pointer
…
…
…
22
Understanding the this Pointer (continued)
23
Understanding the this Pointer (continued)
24
Using the this Pointer Explicitly
25
Using the Pointer-to-Member Operator
26
Understanding Polymorphism
Polymorphism is the object-oriented
program feature that allows the same
operation to be carried out differently
depending on the object
For example,
clerk.displayValues();
shirt.displayValues();
XYZCompany.displayValues();
27
Understanding Polymorphism (continued)
28
You Do It: Creating and Using a Class
class CollegeCourse
{
private:
string department;
int courseNum;
int seats;
public:
void setDepartmentAndCourse(string, int);
void setSeats(int);
void displayCourseData();
};
29
void College::setDepartmentAndCourse(string dept, int
num)
{
}
void College::setSeats(int seats)
{
}
void College::displayCourseData()
{
}
30
Using a static Field
class Letter
{
private:
string title;
string recipient;
static int count;
public:
void setRecipient(string, string);
void displayGreeting();
static void displayCount();
};
31
Understanding How static and Non-static Fields are
Stored
32
Summary
A class is a category of objects
When you create a class, you hide, or encapsulate, the
individual components
When you construct a class, you create the declaration
section and the implementation section
When you create a class, usually you want to make data
items private, and to make functions public
The scope resolution operator (::) identifies a member
function as being in scope within a class
33
Summary (continued)
Each class object gets its own block of memory for its data
members
You can access a static, class-wide field using a static
function
One copy of each class member function is stored no matter
how many objects exist
Within any member function, you can explicitly use the this
pointer to access the object’s data fields
Polymorphism allows the same operation to be carried out
differently depending on the object
34