is an Object-Oriented Programming concept that combines the data and functions
inside a class to manipulate the data to secure data from other methods thus
limits (Data Encapsulation in C++ 2017) interference and misuse from the
outside. The specific attributes of the class are kept private to prevent
unwanted access therefore protecting or hiding sensitive information. By default,
the attributes are private in a class but can be changed to a different level(scope)
of access. There are three main levels of access in C# and C++. These are (Definition
of Encapsulation in Computer Programming 2017) Public, Protected and Private
and additionally internal and protected internal for C#. The data that has been
encapsulated cannot be used publicly but with the use of accessors (retrieves
data) and mutators (modifies data), private data can be manipulated.
A simple everyday example of encapsulation is
the tv remote. The remote is used to
carry out various functions including volume, channels, menus and other
interface setting but without the user having any knowledge of the inner
working, which includes chips, circuits and other components. The advantage of
this is that it allows excess to a level without revealing the complexity
within thus preventing unwanted access by the client, therefore reducing
probability of human errors even by the programmer.
Figure 1:Encapsulation of variable
in case study
figure1 the variable userWeight is made private thus only available to the
current class. This variable can be accessed and manipulated only using the
functions get() and set() which are present inside the class.
Figure 2.1: Parametrized Constructor
C++ has a predefined mechanism, for
initializing an object when it is made, by using built in constructors because
normal initialization (C++ Constructors and Destructors (2017) does not work for a class because the
class members have their associated access specifiers. They might not be
available to the outside world (outside their class). Constructors are member function of a class that
automatically perform the initialization of object of a class, when an object
is created of that class with the same name as the name the class (Constructors
and Destructors in C++ with Examples (2014). The
constructor defined constructors could not return any value not even void.
The class can have multiple constructors defined, which is known as constructor
overloading, but they should differ in number of
parameters, sequence and type of signatures.
Figure 2:Default Constructor
Figure 2.2: Copy Constructor
In figure2,2.1,2.2 different types of constructor are implemented
depending on the purpose of the software. The default constructor initializes
the different variable as does the parameterized but using parameters and the
copy constructor constructs a new
object out of an existing one, copying all its elements into itself.
Figure 3: Destructors out of scope
are used to initialize objects and Destructors are used to destroy these
objects. Similarly destructor is a member function of a class with the same
name but(C++ Constructors and Destructors) preceded by a tilde ( ~ ) sign. For example, the destructor for the
class timeSpent will bear the name ~ timeSpent (). A destructor takes no
arguments and has no return type that can be assigned. A (C++ Constructors and Destructors
(2017)) local object is local to a
block, is destroyed when the block gets over and a global or static object is
destroyed when the program terminates thus cleans up the storage that is no
longer accessible (Destructors (C++ only).
When the destructor is called, or the class is out of scope the
object is destroyed as shown in figure 3.
In C++ Inheritance can be implemented to
inherit properties of objects of one class by objects of another class. The
process will inherit properties from a Base (Parent, Supper) class to sub (derived,
child) class. The sub class inherits all the
features including data and functions members from the base class and possess
unique additional features and allows for the manipulation (C++ Inheritance.
Online) of existing member functions or data by
overriding base class member functions or data in the newly derived class.
Inheritance (Alex. Basic inheritance in C++. 2008) promotes the ideas of code reusability
therefore leading to faster development times with easier maintenance of the
code. Furthermore, if we update or modify the parent class, the new properties
will be inherited by the child class.
In figure 4 the triangle is both a child to
shape and parent to equilateral and isosceles. Therefore, the triangle will
inherit the properties of Shape and equilateral, isosceles will inherit
Figure 4: Inheritance
Figure 4.1: Inheritance Base class(Shapes)
Figure 4.2: Inheritance sub class(rectangle)
The code snippets in figure
4.1 and 4.2 demonstrate inheritance as subclass rectangle uses the properties
of base class(shapes) to calculate the area.
Fitness tracker will have a
base class of activities with objects that will be inherited by the sub
classes(running, cycling,swimming) . An example of properties that maybe
inherited are calorieBurnt or timeTaken.
The term polymorphic means (Timothy Budd.(1991) “many forms”
(poly=many, morphos=form). Polymorphism occurs when there is a hierarchy of the
classes and they are related by inheritance. Polymorphism allows us to use a common interface to perform
different tasks will cause a different function to be executed depending on the
type of object that invokes the function.
A person can have different
characteristic at the same time. The
person is friend, brother, and a husband therefore, the behaviour of the person
will be different depending on the current situation. A virtual function (faribasiddiq.
Encapsulation, Inheritance and Polymorphism In C++. 2014.) can be defined as the member function
within a base class which you expect to redefine in derived classes. Each
derived class can redefine the virtual function for its own purpose.
For example the code
contains a simple base class activities, the class contains virtual functions
Different types of activities like running, cycling. etc may have their own implementations of the
virtual functions present in base class activities because of the difference in the formula.
This demonstrated in the figures 5.1 and 5.2.
Figure 5: Polymorphism in fitness tracker
Figure 5.1Polymophism of a virtual function
Object-oriented programming is modular, as programs are separated into
functional blocks called objects. Objects (Robert Half. 4 Advantages of
Object-Oriented Programming) can also be reused within an across applications
to make programming more productivity compared to traditional procedural base
languages. For example, we have several objects like Activities, running and cycling. The objects will be built differently but
include similar properties between them. The running and the cycling objects are
different kind of activities thus inheritance can be implemented to save time.
As the base class(activities) will allow sub classes (running, cycling) to
inherit generic traits. Furthermore, any modification in the activity class
will be inherited by the sub classes thus the code is up to date in the
Modularity of OOP can simplify the process of testing and
troubleshooting due to encapsulation. As Objects are self-contained thus
individual objects are only effect by themselves without having any direct
impact on other object. Therefore, when a problem is detected in the
application, we can pinpoint the object to modify rather then searching the
whole code unlike procedural technique. The problem might occur in the activity
class thus only modification is needed within the class but also will update
the code for any sub classes.
With procedural techniques writing software becomes more
challenging as the complexity increases with higher chance of errors and more
complex code then necessary (reducing readability). With object oriented
programming complex software can be divided into smaller manageable collection
of objects to be tackled individual to be solved. This technique improves
various processes of writing code for software including writing actual code
into smaller block, testing the code within self-contained object and
maintaining the code in long run.
The process involved in learning and applying object-oriented
programming to software application may not be a natural thought process for
most developers. The complexity of implementing software based on interaction
of objects requires deep understanding and practice of the concepts such as
inheritance, polymorphism and encapsulation. The concept requires the developer
to be skilled to effectively(Martin Wirsing, Simonetta Balsamo(2002) apply them
to different software’s may improve the experience of the developer while
writing the code but typically is much harder to read and understand due to the
class hierarchy. Understanding of specific object instantiates and relationship
with other object is required to understand the code, which is further
complexed using polymorphism.
Encapsulation in C++. Online.Last accessed 05/November 2017 at: https://www.tutorialspoint.com
Definition of Encapsulation in Computer
Programming(2017).Online.Last accessed 04/November/ 2017 at: https://www.thoughtco.com/definition-of-encapsulation-
Constructors and Destructors in C++ with
Examples(2014). Online. Last accessed 05/November 2017 at: http://www.hellgeeks.com/constructors-and-destructors/
Constructors and Destructors . Online. Last accessed 13/December 2017 at: https://codescracker.com/cpp/cpp-constructors-destructors.htm
(C++ only). Online. Last accessed 13/December 2017 at: https://www.ibm.com
Inheritance.Online. Last accessed 13/December 2017 at: https://www.ibm.com
inheritance in C++. 2008. Last accessed 13/December 2017 at:
Half. 4 Advantages of Object-Oriented Programming. 2017. Last accessed
13/December 2017 at: https://www.roberthalf.com
Martin Wirsing, Alexander Knapp, Simonetta
Balsamo(2002). Radical Innovations of Software and Systems Engineering in the
Future. online. Oxford, Butterworth-Heinemann. Book from Google book last
accessed 13 December 2017 at: https://books.google.co.uk
Timothy Budd.(1991). An introduction to
Object-oriented performance. Reading, Addison-Wesley publishing company
faribasiddiq. Encapsulation, Inheritance and
Polymorphism In C++. 2014. Last accessed 13/December 2017 at: