Classes and Objects in C++

C++ is object-oriented. In object-oriented programming, a class is a template or blueprint to create an object with properties similar to a real world entity.

How to Create a Class?

In C++, a class can be created using the keyword class followed by the name of the class.

Syntax

class ClassName {
   
};
Example

class Student {
   
};

What is an Object in C++?

An object is a the real world entity having a state and behavior. A class provides the blueprint for objects, so objects are created from a class. Object is also known as an instance of a class.

How to Create an Object in C++?

In C++, an object can be created by declaring the class followed by the user-defined object name.

Syntax

ClassName objectName;
Example

class Student {
   
};

Student student1, student2; // Creating two objects

Here in the above example, we have created two objects of Student class.

Class Members

In C++, a class may contain the following:

  1. Data members or variables — Data member are the variables declared inside a class.
  2. Example
    
    #include <string>
    using namespace std;
    
    class Student {
      
    string firstName; 
    string lastName;
    int id;
    
    };
    
  3. Access-modifiers — Access-modifiers are used to restrict access to members of a class. In C++, there are public, private, and protected access-modifiers.
  4. Example
    
    #include <string>
    using namespace std;
    
    class Student {
    
    public:  
    string firstName;
    string lastName;
    
    private:
    int id;
    
    protected:
    bool deleted;
    
    };
    
  5. Member functions — Member functions are standard functions declared inside a class. Member functions are used to access, modify or use the data members of a class. You can access or invoke these functions using the dot operator on the object. For example, if the name of an object is obj1 and you want to access the member function with the name printName() then you will have to write obj1.printName().
  6. There are 2 ways to define a member function:

    1. Inside Class definition — This approach is usually used for small functions. We can define a member function inside the class directly without declaring it first in class.
    2. Example
      
      #include <bits/stdc++.h>
      using namespace std;
      
      class Calculator
      {
      public:
          // inside class definition of the function 
          void sum(int num1, int num2) // function header
          { // function body
              cout << "The sum of the numbers is: ";  
              cout << (num1 + num2) << "\n\n";   
          }
      };
      
      int main()
      {
          // create an object of the class
          Calculator calcObj;
          // call the member function
          calcObj.sum(5, 10);
          return 0;
      }
      
      Output
      The sum of the numbers is: 15
    3. Outside Class definition — In this approach, member functions are defined outside the class, even though it is defined outside the class we need to declare it first inside the class and then define it outside the class.
    4. Syntax
      
      ReturnType ClassName :: MemberFuntion(){
          
      }
      
      Example
      
      #include <bits/stdc++.h>
      using namespace std;
      
      class Calculator
      { 
      public: //access modifier
          void sum(int num1, int num2); //declare the member function
      }; 
      
      //definition of the function outside class
      void Calculator :: sum(int num1, int num2) //function header
      {
          cout << "The sum of the numbers is: "; //function body
          cout << (num1 + num2) << "\n\n";
      } 
      
      
      int main()
      {
          Calculator calcObj; //create an object of the class
          calcObj.sum(15, 10); //call the member function
          return 0;
      }
      
      Output
      The sum of the numbers is: 25
    Example

    In the example below, we are creating a Student class with private and public access-modifiers, three variables and functions. The example also shows how to create three objects of Student class and call the member functions using the class object:

    
    #include <string>
    #include <bits/stdc++.h>
    using namespace std;
    
    class Student {
    
    private:
      string firstName;
      string lastName;
      int id;
    
    public:
      string getFirstName();
      string getLastName();
      int getId();
      void setFirstName(string firstName);
      void setLastName(string lastName);
      void setId(int id);
    };
    
    string Student :: getFirstName(){
    	return this->firstName;
    }
    
    string Student :: getLastName(){
    	return this->lastName;
    }
    
    int Student :: getId(){
    	return this->id;
    }
    
    void Student :: setFirstName(string firstName){
    	this->firstName=firstName;
    }
    
    void Student :: setLastName(string lastName){
    	this->lastName=lastName;
    }
    
    void Student :: setId(int id){
    	this->id=id;
    }
    
    int main(){
      //Creating three objects
      Student student1, student2, student3;
     
      //calling functions on objects
      student1.setFirstName("Jake");
      student1.setLastName("A");
      student1.setId(1);
    
      student2.setFirstName("Jay");
      student2.setLastName("B");
      student2.setId(2);
    
      student3.setFirstName("Ryan");
      student3.setLastName("C");
      student3.setId(3);
    
      cout<<"Student 1 firstName "<<student1.getFirstName() <<"\n";
      cout<<"Student 1 lastName "<<student1.getLastName() <<"\n";
      cout<<"Student 1 id "<<student1.getId() <<"\n";
    
      cout<<"Student 2 firstName "<<student2.getFirstName() <<"\n";
      cout<<"Student 2 lastName "<<student2.getLastName() <<"\n";
      cout<<"Student 2 id "<<student2.getId() <<"\n";
    
      cout<<"Student 3 firstName "<<student3.getFirstName() <<"\n";
      cout<<"Student 3 lastName "<<student3.getLastName() <<"\n";
      cout<<"Student 3 id "<<student3.getId() <<"\n";
    
    }
    
    Output
    Student 1 firstName Jake
    Student 1 lastName A
    Student 1 id 1
    Student 2 firstName Jay
    Student 2 lastName B
    Student 2 id 2
    Student 3 firstName Ryan
    Student 3 lastName C
    Student 3 id 3