Core Concepts of OOP

Object Oriented Programming (OOP)

Introduction:

Introduction:

Structured programming can be roughly divided into two categories:

bullet

Procedural

bullet

Object Oriented

Structured programming is basically dividing a program into smaller modules that are easier to manage. All of what we have covered up to this point is part of procedural programming. In this the programmer divides the program into smaller functions (rather than putting the entire code within one function). Each function would have its own variables and sharing of variables between various functions is also possible (using global variables).

What we will see henceforth is related to object oriented programming (OOP). Large programs were difficult to handle in procedural programming and OOP was developed to reduce complexity of software development. In C++, OOP is implemented using classes. The main concepts in OOP are:

bullet

Data Abstraction

bullet

Data encapsulation (hiding)

bullet

Polymorphism

bullet

Inheritance

Data Abstraction:

The fundamental data types (like int, float, double, char etc.) might suffice for simple programs. It is difficult for a programmer to always think in terms of the fundamental data types alone. For example, what data type would you use if you want to model a car in a computer application? A car has many properties and each one will be of a different data type (the speed would be an integer, the colour would be a string and so on). We could use a structure to solve our problem but structures also have their limitations. When you create a structure, the user (another programmer who might use your structure or you yourself) can directly manipulate with the member data of the structure. Let’s say that we have created a structure called ‘car’ which has 2 data types for speed and color. You can create an instance of the structure car (say ferrari) and then assign the value for ‘speed’ as 99999. Or you could also set the colour as ‘xyzer’. This would lead to incorrect data and you wouldn’t want the user to enter such values. Thus a structure wouldn’t permit us to control the way data is stored or accessed. A user can simply modify anything by directly accessing it. Wouldn’t it be more convenient if we could have some control over our data type?

Whenever you create such a complex data type, there will also be some operations that you would want to perform on that data type. In C++, these operations (which are implemented through functions) and the data can be bound together as a class. A C++ class is a data type that the programmer defines. Data abstraction refers to the capability of a programmer to create new, user-defined data types. Classes are also called abstract data types (ADTs or user-defined data types) because they are created using the process of abstraction. So, what is the process of abstraction?

Let us consider a simple example of a person. Every person in this world has many attributes (like age, date of birth, name, gender, marital status, salary, number of children, hobbies, strengths, weaknesses, diseases, nationality etc.). If we are designing a patient database application for a hospital, we might not require storing attributes like salary, strengths and weaknesses of a person. So while designing this application we will only choose the particular features of the patient that we are interested in (like age, date of birth, nationality, disease history, gender etc.).

Now if we have to design another system for a corporation, we will consider the person as an employee. An employee database system wouldn’t need to store information like disease history, haemoglobin level etc. In this case we would only be interested in date of birth, salary, qualification etc.

In both cases, our requirement is to store detail information about a person. In one case we have a patient and in one case we have an employee. Depending on our application we selected what we wanted. Abstraction is the process in which we selectively choose what is needed for our application discarding the unnecessary attributes (based on the requirements). You’ll appreciate ADT as you progress through this chapter.

Data Encapsulation:

Data encapsulation/ data hiding is an important feature of object oriented programming. The mechanism of hiding data is to put them in a class and make them private. The data is now hidden and safe from any accidental manipulations, i.e. no function (from outside the class) can change the member data. Actually there are two things you can hide: implementation of functions (to the user it doesn’t matter as to how you’ve implemented a particular function) and data. In procedural programming it is possible to only hide the implementation details but you cannot hide/ protect data. OOP lets you achieve this. Why do we need to really hide anything? Who are we hiding the data from? These questions will be answered later in this chapter. A simple example is the case of the ‘car’ we considered earlier. We don’t want the user to directly access ‘speed’ and modify it. By making ‘speed’ private, we prevent the user from doing this.

Polymorphism:

Polymorphism means having many forms. Polymorphism can be seen frequently in the English language. There are many English words, which have a different meaning depending on the context of use. The statements “close a book”, “close the file”, “close the door” and “close the deal” all make use of the verb ‘to close’ but the meaning of each statement depends on the context. Another example is the sentence, “I’ve cracked the exam”. The meaning of cracked in this case is different from the crack used in a sentence like, “The pot cracked”. In both sentences the word is the same but its interpretation varies depending on the context.

In the same way you can think of many programming examples. For instance, consider the + operator. When it is used on numbers it will act as an addition operator, adding two numbers mathematically and giving the result. When the + acts on two strings, the result will be the concatenation of the two strings (we’ll take a look at string objects which permit us to use + later). For example:

“new” + “delhi” = “newdelhi”.

Thus, though the operator is the same (+), it can perform different actions depending on the type of the operands. This is a simple example of polymorphism. A few other examples of polymorphism are:

bullet

The << and >> operators are bit-shifting operators and they are also used for displaying information on the screen or for storing values in a variable (the circumstance decides whether they are used to shift bits or as input and output operators).

bullet

The division operator (/) when operating on two integers will produce an integer as the result of division. But if one of the operands are floating point numbers then the result will also be a floating-point number.

In all these cases, the same operator performs different functions depending on the situation. This is called polymorphism.

Inheritance:

Just as the name implies, inheritance refers to children inheriting property from their parents. In C++, the parents are called the parent classes and the children are called the derived (or child) classes. The idea of inheritance is to prevent classes from being redefined over and over again. If a programmer has already created a class and you want to make use of the same class with some additional features, then you needn’t re-write the entire class description again. Instead, you can derive a class from the original one (hence all the existing features of the class will be available in your class also) and you can add the extra features you need to your class. This is called re-usability of code. Instead of re-writing, we can re-use through the concept of inheritance. Let’s take the example of animals: a lion belongs to the cat family; the cat family comes under the mammals’ category and the mammals category will come under the general group called animals. Using inheritance, if a lion is being described then only the unique features of a lion need to be defined (you needn’t define the features of animals, mammals and cats). Thus the class ‘lion’ will be inherited from the class ‘cat’ which will in turn be inherited from the class ‘mammals’ which is inherited from ‘animals’.

OOP Languages:

Object Oriented Programming did not originate in C++. In fact it was already existing and OOP was combined with C programming to develop C++. A few of the OOP languages are:

bullet

Simula

bullet

Modula

bullet

SmallTalk

bullet

Ada

bullet

C++

bullet

Java

Some of these languages are said to be ‘pure OOP’ while others are ‘hybrid OOP’. ‘Pure OOP’ means that everything in a program has to be tied with classes (and you cannot use separate functions). Java is an example of pure OOP. C++ comes under hybrid OOP because you can use OOP as well as the normal C style coding (involving separate functions and data).

A closer look into OOP:

The world can be considered to consist of many objects. Objects will have attributes and behaviours. A water-heater is a simple example of an object. It has certain attributes or properties (like colour, size, maximum and current temperatures etc.) and there are certain behaviours associated with the water-heater (like switching on the heater, increasing the temperature or heating for a specified time interval, switching off the heater etc.). These are actions that can be performed on the heater. Or in other words they are actions which can modify certain properties of the heater (for instance by switching on the heater the current temperature of the heater will change).

A car is another example of an object. It has a lot of attributes such as fuel capacity, current speed, top speed, number of wheels, type of gearbox etc. There are also a lot of operations which you can perform on this object. For example: you can accelerate the car, apply brakes etc. The attributes of a car will have some values at any given instance of time. Once the car is in motion, you can say that at a particular time the speed of the car is 30 km/hr (thus current speed will be 30km/hr). Similarly, the color of the car is red or the car has four wheels. The values for the attributes at any given instant of time define the state of the object. There are two types of states an object can have: static and dynamic. Some attributes of the car will not change over a period of time. The number of wheels in the car is always going to be four (unless you are making a new prototype!). The colour of the car would also remain the same for a long time. These attributes contribute to the static state of the car. The current speed of the car is a dynamic property which will change frequently depending on the actions performed upon the car. In OO terminology you will encounter the following terms frequently:

bullet

State

bullet

Behaviour

bullet

Identity

Behaviour of an object refers to the set of operations (or actions) that can be performed on an object.

Every object will have some attribute that can be used to uniquely identify the object. For example let’s take the example of a car as an object. All cars have colour as an attribute. But can you distinguish two cars based on their colours? Definitely not. But you can distinguish two cars based on their registration number. Hence registration number is the attribute which can be used to uniquely identify a car. If you take a banking example then the account number is a unique way to identify an account (no two accounts can have the same account number).

An object will have two parts:

  1. Interface

  2. Implementation

In a car, the interface is the acceleration and braking actions which can be performed on the car (there are many more but lets just limit ourselves to these two actions). The driver is going to be the user of the car. When the driver presses the accelerator pedal, there are a lot of things that happen within the car which actually cause the rpm (rotations per minute of the wheel) to increase. Is the driver concerned about what actually happens within the engine? No. The driver just wants the car to accelerate on pressing the pedal and is least bothered about the underlying mechanisms used by the manufacturers to achieve this. He doesn’t care about how the engine is designed or as to how the piston is moving to achieve acceleration. All he knows (and wants to know generally) is that the car should accelerate when he presses the pedal. These internal mechanisms are called implementation details in OOP terminology. One of the central features of OOP is to separate the interface from the implementation. The person using an object should not know/worry about the implementation. This is what is termed encapsulation.

Classes and Objects in C++:

In C++ classes are used implement OOP. A class will contain two types of members: member data and member functions. The member functions can be used to operate on the member data within the class. The data members correspond to the attributes while the member functions correspond to the behaviour. Instead of the term ‘function’, some programmers use the term ‘method’.

The term ‘class’ and ‘object’ might seem confusing at first. Basically you cannot directly use a class (we need to create an instance of the class and we call this an object). In our fundamental data types we have int, double, char etc. But are we using them directly? For example, do we say:

int = 5;

No. If we were to do this then we would never be able to create different integer variables. We create an instance of an integer when we say:

int x = 5;

Since classes are also data types (user defined data types), they also follow the same principle. You have to create instances of a class to do anything useful. An object is an instance of a class, i.e. only when you define an object, will the compiler allocate memory for the object. Class is like a model (or a template) from which you can create many objects of the same type. A template can be compared to the plan of a building. When the plan is drawn, we have not yet allocated the area on land for construction. We only know about how the building structure will be. But when construction work begins, the area will be allocated. Similarly, the compiler allocates memory space for every object that is created. This is why a class is called an abstraction (in other words a class is a generality while an object is a specific instance of the class). Let’s say we have a class called student, with the attributes:

bullet

id

bullet

name

bullet

age

We can create two students by saying:

student Watson, Hastings;

Now, Watson and Hastings are 2 students. Each of them will have an id, name and age (we can modify their attributes separately). You will be able to distinguish between a class and an object clearly when we write a few programs.

Everything in a class (data and functions) is private, protected or public. They are called access-specifiers (because they decide how the class members can be accessed).

private:

bullet

As the name suggests, whatever is in the private area of a class can only be accessed from within the class.

bullet

If the data is made private then it can be accessed only through member functions of the class.

bullet

If a function is made private then it can be called from within another member function.

bullet

Data/function is made private by default (i.e. if you don’t mention any specifier).

protected:

bullet

The specifier ‘protected’ is used in inheritance and will be dealt with later.

public:

Public members of a class are accessible from outside the class. Hence when objects are created, they can access the public members directly. Usually, data is made private while the functions are made public (this is done to ensure data encapsulation). These public functions can operate on the private data.

The syntax for a class is:

class name-of-class
{
private :
    data-type variable-name;            
//these are private
public :
    functions;                
//these are public
};         // End of class- make a note of the terminator.

It is not a must that data should be private and functions should be public. You can have private functions as well public data.

Remember: No member of your class can be declared as ‘register’ or ‘extern’.

Note: Before getting into classes, you should know that there are two types of programmers who work with classes: the class designer and the class user. The designer (or creator) creates a class and the user makes use of the class in his/her programs.

The term ‘user’ usually represents the person who will use an application developed by a programmer. But if the term ‘user’ is used while explaining classes, then it refers to a ‘class user’ (a class user is another programmer).

In our example codes, we will be the designers as well as the users of the class (of course if you provide the code to someone else, then we will be considered the designers while they will be the users). A class creator will try to hide from the user whatever is not required by the user (why provide more options to the user and lead to complications!). Hide whatever is possible so that the class user cannot tamper with things that they are not supposed to use.

Go back to the Contents Page

Copyright © 2005 Sethu Subramanian All rights reserved.