Back Button
Back to Chapter
C++ Introduction
No items found.
C++ Basics
No items found.
C++ Control Statements
No items found.
C++ Logical Operators
No items found.
C++ Procedural Programming
No items found.
C++ Structural Programming
No items found.
C++ Implementation of OOPS
No items found.
C++ Arrays and Vectors
No items found.
C++ Error Handling
No items found.
C++ File Handling
No items found.
Collapse_Icon
Table of Contents

OOPS stands for Object-Oriented Programming, this programming paradigm emphasizes the creation of objects which is basically a C++ realization of a real-world object, consisting of attributes like color, size, and shape, etc as well as its behavior.

In Object-Oriented approach the programmers mindset should be to develop a program in terms of real-world objects.

To understand the object-oriented approach better, consider an application for a car showroom, now this app will have information on cars and customers.

The Figure above is a rough representation of how OO programming makes solving real-world problems easier for us by allowing us to make use of certain concepts which will make programming with C++ easier than ever. Let us take a look at these concepts

Encapsulation

In the object-oriented approach we wrap all the attributes and behavior of a real-world object inside a single unit this is known as encapsulation. In the above diagram, the green block contains all the attributes and functions of a customer. Encapsulation is one of the most important concepts of OO programming. Encapsulation enables us to store all the information regarding a particular object in one place. Encapsulation leads us to another important concept of the OO approach data hiding or abstraction.

Data Abstraction

Data abstraction the most essential and important feature of object-oriented programming in C++. Abstraction means displaying only essential information and hiding other details. Data abstraction refers to providing only essential information about the data to the outside world while hiding the background details or implementation.

Consider a real-life example of a pilot flying a plane only the pilot knows how to fly and where the steering and brakes, etc are, he is enclosed inside a cockpit and no passenger has access to the cockpit. The passengers don't see anything and only know that the pilot will fly them to their destination, this allows us to prevent chaos in the airplane if the cockpit was visible to the passengers the mechanism would seem intimidating to the passengers and this could lead to chaos in the plane. It could also lead to passengers trying to take control and flying the plane themselves the results would be disastrous, similarly, in the case of a C++ program exposing the data members of a class to the outside function would cause many problems hence we use data abstraction.

We already talked about access specifiers, in OO programming the object attributes are usually private and the behaviors are public, So if we had to access an attribute we would have to go through the behaviors of the object.

Polymorphism

The word polymorphism means having many forms. A human can have different personality traits. A woman can be a mother, a wife, an employee as well as a daughter.  For each of her roles she has a different personality trait as a mother she is caring and loving, as a daughter she is respecting, and as an employee, she is punctual and decent, and so on. The personality traits are like a function and each time she is called upon as a mother her personality is different from when she is called upon as an employee.

Similarly same functionName can be used perform different task based on the function call.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a7972d40-96fc-4b27-aec9-b22cdd433a11/Untitled_Diagram_(22).png

Although the functions in the above image have the same name they differ in terms of the return type. Hence when we call a function with double type double sum gets called and when we call with int type int sum gets called. This is known as function overloading.

The compiler decides which function to call based on the function signature

Inheritance

Consider the earlier example of an application for a car showroom, now having a single type of car in a showroom makes no sense so we need to have different classes for different cars but we have already defined some properties of a car, so we can use the previous car class as a superclass and have sub-classes which will give specific details of the car.

The sedan will have all characteristics of a car and a few new features which will be specific to the sedan, similarly, the SUV will have characteristics specific to an SUV along with the basic characteristics of a car.

When we derive a subclass from a superclass the subclass is said to be inherited from the superclass and this process is known as inheritance.

Sub Class: The class that inherits properties from another class is called Sub class or Derived Class.

Super Class:The class whose properties are inherited by sub class is called Base Class or Super class.

Message passing

Objects communicate with one another by sending and receiving information to and from each other. A message for an object is a request for execution of a function and therefore will invoke a function, in the receiving object this generates the desired results. Message passing involves specifying the name of the object, the name of the function and the information to be sent.

We will learn about the implementation of all these concepts in C++ in the next chapter, we will also build a simple application for a bmw showroom.