×

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.


Related Topics

Leap Year Program in C++

What is a Leap Year? A solar year is the length of time that it takes for Earth to orbit the Sun - approximately 365.25 days. In a calendar year, we...

4 minutes read.

C++ Interfaces

The C++ programming language provides programmers with a variety of capabilities and functions. It also enables object-oriented programming, which is essential while working on a project. It will be simple...

7 minutes read.

Constructor Overloading

The program contains more than one constructor in a class with the same name, and different types of arguments are called constructor overloading. Calling of constructor depends on the number and types...

2 minutes read.

Is it fine to write void main() or main() in C/C++?

In C programming language: The default function return type in the C programming language is "int," which implies that main() will always return an integer value. In C, the void main()...

3 minutes read.

C++ Switch

In C++, an expression or variable can be tested against a range of constant values using a switch statement, which is a control flow statement. It offers a productive method...

4 minutes read.

How to create a table in C++

C++ is a programming language that has evolved as an improvement of c language. It includes an object-oriented archetype. C++ programming language is a compiled language that complies with the...

3 minutes read.

Char Array to String in C++

Regardless of the programming language you use, data structure is critical to the success of your project. Although each programming language has its own collection of data structures, C++ contains a...

4 minutes read.

Auto keyword in C++

The primary function of auto keyword is to assign and detect the value of the data type automatically in C++. The compiler knows the data type of the variable by...

2 minutes read.

C++ Deque

Definition: Deque or the Doubly ended queue is a data structure or operation performed under queue where insertion and deletion are allowed at both ends. A deque is an ordered collection of...

5 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

Returning Multiple Values from a Function using Tuple and Pair in C++

We may come across many situations where after the driver code's execution is performed in a code block, the return should be either multiple values or a single value possibly...

4 minutes read.

Stack in C++

Stack: The stack is a very popular data structure. It is the form of data structure that follows a particular order called FIFO(First-In-First-Out). In simple words, a stack is an Abstract...

4 minutes read.

Naming Convention in C++

The first and most fundamental step a programmer takes to produce clean code is to name a file or a variable. This naming must be acceptable so that it serves...

5 minutes read.

How to improve programming skills in C++

Before getting started, one should know why to improve their programming skills. To become a good software developer or programmer, one must be skilled in at least one programming language. Many...

4 minutes read.

Differences between Local and Global Variable

Define Global Variable Global variables are those that may be accessible worldwide across a programme and are defined outside of any functions or blocks. It may be accessed by any function in...

3 minutes read.

Default arguments in C++

Arguments in a function are defined as the values supplied when the function is called. The source is the values supplied, and the destination is the receiving function. Let us...

3 minutes read.

C++ Program to Implement Merge Sort

C++ Program to Implement Merge Sort The technique of merge sort is based on the strategy of divide and conquer. We divide the set of while data into smaller bits, arranged...

3 minutes read.

Armstrong number using for loop in C++

What is For Loop? A for loop is a repetitive control structure that allows you to create a loop to execute a specific number of times efficiently. The syntax that can be...

4 minutes read.

C++ Dijkstra Algorithm Using the Priority Queue

In this article we will be finding the shortest routes from a source vertex in a graph to all vertices in the graph, given a graph and a source vertex...

5 minutes read.

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

4 minutes read.