PLC Session 9

Session 9: Object Oriented Programming

 

A. Introduction to Object Oriented Programming

OOP (Object Oriented Programming) is a programming paradigm based on the concept of “object”, which can contain the data, in the form field, which is often called attributes, and code, in the form of procedure of the form, which is often called the method.

The object itself is a component of a program that knows how to perform certain actions and how it interacts with other elements of the program. The object is the basic unit of object-oriented programming. Suppose we have an object “person”. Logically, the “people” are definitely has a name as a property of the “people”. We can also make the object “person” to do something like, “walk” or “drive”. It is called the “method” of the “people”.

We can also interact an object with another object. As an example we want to create a program in which “people” drive “cars” from A to B. We can simply add new objects, the “car” and some methods that include how “people” to drive a car and what do the “car” when is being driven by people. When completed, connect both the object so that the object can work together – the same .It can occur due to code in OOP is set around the object.

Class is a blueprint of an object. The assumption class it as a concept, and the object is the embodiment of the concept. We must create a class before making an object. Say that we want to make a person in a program. We want to describe how the person looks and what they would do. A class called “people” will provide plan how “man” is seen and what is a “person” can do. To really use someone in your program, you need to create an object. You use the class to create an object of type “person”.

Class is very useful in programming. In the manufacture of the object, we can make more than one object in one class only. Manufacturing still needs a detailed declaration but the structure remains the same object.

Once we create an object, we want the object – the object to do something. This is where the method works. Method in OOP is a procedure related to the class. A method defines the behavior of objects created from the class. Method is also defined as an act that is the object capable of doing. The relationship between the method and the class is called binding. For example, an object of type ‘person,’ which made use of a class of people. Method associated with this class can consist of things such as walking and driving. Method is sometimes confused with the function, but they are different.

Function is a combination of instruction combined to achieve results. A function usually requires some input (called arguments) and return some results. For example, driving a car. To determine the distance, we need to perform calculations using the distance driven and the amount of fuel used. You can create a function to perform this calculation. The arguments passed into the function of the great distance and fuel consumption, and the result will be the mileage. Whenever we want to determine the distance, you can simply call a function to perform calculations.

What’s different between function and method? The function is independent and not associated with the class. We can use this function anywhere in our code, and we do not need to have an object for use.

Now, what if we are to associate the function with an object of type ‘car?’ For example, you want to show the mileage on the car dashboard. In this case, the calculation of mileage has been the method because it is a procedure associated with the car class. Every time we create a new object of type ‘car’ to use the car class, this method will become part of the object. Measures that can now be done is to calculate the distance. This is the same calculation as that of a stand-alone function, but is now tied to the car. Four core concepts of object oriented programming is abstraction, encapsulation, inheritance and polymorphism

B. ADT(Abstract Data Type)

Abstract data refers to the data, only provides important information to the outside world and hide the details of their background, that is, to represent the needed information in program without presenting detail. For example, the database system hides certain details of how data is stored, prepared and maintained. the same way, C ++ class provides different methods to the outside world without giving details about the internal methods and data.

C. Encapsulation

Encapsulation is an OOP concept that binds together the data and functions that manipulate data, and that makes them safe from outside interference and misuse. Data encapsulation led to important OOP concepts of hiding data.

Data encapsulation is a mechanism of bundling the data, and the functions that use them and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.

C++ supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. We already have studied that a class can contain private, protected and public members. By default, all items defined in a class are private.

 

Example of encapsulation :

#include <iostream>

using namespace std;

class Adder {

public:

// constructor

Adder(int i = 0) {

total = i;

}

// interface to outside world

void addNum(int number) {

total += number;

}

// interface to outside world

int getTotal() {

return total;

};

private:

// hidden data from outside world

int total;

};

int main( ) {

Adder a;

a.addNum(10);

a.addNum(20);

a.addNum(30);

cout << “Total ” << a.getTotal() <<endl;

return 0;

}

D. Inheritance

One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.

When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the baseclass, and the new class is referred to as the derived class.

The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.

A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes. To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form:

class derived-class: access-specifier base-class

 

Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default.

Example of inheritance (base and derived class) :

#include <iostream>

using namespace std;

// Base class

class Shape  {

public:

void setWidth(int w) {

width = w;

}

void setHeight(int h) {

height = h;

}

protected:

int width;

int height;

}

// Derived class

class Rectangle: public Shape {

public:

int getArea() {

return (width * height);

}

};

int main(void) {

Rectangle Rect;

Rect.setWidth(5);

Rect.setHeight(7);

// Print the area of the object.

cout << “Total area: ” << Rect.getArea() << endl;

return 0;

}

A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.

We can summarize the different access types according to who can access them in the following way:

Access public protected private
Same class yes yes yes
Derived classes yes yes no
Outside classes yes no no

A derived class inherits all base class methods with the following exceptions:

  • Constructors, destructors and copy constructors of the base class.
  • Overloaded operators of the base class.
  • The friend functions of the base class.

When deriving a class from a base class, the base class may be inherited throughpublic, protected or private inheritance. The type of inheritance is specified by the access-specifier as explained above.

We hardly use protected or private inheritance, but public inheritance is commonly used. While using different type of inheritance, following rules are applied:

  • Public Inheritance: When deriving a class from a public base class,public members of the base class become public members of the derived class and protected members of the base class becomeprotected members of the derived class. A base class’s privatemembers are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.
  • Protected Inheritance: When deriving from a protected base class,public and protected members of the base class become protectedmembers of the derived class.
  • Private Inheritance: When deriving from a private base class, publicand protected members of the base class become private members of the derived class.

E. Polymorphism

The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.

C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.

Consider the following example where a base class has been derived by other two classes:

#include <iostream>

using namespace std;

 

class Shape {

protected:

int width, height;

 

public:

Shape( int a = 0, int b = 0) {

width = a;

height = b;

}

 

int area() {

cout << “Parent class area :” <<endl;

return 0;

}

};

 

class Rectangle: public Shape {

public:

Rectangle( int a = 0, int b = 0):Shape(a, b) { }

int area () {

cout << “Rectangle class area :” <<endl;

return (width * height);

}

};

 

class Triangle: public Shape{

public:

Triangle( int a = 0, int b = 0):Shape(a, b) { }

int area () {

cout << “Triangle class area :” <<endl;

return (width * height / 2);

}

};

 

// Main function for the program

int main( ) {

Shape *shape;

Rectangle rec(10,7);

Triangle  tri(10,5);

 

// store the address of Rectangle

shape = &rec;

 

// call rectangle area.

shape->area();

 

// store the address of Triangle

shape = &tri;

 

// call triangle area.

shape->area();

 

return 0;

}

When the above code is compiled and executed, it produces the following result:

Parent class area

Parent class area

 

The reason for the incorrect output is that the call of the function area() is being set once by the compiler as the version defined in the base class. This is calledstatic resolution of the function call, or static linkage – the function call is fixed before the program is executed. This is also sometimes called early binding because the area() function is set during the compilation of the program.

But now, let’s make a slight modification in our program and precede the declaration of area() in the Shape class with the keyword virtual so that it looks like this:

class Shape {

protected:

int width, height;

public:

Shape( int a = 0, int b = 0) {

width = a;

height = b;

}

 

virtual int area() {

cout << “Parent class area :” <<endl;

return 0;

}

};

After this slight modification, when the previous example code is compiled and executed, it produces the following result:

Rectangle class area

Triangle class area

This time, the compiler looks at the contents of the pointer instead of it’s type. Hence, since addresses of objects of tri and rec classes are stored in *shape the respective area() function is called.

As you can see, each of the child classes has a separate implementation for the function area(). This is how polymorphism is generally used. You have different classes with a function of the same name, and even the same parameters, but with different implementations.

 

Source(s) :

https://en.wikipedia.org/wiki/Object-oriented_programming

http://study.com/academy/lesson/oop-object-oriented-programming-objects-classes-interfaces.html

https://www.tutorialspoint.com/cplusplus/cpp_object_oriented.htm

https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html

https://www.tutorialspoint.com/cplusplus/cpp_data_encapsulation.htm

https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm

Slide Binus Maya

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.