Object-Oriented Programming is a concept that aims at solving problems in a more realistic way. It is not a programing language in itself.
1.1 Classes and Objects
Classes and Objects are the building blocks of Object-Oriented Programming. A house, an aircraft, a phone, a car are all examples of objects in real life. Each object is different from the other and you can inherently distinguish between these objects. Thus, each object has its own identity. In addition, objects have attributes. For example, attributes of a phone are its model, color, and price. Also, objects behave in a certain way that is specific to their type. A phone rings, a car can move forward and backward. Similarly, objects in Object-Oriented Programming have an identity, attributes, and behavior.
Note: The value of attributes of an object indicates their state.
Before you actually build a house, you create a blue print of the house. The blue print has all the details about the house, but it’s not the actual house. You can build multiple houses from the same blue print. Similarly in Object-Oriented Programming, you create a class first and then objects of that class. The class is the blue print, and it contains data and functionality.
Instantiation is the process of creating objects from a class. An object is an instance of a class. You can create multiple objects of the same class.
In the above example, the Person class has the attributes Name and Age. The class has two objects, and each has its own set of attributes; Raymond 22, and Alice 35. For more information, see C++ Classes and C++ Objects.
While creating classes, you need to keep the following principles in mind:
Abstraction refers to the concept of looking at the task in hand from a higher level and not being concerned about the implementation or background details. Consider cars and trucks, they are different but both are vehicles. Thus, vehicle is a higher level description of cars and trucks or an abstraction of cars and trucks. Similarly, apples and mangoes both are different but at the same time at higher level they both are fruits. Thus, fruit is an abstraction of mangoes and apples.
1.2.1 ABSTRACTION EXAMPLE
Consider that you need to create an employee database. What information about the employees would you store in the database? A pool of information is available about each employee:
- Date of Joining
- Year of Graduation
- Favorite book
- Favorite cuisine
- Marital status
However, for the Employee database, you don’t need all the above information. You need to select relevant information. In other words, you need to abstract data from a larger pool of data.
In C++, you use classes to implement the concept of abstraction.
In procedural programming, all the data is accessible to all parts of the program. This became a problem as programs grew larger and more complex. If the value of data is not what you expect it to be, then you have to go through the entire code to find where the issue is. On the other hand, if changes have to be made to the data, they have to be made in all parts of the program that are accessing the data. As the data is accessible all through the program, the integrity of data is also questionable.
Encapsulation addressed these problems by bundling data and functions that act on that data in a single unit (class). You can look at it is having mini programs, instead of one large program. Only the functions of a class can directly access its data. Now if the value of data is not what you expected it to be, you only have to go through a small piece of code that has access to that data. Similarly if you need to change data, then you only have to update a small piece of code. As you have limited the access to data, by encapsulating it, the chances of deliberate or accidental alteration to data are minimized.
In C++, you can implement encapsulation using access sepcifiers. For more information, see C++ Encapsulation in the tutorial.
In real life, we often say that a child has the same features as its mother or behaves like its father. In other words, the features and behavior of the parent are now a part of the child. The child has his own unique features too. Object-Oriented Programming also supports the concept of inheritance. It allows you to inherit the attributes and behavior of an existing class to create a new class. The new class has all the features of the existing class and can add its own attributes and behavior. Inheritance promotes code reuse.
The class from which you inherit is known as the Base Class/Super Class or Parent Class, and the new class is the Derived Class/Sub class, or Child Class.
To implement inheritance, while creating classes, think of “IS A” relationship.
- A car is a vehicle
- A Mango is a fruit
- A truck is a vehicle
- An Apple is a fruit
- A person is customer
- A person is an employee
But you don’t say a truck is a car, an apple is a mango, or an employee is a customer. These statements do not make sense. In essence, you can say that cars and trucks are specialized instances of vehicles. Thus, Vehicle, Fruit, and Person are all candidates for a super class.
1.4.1 INHERITANCE EXAMPLE
In the above diagram, Person is the Super class and has two derived classes; Customer and Employee. Each of these classes has all the attributes of the person class and also their own attributes. The Customer class has CustomerID, and Employee class has EmployeeID. Similarly, derived classes can have their own methods too. By identifying inheritance relationships, you can save a lot of time and effort by not writing the same code multiple times. For more information, see C++ Inheritance.
Polymorphism comes from two Greek words; Poly (many) and Morphos (form); many forms. Polymorphism adds flexibility and makes the program more user-friendly. You can achieve polymorphism in C++, by overloading functions and operators. For more information, see C++ Function Overloading and C++ Operator Overloading.