×

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

Do-While Loop Examples in C++

Before we move on the examples of Do-While loop, let’s learn little bit about the Do-While loop in C++ language. Do While loop An iterative loop that checks the condition at the...

5 minutes read.

C ++ Program: Alphabet Triangle and Number Triangle

Alphabet Triangle and Number Triangle An alphabet triangle is a triangle that typically looks like a pyramid or other triangles like an isosceles triangle, a right-angled triangle consisting of similar or...

4 minutes read.

Array of Vectors in C++ STL

Prerequisites: C++ STL Arrays and C++ STL Vector. A group of items kept in consecutive memory region is known as an array. It is to group similar objects of the same...

4 minutes read.

Factory Method for Designing Pattern in C++

In C++, the factory method is a type of conditional design pattern. The factory method is related to creating a new object in C++. With the help of a factory...

3 minutes read.

getline() Function and Character Array in C++

In this article, we will explore about some concepts on getline() function and character array in the most useful language C++. The getline() method in C++ is simply a standard library...

6 minutes read.

C++ Pointer

Pointer is a derived data type that stores the address of a variable. A pointer is used for memory management and dynamic memory allocation. Pointer works on the address of data rather than...

2 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.

Palindrome Using While Loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

C++ Fork

A new process known as a "child process" is created with the fork system function and runs concurrently with the process that invoked fork() (parent process). Both processes will carry...

4 minutes read.

std::min in C++

std::min in C++ std::min is specified in the program code, used to calculate the lowest amount that has been transferred. When there's more of someone who returns first of them. It's used...

2 minutes read.

C++ | C Plus Plus Data type

Data type in every language is very important. Data type means the different kinds of data that are supported by a particular programming language. A computer language’s data types specify the...

15 minutes read.

Sliding Window Technique in C++

Sliding Window Technique or Window Sliding Technique is a computational technique that is mainly used to reduce the use of nested loop and replace it with a single loop. It...

4 minutes read.

Continue in C++ While loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the...

4 minutes read.

Reverse String Word-Wise in C++

What is a reversed String? Reversing the words of a sentence is called reversed string by words. The difference between the reverse a string and the reverse a string word-wise is...

4 minutes read.

C++ Aggregation

C++ Aggregation Definition: In C++, aggregation is a process in which one class (as an entity reference) defines another class. It provides another way to reuse the class. It represents...

4 minutes read.

Virtual Functions and Runtime Polymorphism in C++

In this tutorial, we will explore more on virtual functions and runtime polymorphism in the most useful language C++. A virtual function is a member function with the keyword virtual used...

6 minutes read.

Palindrome using Do-while loop in C++

What is Palindrome? A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++...

5 minutes read.

Random Number Generator in C++

In programming, we need to frequently create the randomly. For example, a dice game, handing out cards to players, apps for rearranging tunes, etc. T There are two tools available in...

4 minutes read.

INT_MAX and INT_MIN in C/C++

In competitive programming, assigning a variable that maximum or minimum value a data type can carry is frequently necessary. Still, it might be challenging to recall such a significant, exact...

3 minutes read.

Pointers in C++

Pointers are a powerful feature in the C++ programming language, allowing developers to directly manipulate memory addresses and create more efficient and dynamic programs. However, pointers can also source various...

3 minutes read.