Sunday 4 March 2018

Class and Object in C++

class
      A class is a collection of objects.
A class is a collection of data members, member functions and object.
It is the blueprint that describes the details of an object.

Declaration of class
class class_name
{
//variables or data members
//functions or member functions
};

Where class is a keyword and class name is user defined that means you can give any name to class

Example:
class  Employee
{
     public:  
     int id;     //field or data member or instance variable     
     String name;
     float  salary; 
      public:
    void show()
    {
    // statements
    }  
       };

     Object

Object is an instance of a class, and it has its own state, behavior and identity.
All the members of the class can be accessed through object.
Object is a real world entity, for example mobile, laptop, computer, chair and table etc.
Example: if computer is a class then keyboard, mouse are the objects.

Declaration of object:

class_name  reference_name;

Example:
 Employee  e1; //Employee is class name, e1 is reference name

 e1.show ();

 // call or invoke the member functions to execute the program (e1 is the reference   name and show () is member function)

e1.id=105;
// used to initialize the variable (e1 is the reference name and id is the data member)

Uses of object:

It is used to call or invoke the member functions to execute the program.
It is used to initialize the data members.
 
1) Object is used to call or invoke the member functions
 1st way(Using initialization)
    #include <iostream.h> 
    #include<conio.h>
    class Employee { 
       public: 
       void show(int id, string name, float salary)   
        {   
           
  cout<<"id is"<<id<<"\n"<<"name is:"<<name<<" \n "<<"salary is:"<<salary<<endl;   
        }   
       }; 
  
  void main() { 
   clrscr();
    Employee e1; //creating an object of Employee class 
   e2.show(102, "suryosnata", 35000);
    getch();  
      }
    Output:
id is: 102
name is :suryosnata
salary is:35000

2nd way(Taking value from user)
   #include <iostream> 
   #include<conio.h>
    class Employee { 
   public: 
  void show()   
     {   
   int id;
  string name;
  float salary;
  cout<<"Enter id"<<endl;
  cin>>id;
  cout<<"Enter name"<<endl;
  cin>>name;
  cout<<"Enter salary"<<endl;
  cin>>salary;
  cout<<"id is"<<id<<"\n"<<"name is:"<<name<<" \n "<<"salary is:"<<salary<<endl;    
        }   
      }; 
  void main() { 
   clrscr();
    Employee e1; //creating an object of  Employee  class 
   e1.show();
    getch();  
      }
   Output:
     id is: 102
name is :suryosnata
salary is:35000

   3rd way(Using initialization of instance variable)
  #include <iostream.h> 
  #include<conio.h>
  class Employee { 
   public: 
       int id;    //instance variable
       string name; //instance variable
       float salary; //instance variable
       void show(int i, string nm, float sal)   
        {   
            id = i;   
            name = nm;   
            salary = sal; 
        cout<<"id is:"<<id<<"\n"<<"name is:"<<name<<"\n"<<"salary is:"<<salary<<endl;      
        }   
   }; 
   void main()
   { 
    Employee e1; //creating an object of Employee   class
     e1.show(102, "Suryosnata",35000);   
    getch();
    } 
Output:
id is: 102
name is :suryosnata
salary is:35000


2)Object is used to initialize the data members.
#include <iostream.h> 
#include<conio.h>
class Employee { 
   public: 
      int id;//data member 
      string name;//data member  
}; 
 void main() { 
    Employee e1; //creating an object of Employee class
    e1.id = 102;   
    e1.name = "suryosnata";  
    cout<<"id is:"<<e1.id<<endl; 
    cout<<"name is:"<<e1.name<<endl; 
getch();
} 

Output:
id is: 102
name is :suryosnata


No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...