Object in C /C++ with programming example

(Last Updated On: April 4, 2021)

Introduction of the Object :

object in c/c++ A data type is used to declare a variable. A variable of a data type is also known as the instance or case of that data type. Each variable has a unique name but each variable follows the rules of its data type. When a variable of a data type is declared, some space is reserved for it in the memory.

A class is also like a data type. It is therefore used to declare variables or instances. The variable or instances of a class is called object in c. A class may contain several data items and functions. Thus, the object of a class consists of both the data members and the member functions of the class. The combining of both the data and the functions into one unit is called data encapsulation.

An object in c/c++ represents data members of a class in the memory. Each object of a class has a unique name. the name of an object differentiates it from other objects of the same class. The values of data members of different objects may be different or same. The values of data members in an object are known as the state of the object.

The functions in an object are called the member functions. They are also known as methods. The member functions are used to process and access data of the object in c/c++.

For example, the data from an object is accessed and processed through the member function of the object. It cannot directly read the data in the object. The data is hidden for other objects of the program. This is called data hiding. The data hiding and data encapsulation are the main features of object-oriented programming.

Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Declaring Object in c/c++ of a Class:

The objects to a class are declared in a similar way as the variables of any data or structure type are declared.

When a class is defined, no space is reserved for it in the memory. It only provides information about how its object will look like. When an object of a class is declared, memory space is reserved for that object.

The syntax to create an object in c/c++ of class type is :

Class_name object_name separated by commas;

For example, to define an object data of class student, the statement is written as :

student data;

In the above statement, one object data is declared of class “student”. It is the declaration of an object that actually creates an object in the memory.

Calling Member Functions of the object in c/c++:

The member function of a class is called in a similar way as member or data item of a structure is called. The member function is called through an object of the class. The dot operator is used. The dot operator connects the object name and the member functions.

The general syntax to call a member function is:


For example, if  “data” is the name of the object  and “show_data” is the member function then the member function is called as shown below:


the dot operator is also called the class member access operator.

Only those member functions can be accessed from outside the class with the dot operator that has been declared as public.

Example how to call member function in  of the object in c/c++ programming:


Example calling member function  of Object in c/c++:


Example how to use an object in c/c++ programming:


Defining Member Functions Outside the Class:

Member functions of a class can also be defined outside the class. In this case, only the prototype of the member function is declared inside the class.

The member functions are defined outside the class in a similar way as user-defined functions are defined. However, the scope resolution operator ( :: ) is used in the member function declaration to define the function of class outside the class.

The general syntax of a member function definition outside the class is:

Type class_name :: function_name(arguments)


Body of the function;




          Represents the data type of value returned by the member function.


          Represents the class name to which the member function belongs.


          Double colons represent a scope resolution operator that is used to define the body of the member function of a specified class.


          Represents the name of the member function of a class with arguments.

Body of the function

          It is the set of statements of the function.

The member function is defined outside its class only if the body of the function definition in is large. Otherwise the function definition should be defined inside the class.

Example how to call Member Functions Outside the Class in programming:


Storage of Objects in Memory:

When an object of a class is created, space is reserved in the computer memory to hold its data members. Similarly, a separate memory space is reserved for each class object.

The member functions of a class are, however, stored at only one place in the computer memory. All objects of the class use the same member functions to process data.

Therefore, while each object has separate memory space for data members, the member functions of a class are stored in only one place and are stored by all objects of the class.

Example how to use storage of objects in memory in c/c++ programming:


the storage of object in c/c++ a and b as mentioned in the above program example is shown below. These objects use the same member functions.

object in c

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply