Object-Oriented Programming 2
Object-Oriented Programming 2
Object-Oriented Programming 2
1
Object-Oriented Programming 2
So, what are we waiting for? Let us continue our exploration of the world of
Computer Programming.
Course Module
Recall
Since the last module, we have talked about the world of Object-Oriented
Programming. We covered the essentials of Object-Oriented Programming
which enabled you to write OOP Applications in C++. However, the journey in
the world of OOP does not stop there. There are four (4) principles behind
OOP that you need to learn and how it applies in your current knowledge.
These principles form the core of Object-Oriented Programming. They serve
as the heart of OOP such that without them, OOP is just a simple non-
deterministic programming paradigm.
Encapsulation
This principle is initially enabled using scopes. Remember that there are
three (3) scopes in C++, namely:
(a) Public
(b) Private
(c) Protected
These scopes limit the visibility of the properties and methods from outside
of the class and outside the inheriting class.
Accessors
An accessor is a method within the class that enables the retrieval of values
hidden by scopes. For example, if we hid the property int myID under the
private scope, we cannot access that property from outside of the class. To
let the user, for example, retrieve the value stored in the int myID property,
we can use an accessor.
Lastly, accessors are prefixed with “get” in their function name to indicate
that those methods are indeed accessors. For example, if we are to write an
accessor for our int myID property, its equivalent accessor should be int
getMyID() method.
Implementing Accessors
Figure 1 shows a variant of our box.h header file which has private
properties. In this example, we moved all the properties to private scope.
Because of this, we cannot anymore call any of the properties (e.g. int
length) in our int main() function. To provide read-only access to our
private properties, we implemented accessors.
Figure 2 features a snippet of our box.h header file. In this snippet, we have
added a new function under our public scope, the int getLength() method.
Notice that our int getLength() method:
(a) Has a return type of int similar to the data type of Length property,
Course Module
(b) Does not have any parameter, and
(c) Is prefixed with “get” in the function name.
This so far complies with the characteristics of accessors. Now, let us look at
the implementation of our int getLength() method.
Mutators
Now that we have a method to retrieve values from our hidden properties,
we also have a way to assign values to our hidden properties through
mutators. A mutator is a method within the class that enables the assignment
of values to our properties hidden by scopes. For example, if we hid the
property string Color under the private scope, we cannot anymore assign
values to it directly. In this case, we will be using an intermediary which will
assign values to it – the mutator.
Like our accessors, mutators are bound to several characteristics which help
define their purpose. These characteristics must be keenly observed. These
characteristics are:
(a) Mutators have one parameter.
(b) Mutators may or may not return the final assigned value.
(c) Mutators are prefixed with “set” in their function name.
The main purpose of our mutator is to store values to our hidden properties,
and that only.Therefore, we set only one parameter – the processed or
unprocessed value we will assign to the property. Processed values are values
that have been through several operations which changed the original value
to the current value. Unprocessed values are values which have yet to be
modified.
need to convert the value to int. The converted value then becomes our
processed value.
Converting a value from one data type to a different data type is called
“casting”.
In addition, the mutator may or may not return the assigned value. This
solely depends on the developer’s discretion. However, it is advised to return
the value if and only if there were some operations conducted before the
value is assigned to the property. In our previous example of a string value
to be assigned to an int variable, since there will be conversion of data type
about to take place, the resulting int value should be returned. If the value
will be assigned to the property directly without any conversion or
processing, then there is no need to return the value.
Implementing Mutators
Course Module
Figure 4. box.h Header File 2
In Figure 4, we look at a variable of the Box class that implements the idea of
both assessors and mutators. Notice we have added a new method void
setColor(string) which, based on the naming convention used, will assign
a value to our string Color property hidden under private scope. Let us
dissect this variant of our Box class.
Abstraction
Inheritance
An interface is a class in C++ which has methods that are virtual in nature.
Virtual means that the method is not implemented wheresoever inside of the
class. In simpler terms, there is no code blocks under those methods.
Course Module
Figure 6. IBox Interface
We have yet another version of our box example shown in Figure 7. In this
Box class, we have inherited the IBox interface we defined in Figure 6.
Since the IBox interface contains two (2) pure virtual methods int
getVolume() and string getColor(), we were required to implement the
two methods inside our Box class.
Course Module
Polymorphism
For example, we have the sum() function. This function shall accept two (2)
inputs in its parameter. Since we cannot force the use of the sum() function
to be limited to integers only, we need to polymorph the function.
In our sum() function example, we can create several variation of that same
function. If we need to add two integers, we can declare a int sum(int,
int) function. If we need to add two floats, we can declare a float
sum(float, float) function. And if we need to add three integers float
sum(float, float), we can declare int sum(int, int, int) and float
sum(float, float, float) function.
There are two (2) forms of polymorphism, the overriding and the
overloading.
Overriding
In this type of polymorphism, we have the functions call upon the existing
functions and overlap their statements with another. This is common in
inheriting classes wherein the parent class has an existing method and the
child class overrides that existing method to perform a different set of
functions.
For example, we have a parent IBox interface from Figure 6. In that figure,
the IBox interface has two (2) methods, string getColor() and int
getVolume(). Then in Figure 7, the Box class inherited the IBox interface. In
doing so, Box class also inherited the right to the two (2) methods of IBox
interface. Box class implemented the string getColor() and int
getCVolume() methods. In this case, the two functions of IBox was
overridden by the functions of Box.
Overloading
Glossary
Abstraction: A principle of Object-Oriented Programming which generalizes
the properties and methods of classes in such a way that the implementation
is hidden from outside the class.
Accessor: A method of a class that returns the value of hidden properties.
Casting: Converting a value of one data type to a different data type.
Encapsulation: A principle of Object-Oriented Programming which groups
the functionalities and statements of certain areas in a code block.
Inheritance: A principle of Object-Oriented Programming which extends the
features and functionalities of existing classes and interfaces.
Integrity Check: Avalidation conducted to verify the integrity of the values
of properties throughout the execution of the application.
Mutator: Amethod of a class that assigns value to hidden properties.
Overriding: Polymorphism that happens during compile time.
Overloading: Polymorphism that happens during runtime.
Polymorphism: A principle of Object-Oriented Programming which allows
multiple variations and versions of the same function sharing the same name.
Processed Value: A value that has undergone a series of operations and
modifications.
Pure Virtual [Method]: A virtual method that must be implemented in the
inheriting classes.
Unprocessed Value: A value that is yet to undergo operations and
modifications.
Virtual [Method]: A method which does not have a body.
Computer Programming 1
15
Object-Oriented Programming 2
Course Module