A Very Easy to Understand C++ OOP Example

What is OOP?

OOP is short form of Object Oriented Programming. The core of this programming technique is to create an object which has certain properties and functions. It is based on real-world modeling. As in real-world, things have properties and working capabilities. Similarly, objects have data and functions. Data represents properties and functions represent working of objects.

What is Object?

Object is a collection of data and functions. Object may represent a person, thing or place in real-world. For example a car is an object which has certain properties such as color, model etc. It also has some functions like accelerate, brake, reverse etc.

What is Class?

Classes are just like blueprints for creating objects. All properties and functions of an object are specified in classes.

The code given below is an example of object oriented programming using C++ . It demonstrates the function of objects and classes.

C++ OOP Example Code:

Code Explanation:

The above given code is a basic C++ OOP example. It simply asks user to enter two numbers and then displays that numbers on the screen. The program creates a class named example and  an object on the base of class.

The class contains data members, constructor and functions. The two data members are variables of integer type. These are simply used to take input from user. The constructor and functions are as follows:


This is a constructor. It is used to initialize values of data members. it initializes both variables to zero.


This function is used to take input from user. it simply uses cin statement to take values in variables.


This function is used to show output to user. It uses cout statement to display numbers on the screen.

Recommended reading: C++ Program to add two numbers using classes

In main function, an object of the class is created. The name of this object is obj. And then we just used two functions for input and output.

Program Output:

OOP Example using C++

If you liked this article, then please consider sharing it with your friends and leaving a comment below.

You can also Click Here To Download Exercise Files.