By Wayne Cheng
The foundation of C++: Classes
It is the name given to a specific paradigm in the field of
With objects we are creating pieces of code that can stand
You know the box has inputs and outputs but you don’t know
what is inside it.
The goal is to bind data and the functions that operate on that
data together in one “object”.
The functions will be our interface while the data will be
hidden in the black box.
Classes in C++ bind data and functions together in objects.
The goal is to force users to interface with the object’s
functions rather than accessing data directly.
Data is in the box.
The user can not access the data, he/she must use the defined
interface functions to manipulate the data.
Public: elements of class are accessible from outside of class.
Private: restricts access to these functions and data members to
this class alone. (as default)
Protected: restricts access to these functions and data member
to this class and its children. (Children are classes derived
from the current class)
The goal is to prevent the user from seeing how our functions
are implemented and exactly what they do.
All the user will see are function prototypes, but no actual
Multiple file projects: by storing each class implementation in
its own file, we can compile and distribute them without
allowing users to see the internals of functions.
The syntax for a class definition is
It is a mechanism that allows us to include “Parent” class into
a “Child” class.
It just another name for the topic of derived classes.
The goal is code reusability and extensibility.
With it we can reuse code without having to cut and paste it
out of our old programs.
It is a major stepping stone on the path to polymorphism.
The goal is to write generic code that can handle multiple
objects in the same way.
In code, we can pass multiple objects all as the same base
It is refers to the ability to associate multiple meaning to one
function name by means of a special mechanism known as
We use class hierarchies to allow us to treat child classes as
their parent class or grandparent, etc.
A collection of related data and functions bound together.
A function of an object.
A call to an object method. The term “message passing”
in reference to OOP you are dealing with method calls.
A method or variable that is part of an object (or class).
an object using another object that exists outside of
the first. (Must pass by address or reference)
An object that contains another object.
An object that publicly inherits its parent.
variable of data type (class or struct).
The class is how we are able to implement OOP concepts in
The class by itself gives us three of our five tenets OOP such
are Encapsulation, Data Abstraction and Information Hiding.
It is a technique used in binding functions to the data that they
It allows us to protect the data from the users direct
manipulation. (Data Abstraction)
It allows us to hide the implementation of functions from the
user. (Information Hiding)
It defines all its members to be private by default.
A class is a data type whose variables are objects
The definition of a class includes
Description of the kinds of values of the member variables
Description of the member functions
A class description is somewhat like a structure definition plus
the member variables
Examine a stack class: two functions and two pieces of data.
“Push”: it will have one parameter and will return a Boolean
success or failure. The data passed in
will be placed at the into the stack
at the top.
“Pop”: it will have one parameter, a reference, and will return
a Boolean value
indicating success or failure. The value
of the reference will be set to the
value at the top of the
The first element of data will be the stack itself.
The second element of data will be called top and will mark the current position
in the stack.
// This is an integer stack class.
//the class constructors
//the push function prototype
//the pop function prototype
//the definition of calss variables
//Import class specification
int IntStack::push(int x)
//The push function implementation
if(top == 9)
stack[top] = x;
int IntStack::pop(int & x)
//The pop function implementation
x = stack[top];
void main ( )
x = 0; x < 12; x++)
cout << “Pushing “ << x << “…”;
cout << (stack.push(x)?”Success” : ”Fail”) << endl;
for (x = 0; x < 12; x++)
cout << “Poping…”;
(stack.pop(back)? (cout << back)
: (cout << “Fail”)) << endl; }