OOPs Concept in C++

Object-oriented programming shifts the focus of attention to the objects, that is, to the aspects on which the problem is centered. A program designed to maintain bank accounts would work with data such as balances, credit limits, transfer, interest calculation, and so, on. An object representing an account in a program will have properties and capacities that are important for account management.

OOP-object combine data(properties) and functions(capacities).A class defines a certain object type by defining both the properties and the capacities of the objects of that type.

OPP's Concept
OPP’s Concept

classes (including constructors and destructors), encapsulation, inheritance, polymorphism, virtual functions (dynamic binding), etc. This is the part of C++ to which the classic rules for object-oriented design most directly apply.

Page Contents:

Creating new data types
Data Abstraction
Polymorphism and Overloading
Advantage of OOPs over Procedure-oriented programming language


When you approach a programming problem in an object-oriented language, you no longer ask how the problem will be divided into functions, but how it will be divided into objects. Thinking in terms of objects, rather than functions, has a surprisingly helpful effect on how easily programs can be designed. This results from the close match between objects in the programming sense and objects in the real world.


In OOP we say that objects are members of classes. Here we know that almost all computer languages have built-in data types. For example, data type int(predefined in C++). You can declare as many variables of type int as you need in your program:

int hour;
int day; 
int date; 
int year;

In a similar way, you can define many objects of the same class, the class serves as a plan, or blueprint. It specifies what data and what functions will be included in the objects of that class. Defining the class doesn’t create any objects, just as the mere existence of data type int doesn’t create any variables.

A class is thus a description of a number of similar objects. This fits our non-technical understanding of the word class. Here take an example Such as Car, Bike, bicycle is a member of the Class vehicle. So here there is no one called “vehicle”, but specific people with specific names are members of this class if they possess certain characteristics. An object is often called an “instance” of a class.

Examples for object and Class

 //Class name is Vehicle 
class Vehicle 
//Data members     
char name[20];     
int speed;     
int weight;
void brake()
void slowDown()
 int main() 
//ford is an object    
Vehicle Car;  


The mechanism of deriving a new class from an old one is called inheritance. the old class is known as the base class or superclass or parent class and the new one is called subclass or derived class or child class. The inheritance allows subclasses
to inherit all the variables and methods of their parent classes

The idea of classes leads to the idea of inheritance. In our daily lives, we use the concept of classes divided into subclasses. We know that the animal class is divided into mammals, amphibians, insects, birds, and so on. The vehicle class is divided into cars, trucks, buses, motorcycles, and so on. The principle in this sort of division is that each subclass share common characteristics with the class from which it’s derived. Cars, trucks, buses, and motorcycles all have wheels and a motor; these are the defining characteristic of vehicles.

OOP class become a parent of several subclasses. In C++ the original class is called the base class; other classes can be defined that share its characteristics, but add their own as well. These are called derived classes.

Don’t confuse the relation of object to classes, on the one hand, with the relation of a base class to derived classes, on the other. Objects, which exist in the computer’s memory, each embody the exact characteristics of their class, which serves as a template. Derived classes inherit some characteristics from their base class, but add new ones of their own. Inheritance is somewhat analogous to using a function to simplify a traditional procedural program. If we find that three different sections of a procedural program do almost exactly the same thing, we recognize an opportunity to extract the common elements of these three sections and put them into a single function. The three sections of the program can call the function to execute the common actions, and they can perform their own individual processing as well. Similarly, a base class contains elements common to a group of derived classes. As functions do in a procedural program, inheritance shortens an object-oriented program and clarifies the relationship among program elements.

class Base
int val;
void setData(int v)
class Derived:public Base
void cube()
cout<<"Cube of"<<val<<"is"<<val*val*val;
int main()
Derived d;
return 0;


Cube of 10 is 1000


Once a class has been written, created, and debugged, it can be distributed to other programmers for use in their own programs. This is called reusability. It is similar to the way a library of functions in a procedural language can be incorporated into different programs.

However, in OOP, the concept of inheritance provides an important extension to the idea of reusability. A programmer can take an existing class and, without modifying it, add additional features and capabilities to it. This is done by deriving a new class from the existing one. The new class will inherit the capabilities of the old one, but is free to add new features of its own.

For example, you might have written(or purchased from someone else) a class that creates a menu system, such as that used in Windows or other Graphic User Interfaces(GUIs). This class works fine, and you don’t want to change it, but you want to add the capability to make some menu entries flash on and off. To do this, you simply create a new class that inherits all the capabilities of the existing one but adds flashing menu entries.

Creating new data types:

One of the benefits of objects is that they give the programmer a convenient way to construct new data types. Suppose you work with a two-dimensional position(such as x and y coordinates, or latitude and longitude) in your program. You would like to express operations on positional values with normal arithmetic operations, such as


Where the variable p1,p2, and origin each represent a pair of independent numerical quantities. By creating a class that incorporates these two values, and declaring p1,p2, and origin to be objects of this class, we can, in effect, create a new data type. Many features of C++ are intended to facilities the creation of new data types

Data Abstraction:

Classes normally hide their implementation details from the clients of the classes. This is called information hiding.

As an example of information hiding, let us consider a data structure called a stack.

Think of a stack in terms of a pile of dishes. When a dish is placed on the pile, it is always placed at the top(referred to as pushing onto the stack); when a dish is removed from the pile, it is always removed from the top(referred to as popping off the stack). Stacks are known as last-in,first-out(LIFO) data structure the last item pushed(inserted) on the stack is the first item popped(removed) from the stack. Describing the functionality of a class independent of its implementation is called data abstraction.

The class has the property of information hiding. This means that although class objects may know how to communicate with one another across well-defined interfaces, classes normally are not allowed to know how other classes are implemented implementation details are hidden within the class themselves. Surely it is possible to drive a car effectively without knowing the details of engines, transmission, and exhaust systems work internally

why the information hiding in C++ is so crucial to good software engineering.?

In C and other procedural programming languages, programming tends to be action-oriented, whereas ideally in C++ programming is object-oriented. In C, the unit of programming is the function, In C++, the unit of programming is the class from which objects are eventually instantiated(created). Data abstraction is the concept include in Computer Science and software development.In C++ programmers concentrate on writing functions.Groups of actions that perform some task are formed into functions, and functions are grouped to form programs. Data are certainly important in C, but the view is that data exit primarily in support of the actions that functions perform. The verbs in a system specification help the C programmer determine the set

 #include <iostream> 
using namespace std; 
class Abstraction 
int a, b; 
// method to set values of  
// private members 
void set(int x, int y) 
a = x; 
b = y; 
void display() 
cout<<"a = " <<a << endl; 
cout<<"b = " << b << endl; 
int main()  
Abstraction obj; 
obj.set(10, 20); 
return 0; 


a = 10 
b = 20  

Polymorphism and Overloading:

Note* that the =(equal) and +(plus) operators, used in the position arithmetic above, don’t act the same way they do in operations on built-in types such as int. The objects p1 and so on are not predefined in C++ but are programmer-defined object of class Position. How do the = and + operators know how to operate on objects?

The answer is that we can define new behaviors for these operators. These operations will be member functions of the position class.

Using operators or functions in different ways, depending on what they are operating on, is called polymorphism(one thing with several distinct forms). When an existing operator, such as + or =, given the capability to operate on a new data type, it is said to be overloaded. Overloading is a kind of polymorphism; it is also an important feature of OOP. One example of an overloaded operator built into C++ is operator <<, which is used both as the stream-insertion operator and as the bitwise left-shift operator. Similarly, >> is also overloaded; it is used both as the stream-extraction operator and as the bitwise right-shift operator


Encapsulation is defined as the wrapping of data into a single unit. encapsulation makes it possible for an object to be treated like ‘black boxes’ each performing a specific task without any concern for internal implementation.

Advantage of OOPs over Procedure-oriented programming language

1.Reduced susceptibility to errors
2.Easy re-use
3.Low maintenance requirement 4.Easy to maintain
5.Easy to read the code and debug
6.Easy to extend the program and add features
7.Easy to extend the program and add features
8.reduce the no of lines of code
9.Providing data security and accidental corruption by restricting access
11. Easy problem solving

Translate »