C++ Object and Class

Since C++ is an object-oriented language, program is designed using objects and classes in 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.

Example
snippet
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.

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.

Example #1

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

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

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.

snippet
#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 = 201;  
    s1.name = "John Abraham"; 
    cout<<s1.id<<endl;
    cout<<s1.name<<endl;
    return 0;
}
Output
201 John Abraham
Example #3

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.

snippet
#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, "John");  
    s2.insert(202, "Nakul");  
    s1.display();  
    s2.display();
    return 0;
}
Output
201 John 202 Nakul
Example #4

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.

snippet
#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(201, "Sonoo",990000);  
    e2.insert(202, "Nakul", 29000);  
    e1.display();  
    e2.display();  
    return 0;
}
Output
201 Sonoo 990000 202 Nakul 29000
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +