Encapsulation Encapsulationis an Object-Oriented Programming concept that combines the data and functionsinside a class to manipulate the data to secure data from other methods thuslimits (Data Encapsulation in C++ 2017) interference and misuse from theoutside. The specific attributes of the class are kept private to preventunwanted 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 (Definitionof Encapsulation in Computer Programming 2017) Public, Protected and Privateand additionally internal and protected internal for C#. The data that has beenencapsulated cannot be used publicly but with the use of accessors (retrievesdata) and mutators (modifies data), private data can be manipulated. A simple everyday example of encapsulation isthe tv remote. The remote is used tocarry out various functions including volume, channels, menus and otherinterface setting but without the user having any knowledge of the innerworking, which includes chips, circuits and other components.
The advantage ofthis is that it allows excess to a level without revealing the complexitywithin thus preventing unwanted access by the client, therefore reducingprobability of human errors even by the programmer. Figure 1:Encapsulation of variable in case study Infigure1 the variable userWeight is made private thus only available to thecurrent class. This variable can be accessed and manipulated only using thefunctions get() and set() which are present inside the class. Figure 2.1: Parametrized Constructor ConstructorsC++ has a predefined mechanism, forinitializing an object when it is made, by using built in constructors becausenormal initialization (C++ Constructors and Destructors (2017) does not work for a class because theclass members have their associated access specifiers. They might not beavailable to the outside world (outside their class).
Constructors are member function of a class thatautomatically perform the initialization of object of a class, when an objectis created of that class with the same name as the name the class (Constructorsand Destructors in C++ with Examples (2014). Theconstructor defined constructors could not return any value not even void.The class can have multiple constructors defined, which is known as constructoroverloading, but they should differ in number ofparameters, sequence and type of signatures. Figure 2:Default Constructor Figure 2.
2: Copy ConstructorIn figure2,2.1,2.2 different types of constructor are implementeddepending on the purpose of the software.
The default constructor initializesthe different variable as does the parameterized but using parameters and thecopy constructor constructs a newobject out of an existing one, copying all its elements into itself.Destructors Figure 3: Destructors out of scope Constructorsare used to initialize objects and Destructors are used to destroy theseobjects. Similarly destructor is a member function of a class with the samename but(C++ Constructors and Destructors) preceded by a tilde ( ~ ) sign. For example, the destructor for theclass timeSpent will bear the name ~ timeSpent (). A destructor takes noarguments and has no return type that can be assigned. A (C++ Constructors and Destructors(2017)) local object is local to ablock, is destroyed when the block gets over and a global or static object isdestroyed when the program terminates thus cleans up the storage that is nolonger accessible (Destructors (C++ only).
When the destructor is called, or the class is out of scope theobject is destroyed as shown in figure 3. InheritanceIn C++ Inheritance can be implemented toinherit properties of objects of one class by objects of another class. Theprocess will inherit properties from a Base (Parent, Supper) class to sub (derived,child) class. The sub class inherits all thefeatures including data and functions members from the base class and possessunique additional features and allows for the manipulation (C++ Inheritance.Online) of existing member functions or data byoverriding base class member functions or data in the newly derived class. Inheritance (Alex. Basic inheritance in C++. 2008) promotes the ideas of code reusabilitytherefore leading to faster development times with easier maintenance of thecode.
Furthermore, if we update or modify the parent class, the new propertieswill be inherited by the child class. SHAPE Triangle Equilateral Rectangle isosceles In figure 4 the triangle is both a child toshape and parent to equilateral and isosceles. Therefore, the triangle willinherit the properties of Shape and equilateral, isosceles will inherittriangles properties. Figure 4: Inheritance Figure 4.1: Inheritance Base class(Shapes) Figure 4.2: Inheritance sub class(rectangle) The code snippets in figure4.1 and 4.2 demonstrate inheritance as subclass rectangle uses the propertiesof base class(shapes) to calculate the area.
Fitness tracker will have abase class of activities with objects that will be inherited by the subclasses(running, cycling,swimming) . An example of properties that maybeinherited are calorieBurnt or timeTaken.Polymorphism The term polymorphic means (Timothy Budd.(1991) “many forms”(poly=many, morphos=form). Polymorphism occurs when there is a hierarchy of theclasses and they are related by inheritance. Polymorphism allows us to use a common interface to performdifferent tasks will cause a different function to be executed depending on thetype of object that invokes the function.
A person can have differentcharacteristic at the same time. Theperson is friend, brother, and a husband therefore, the behaviour of the personwill be different depending on the current situation. A virtual function (faribasiddiq.Encapsulation, Inheritance and Polymorphism In C++. 2014.) can be defined as the member functionwithin a base class which you expect to redefine in derived classes. Eachderived class can redefine the virtual function for its own purpose.
For example the codecontains a simple base class activities, the class contains virtual functionslike calorieBurnt().Different types of activities like running, cycling. etc may have their own implementations of thevirtual 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 AdvantagesObject-oriented programming is modular, as programs are separated intofunctional blocks called objects. Objects (Robert Half. 4 Advantages ofObject-Oriented Programming) can also be reused within an across applicationsto make programming more productivity compared to traditional procedural baselanguages. For example, we have several objects like Activities, running and cycling.
The objects will be built differently butinclude similar properties between them. The running and the cycling objects aredifferent kind of activities thus inheritance can be implemented to save time.As the base class(activities) will allow sub classes (running, cycling) toinherit generic traits. Furthermore, any modification in the activity classwill be inherited by the sub classes thus the code is up to date in thedifferent classes.
Modularity of OOP can simplify the process of testing andtroubleshooting due to encapsulation. As Objects are self-contained thusindividual objects are only effect by themselves without having any directimpact on other object. Therefore, when a problem is detected in theapplication, we can pinpoint the object to modify rather then searching thewhole code unlike procedural technique. The problem might occur in the activityclass thus only modification is needed within the class but also will updatethe code for any sub classes.With procedural techniques writing software becomes morechallenging as the complexity increases with higher chance of errors and morecomplex code then necessary (reducing readability).
With object orientedprogramming complex software can be divided into smaller manageable collectionof objects to be tackled individual to be solved. This technique improvesvarious processes of writing code for software including writing actual codeinto smaller block, testing the code within self-contained object andmaintaining the code in long run.DisadvantagesThe process involved in learning and applying object-orientedprogramming to software application may not be a natural thought process formost developers. The complexity of implementing software based on interactionof objects requires deep understanding and practice of the concepts such asinheritance, polymorphism and encapsulation. The concept requires the developerto be skilled to effectively(Martin Wirsing, Simonetta Balsamo(2002) apply themto different software’s may improve the experience of the developer whilewriting the code but typically is much harder to read and understand due to theclass hierarchy.
Understanding of specific object instantiates and relationshipwith other object is required to understand the code, which is furthercomplexed using polymorphism. References1) DataEncapsulation in C++. Online.Last accessed 05/November 2017 at: https://www.tutorialspoint.com 2) Definition of Encapsulation in ComputerProgramming(2017).
Online.Last accessed 04/November/ 2017 at: https://www.thoughtco.com/definition-of-encapsulation- 3) Constructors and Destructors in C++ withExamples(2014).
Online. Last accessed 05/November 2017 at: http://www.hellgeeks.com/constructors-and-destructors/4) C++Constructors and Destructors . Online.
Last accessed 13/December 2017 at: https://codescracker.com/cpp/cpp-constructors-destructors.htm5) Destructors(C++ only). Online. Last accessed 13/December 2017 at: https://www.ibm.com6) C++Inheritance.
Online. Last accessed 13/December 2017 at: https://www.ibm.
com7) Alex.Basicinheritance in C++. 2008. Last accessed 13/December 2017 at:8) http://www.learncpp.
com/cpp-tutorial/112-basic-inheritance-in-c/9) RobertHalf. 4 Advantages of Object-Oriented Programming. 2017. Last accessed13/December 2017 at: https://www.
roberthalf.com10)Martin Wirsing, Alexander Knapp, SimonettaBalsamo(2002). Radical Innovations of Software and Systems Engineering in theFuture. online. Oxford, Butterworth-Heinemann. Book from Google book lastaccessed 13 December 2017 at: https://books.
google.co.uk11)Timothy Budd.(1991). An introduction toObject-oriented performance.
Reading, Addison-Wesley publishing company 12)faribasiddiq. Encapsulation, Inheritance andPolymorphism In C++. 2014. Last accessed 13/December 2017 at:https://www.go4expert.com