Classes and Objects in C++

C++ Class

  • Class is a collection of data and function under a single name.
  • Class is similar to function with only difference that it can have functions besides data items.
  • It is a blueprint for objects.

  • Syntax:
    class class_name
    {
         Access specifier:
             Data members;
             Member functions(){}
    };

    Example

    class employee
    {
         public:
             int empid;
             string empname;
             float salary;
    };


  • Class definition starts with the keyword class followed by the class name and ends with a semicolon (;).
  • The primary purpose of a class is to hold data/information.

C++ Object

  • Object is an instantiation of a class.
  • It has same relationship to class as variable has to the data type.
  • Object is created from a class.

  • Syntax:
    class_name variable_name;

    Example

    Consider the above Employee class. Object for Employee class can be defined as:

    Employee e;

    Here, object e of Employee class is defined.


  • It is exactly the same sort of declaration that we do for the variables of different data types.

Example : Following example demonstrates the working of Objects & Class in C++

#include <iostream>
using namespace std;
class Employee
{
     private:
          int empid;
          string empname;
          float salary;
     public:
          int emp_details()
          {
               empid=100;
               empname="ABC";
               salary=10000.0;
          }
          int show()
          {
               cout<<"Employee Id : "<<empid<<endl;
               cout<<"Employee Name : "<<empname<<endl;
               cout<<"Employee Salary : "<<salary<<endl;
          }
};
int main()
{
    Employee e;
    e.emp_details();
    e.show();
    return 0;
}


Output:
Employee Id : 100
Employee Name : ABC
Employee Salary : 10000

  • In the above program, there are three data members empid, empname & salary. Two member functions emp_details() & show() are defined under Employee class.
  • Object e of the Employee class is declared. Function emp_details() for the object e is executed using code e.emp_details() which includes details of the employee. Then, function show() for the object e is executed which displays details of the employee and returns it to the calling function.

Class v/s Structure

Following is the difference between Class and Structure:

ClassStructure
Class is a reference type.Structure is a value type.
In class, object is created on the heap memory.In structure, object is created on the stack memory.
It supports inheritance.It does not support inheritance.
It includes all types of constructors and destructors.It includes only parameterized constructors.
Object can be created using new keyword.
For eg. Test t = new Test();
Object can be created without using the new keyword.
For eg. Test t;
The member variable of class can be initialized directly.The member variable of structure cannot be initialized directly.