What is an Object in C++
Introduction
C++ is an object-oriented language used to simulate real-world elements in programs. Classes and objects are used in all object-oriented programming languages to accomplish this goal. To generate objects with comparable characteristics, classes serve as a template. The object-oriented methodology is built upon the basic concept of classes and objects in C++. Decreasing code duplication and debugging time improves the program's performance.
Classes in C++
A class, which is a template or a blueprint, links the traits and capabilities of an entity. The concept of a class allows you to group all entities or objects with similar characteristics. Encapsulation, data hiding, and abstraction are just some fundamental ideas that classes implement further. An object or instance of a class in C++ works as a data type and can have numerous copies of it.
Objects in C++
In C++, real-life things are comparable to objects. There are things about you, including automobiles, dogs, seats, tables, and many other things, such as birds and trees. These objects are linked to a few properties and functions. Objects are a concept that is also present in C++. Each detail about the objects of the class type is included in the definition of a class. The class name serves as the type specifier, allowing it to build objects with similar properties after defining the class.
Data structures that are instances of classes are called objects. In order to keep information and execute the required actions on it, storage is not provided if a class is defined; nevertheless, memory is allocated when an instance is created by defining an object.
Syntax
The C++ syntax for generating objects is as follows
class_name object_name;
If created, the object object_name can be used with the dot operator to get the information from the class class_name, including its information, participants, and member methods:
obj.data = 11; // accessing data member
obj.fun(); // accessing member function
Array of Objects
A data collection kept in multiple contiguous memory places is called an array in C++. In addition to various derived data types like structures, pointers, etc., an array can include int, string, or char values. A collection of items from the same class is referred as an array of objects. The total number of objects in the array shall be declared in closed brackets after the object name.
Syntax
ClassName ObjName[Number of Objects];
Example
Car car[4] //array of 4 objects of class Car
The use of an array of objects is demonstrated in the following example.
Code
#include <iostream>
using namespace std;
class Example1
{
public:
//Declare the member function
void printValue(int value)
{
cout << "The value is: " << value << "\n";
}
};
int main()
{
//Create an object of the class
Example1 obj[6];
// call printValue() function for all objects
for (int i = 1; i < 6; i++)
{
obj[i].printValue(4 * i); // printValue() is called
// for a particular object
}
cout << "\n\n";
return 0;
}
Output
![What is an Object in C++](https://static.tutorialandexample.com/cpp/what-is-an-object-in-cpp1.png)
Objects as Function Arguments
Similar to passing parameters to a function, class objects can be supplied as parameters in C++. A friend function, a non-member function, or a member function can all accept an object as an argument. The member and friend functions have access to the object's private and public members. A non-member method is limited to only using the object's public participants, though. There are a few approaches to sending the objects as arguments:
- Objects passed by values
- Objects passed by references
We'll look at two approaches of passing an object as the function's parameter.
Passing Objects by Value
You can pass objects as parameters to a method using the "call by value technique." The function is given nothing but the object's value. For the function, it creates a unique copy of the object. The original object is not altered by any changes the function makes to its members. The instance following illustrates the way to supply objects by value as arguments.
Code
#include <iostream>
using namespace std;
class CTemperature
{
public:
float f1, c1;
void getTemp(float value)
{
f1 = value;
}
// function that accepts an object as an argument
void findTemp(CTemperature obj)
{
obj.c1 = ((obj.f1 - 32) * 5) / 9;
cout << "\nThe temperature in Celsius is: " << obj.c1 << "\n";
}
};
int main()
{
//Create objects of the class
CTemperature obj1;
obj1.getTemp(98);
obj1.findTemp(obj1); // pass obj1 by value
// garbage value of c will be printed
// as the object was passed by value
cout << "The value of c1 (object's copy) is: " << obj1.c1 << "\n";
cout << "\n";
return 0;
}
Output
![What is an Object in C++](https://static.tutorialandexample.com/cpp/what-is-an-object-in-cpp2.png)
In the example above, the object obj1 of the type CTemperature is supplied as a value to the function findTemp(). Modifies the participant parameter "c1" to reflect the current number while estimating the temperature in Celsius. It merely modifies the function's version of "c," leaving the copy of the object's variable "c1" unchanged because it sends the object by value. As a result, the value is printed by the obj1. The c1 statement is garbage.
Passing Objects by Reference
The alternative way of providing an object as a parameter can be done using the "call by reference technique." The object's storage location is supplied as an argument in this situation. Therefore, the object and its members are readily available via the function. The initial members of the object are now the only ones that the function has altered. The example that follows shows how to use sending objects by reference as parameters.
Code
#include <iostream>
using namespace std;
class RObject
{
public:
int o ;
void updateValue(RObject &obj)
{
obj.o = 500;
cout << "\nThe modified value of o is: " << obj.o << "\n";
}
};
int main()
{
//Create an object of the class
RObject obj;
obj.updateValue(obj); // pass obj by reference
// same value of x will be printed
// as the object was passed by reference
cout << "The value of o (object's copy) is: " << obj.o << "\n";
cout << "\n";
return 0;
}
Output
![What is an Object in C++](https://static.tutorialandexample.com/cpp/what-is-an-object-in-cpp3.png)
The function updateValue() in the example above is called with the object obj of the class RObject as a parameter. Direct access to the object's storage location is provided to the function. The original object is transformed when the function uses the statement obj. o = 500 to change the member variable "o". The resulting statement prints the changed value of o at the same location.
cout << obj.o
Conclusion
An object is a data structure that is an instance of a class, whereas a class in C++ is a user-defined data type that performs the same as the built-in data types of C++. Class and object represent the two primary building blocks of object-oriented programming in C++. When declaring an object, we first state the class, then the user-defined object name. Similar to passing any other primitive data like int, string, etc., we pass objects as arguments.