Search Your Topic

config
Tuesday, 13 June 2017 12:35

C++ Object and Class

Written by
Rate this item
(0 votes)

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


C++ Object

In C++, 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.

 
  1. 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++, 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++ class that has three fields only.

 
  1. class Student    
  2.  {    
  3.      public:  
  4.      int id;  //field or data member     
  5.      float salary; //field or data member  
  6.      String name;//field or data member    
  7.  }    

C++ 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 = "Sonoo Jaiswal";   
  12.     cout<<s1.id<<endl;  
  13.     cout<<s1.name<<endl;  
  14.     return 0;  
  15. }  

Output:

201
Sonoo Jaiswal

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.

 
  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.        void insert(int i, string n)    
  8.         {    
  9.             id = i;    
  10.             name = n;    
  11.         }    
  12.        void display()    
  13.         {    
  14.             cout<<id<<"  "<<name<<endl;    
  15.         }    
  16. };  
  17. int main(void) {  
  18.     Student s1; //creating an object of Student   
  19.     Student s2; //creating an object of Student  
  20.     s1.insert(201, "Sonoo");    
  21.     s2.insert(202, "Nakul");    
  22.     s1.display();    
  23.     s2.display();  
  24.     return 0;  
  25. }  

Output:

201  Sonoo
202  Nakul

C++ Class Example: Store and Display Employee Information

Let's see another example of C++ 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, "Sonoo",990000);    
  23.     e2.insert(202, "Nakul", 29000);    
  24.     e1.display();    
  25.     e2.display();    
  26.     return 0;  
  27. }  

Output:

201  Sonoo  990000
202  Nakul  29000
Read 261 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in
More in this category: « C++ OOPs Concepts C++ Constructor »

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.