Object and Class in C plus plus

Since C++ is an object-oriented language, program is designed using objects and classes in C plus plus.


C plus plus Object

In C plus plus, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc.

In other words, object is an entity that has state and behavior. Here, state means data and behavior means functionality.

Object is a runtime entity, it is created at runtime.

Object is an instance of a class. All the members of the class can be accessed through object.

Let’s see an example to create object of student class using s1 as the reference 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 plus plus Class

In C plus plus, object 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 C plus plus 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 plus plus Object and Class Example

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

 
  1. #include <iostream>  
  2. using namespace std;  
  3. class Student {  
  4.    public:  
  5.       int id;//data member (also instance variable)      
  6.       string name;//data member(also instance variable)      
  7. };  
  8. int main() {  
  9.     Student s1; //creating an object of Student   
  10.     s1.id = 201;    
  11.     s1.name = “Abhishek Tyagi”;   
  12.     cout<<s1.id<<endl;  
  13.     cout<<s1.name<<endl;  
  14.     return 0;  
  15. }  

Output:

201
Abhishek Tyagi

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

Let’s see another example of C++ class where we are initializing and displaying object through 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(201, “Abhishek”);    
  •     s2.insert(202, “Mukul”);    
  •     s1.display();    
  •     s2.display();  
  •     return 0;  
  • }  

Output:

201  Abhishek
202  Mukul

C plus plus Class Example: Store and Display Employee Information

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

 
  1. #include <iostream>  
  2. using namespace std;  
  3. class Employee {  
  4.    public:  
  5.        int id;//data member (also instance variable)      
  6.        string name;//data member(also instance variable)  
  7.        float salary;  
  8.        void insert(int i, string n, float s)    
  9.         {    
  10.             id = i;    
  11.             name = n;    
  12.             salary = s;  
  13.         }    
  14.        void display()    
  15.         {    
  16.             cout<<id<<”  “<<name<<”  “<<salary<<endl;    
  17.         }    
  18. };  
  19. int main(void) {  
  20.     Employee e1; //creating an object of Employee   
  21.     Employee e2; //creating an object of Employee  
  22.     e1.insert(201, “Abhishek”,990000);    
  23.     e2.insert(202, “Mukul”, 29000);    
  24.     e1.display();    
  25.     e2.display();    
  26.     return 0;  
  27. }  

Output:

201  Abhishek  990000
202  Mukul  29000