Object and Class In C++

Share Your Love

The Class and the Object In C++, tackling real-world problems is a critical component. Because C++ is an object-oriented programming language, the program has been developed with C++ objects and classes.

C++ Object:

Before start discussing Object and Class In C++ first, we discuss Object.

An Object is a real-world entity in C++, such as a chair, automobile, pen, phone, laptop, and so on.

To put it another way, an object is an entity with a state and behaviour. Here, state information or data, and behaviour refers to functioning.

The object is a runtime entity, it is created at runtime.

We represent an object as an instance of a class. Objects can be used to access all members of the class.

Let’s look at an example of creating a student class object with s1 as the instance variable.

Student s1;  //creating an object of Student      

In this example, Student is the type and s1 is the reference variable that refers to the instance of Student class.

C++ Class:

In C++, a class is a group of similar objects. It is a template from which objects are created. It can have fields, methods, constructors etc.

Let’s see an example of a C++ class that has three fields only.

  class Student    
 {    
     public:  
     int id;  //field or data member     
     float salary; //field or data member  
     String name;//field or data member    
 } 

C++ Object and Class Example:

Let’s see an example of a class that has two fields: id and name. It creates an instance of the class, initializes the object and prints the object value.

  #include <iostream>  
using namespace std;  
class Student {  
   public:  
      int id;//data member (also instance variable)      
      string name;//data member(also instance variable)      
};  
int main() {  
    Student s1; //creating an object of Student   
    s1.id = 324;    
    s1.name = "Lingaraj Senapati";   
    cout<<s1.id<<endl;  
    cout<<s1.name<<endl;  
    return 0;  
}

Output:

324
Lingaraj Senapati

C++ Class Example: Initialize and Display data through method:

Let’s see another example of a C++ class where we are initializing and displaying objects through the method.

#include <iostream>  
using namespace std;  
class Student {  
   public:  
       int id;//data member (also instance variable)      
       string name;//data member(also instance variable)      
       void insert(int i, string n)    
        {    
            id = i;    
            name = n;    
        }    
       void display()    
        {    
            cout<<id<<"  "<<name<<endl;    
        }    
};  
int main(void) {  
    Student s1; //creating an object of Student   
    Student s2; //creating an object of Student  
    s1.insert(203, "Lingaraj");    
    s2.insert(204, "Senapati");    
    s1.display();    
    s2.display();  
    return 0;  
}  

Output:

203 Lingaraj
204 Senapati

C++ Class Example: Store and Display Employee Information:

Let’s see another example of a C++ class where we are storing and displaying employee information using the method.

#include <iostream>  
using namespace std;  
class Employee {  
   public:  
       int id;//data member (also instance variable)      
       string name;//data member(also instance variable)  
       float salary;  
       void insert(int i, string n, float s)    
        {    
            id = i;    
            name = n;    
            salary = s;  
        }    
       void display()    
        {    
            cout<<id<<"  "<<name<<"  "<<salary<<endl;    
        }    
};  
int main(void) {  
    Employee e1; //creating an object of Employee   
    Employee e2; //creating an object of Employee  
    e1.insert(204, "Lingaraj",1000000);    
    e2.insert(205, "Senapati", 300000);    
    e1.display();    
    e2.display();    
    return 0;  
}  

Output:

204  Lingaraj  1000000
205  Senapati  300000

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 411

Newsletter Updates

Enter your email address below to subscribe to our newsletter