Skip to main content

C++: Object, Class

C++ Object and Class

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



Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);               f.setSize( 400 , 400 );               f.setLayout( null );               f.setVisible( true );   }   public   static   void  main(String args[]){           new  ScrollbarExample();   }   }   Output: Java AWT Scrollbar Example with AdjustmentListener import  java.awt.*;   import  java.awt.event.*;   class  ScrollbarExample{        ScrollbarExample(){               Frame f=  new  Frame( "Scro

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari

C++ this Pointer, static, struct and Enumeration

C++ this Pointer In C++ programming,  this  is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++. It can be used  to pass current object as a parameter to another method. It can be used  to refer current class instance variable. It can be used  to declare indexers. C++ this Pointer Example Let's see the example of this keyword in C++ that refers to the fields of current class. #include <iostream>    using   namespace  std;   class  Employee {       public :           int  id;  //data member (also instance variable)               string name;  //data member(also instance variable)            float  salary;          Employee( int  id, string name,  float  salary)             {                   this ->id = id;                  this ->name = name;                  this ->salary = salary;            }             void  display()             {                 cout<<id<< "  " <<name<&