Object in C++
In this article, we will learn about Object in C++.
In short, an object is a stateful entity with behaviour. Data is referred to as state, and functionality is referred to as behaviour.
An object appears to be a runtime entity that is created at runtime.
It is a class instance. All of the class members are accessible via the object.
Let's look at an example of creating a student class object with s01 as the reference variable.
Student s01; // creating a Student object
Throughout this instance, Student is the type, and s01 is just a reference variable in which it refers to a Student class instance.
C++ Object and Class Illustration
Consider the following class, which has two fields: id no and name01. It makes a class instance, initialises the object, and prints the object value.
C++ Program 1:
#include <iostream>
using namespace std;
class Employee {
public:
int id_no;
string name01;
};
int main () {
Employee s01; //creating an object of Employee
s01.id_no = 21150;
s01.name01 = "Sweta Thakur";
cout << s01.id_no << endl;
cout << s01.name01 << endl;
return 0;
}
Output:
21150
Sweta Thakur
Let's look at another C++ class where we're initialising and displaying an object via a method.
C++ Program 2:
#include <iostream>
using namespace std;
class Employee {
public:
int id_no;
string name;
void insert ( int id, string nm )
{
id_no = id;
name = nm;
}
void display ()
{
cout << id_no << " " << name << endl;
}
};
int main (void) {
Employee s01; //creating an object of Employee
Employee s02; //creating an object of Employee
s01.insert ( 201501, "Tiyasa" );
s02.insert ( 201502, "Sweta" );
s01.display ();
s02.display ();
return 0;
}
Output:
201501 Tiyasa
201502 Sweta
Let's look at another C++ class that uses methods to store and display patient information.
C++ Program 3:
#include <iostream>
using namespace std;
class Patient {
public:
int Bed_no;
string patient_name;
float bill;
void insert ( int B, string pn, float bl )
{
Bed_no = B;
patient_name = pn;
bill = bl;
}
void display ()
{
cout << Bed_no << " " << patient_name << " " << bill << endl;
}
};
int main (void) {
Patient p1; //creating an object of Patient
Patient p2; //creating an object of Patient
p1.insert ( 105, "Tiyasa", 9000 );
p2.insert ( 106, "Sweta", 29000 );
p1.display ();
p2.display ();
return 0;
}
Output:
105 Tiyasa 9000
106 Sweta 29000
Conclusion:
C++ programming’s primary goal is to add object orientation to the C programming language. A class is the core component of C++ which helps to facilitate object-oriented programming and is commonly referred to as user-defined types.
A class serves to define the form of an object, and it manages to combine data representation and methods for attempting to manipulate that data into a compact format. Class members are the data and functions contained within a class.
In C++, everything is linked to classes and objects, as well as their attributes and methods. For example, a bus is an object in everyday life. The bus has characteristics such as weight and colour, as well as modes of operation such as drive and brake.
Attributes and methods are variables and functions that are part of the class. These are commonly known as "class members."
A class is a user-defined data type that can be used as an object function Object() [native code] or "blueprint" for creating objects in our curriculum.