C++ Classes & Objects

    C++ is an extension of the C programming language, and the Object-Oriented Programming (OOPs) concept made C to C++. There are many properties of Object Orientation Programming and class & objects are the core fundamentals we use to implement the OOP’s concepts.

    Class in C++

    A class is a Data Structure and a building block that play an important role to implement the concepts of Object-Oriented Programming. It can represent a group of members, and it is a technique that helps to bind the data and functions which describe the class entity. Let’s understand the implementation of class with a real-world example, consider a bank_account as a class which can bind data such as account_number, account_balance, and account_type, the bank_account also bind functions such as cash_withdrawl and cash_deposite. Note : In class, the functions are known as class methods or just methods .

    • Class is a user-defined data structure that can hold multiple members, such as data and function members.
    • The data members are the data variables inside the class.
    • The function members or methods are the user-defined functions inside the class.
    • In the bank_account example account_number, account_balance, and account_type are data member and cash_withdrawl and cash_deposite are member functions.

    Class Syntax

    class Class_Name
    {
     class members
    } ;

    Object

    An object is the instance of the class. If we consider the bank_account example we can say that a bank account is a concept or blueprint which come in existence when a customer register for a bank account, similarly the class is also a blueprint which comes in existence when its object gets created. Like a bank can have multiple back accounts similarly a class can have multiple objects.

    • The object is used to initialize the class.
    • With the help of the objects, we can access the class public (access modifiers) properties (class members).
    • To declare an object, we use the class name.
    • To access the class properties, we use the dot operator along with the class object.

    Object declaration Syntax

    Class_Name objet_name;

    Accessing Data members of Class

    To access the class members outside the class we use the class object name and dot(.) operator. We are only allowed to access those class members which are under the public access modifiers. To protect the class data we have the access modifiers and there are three access modifiers in C++ class public, private and protected.

    • By default, if we do not mention any access modifier in class the class follow the private access modifier.
    • If we mention the public: access modifier in the class then all the statements beneath the public: statement can be accessed outside the class.
    • protected: access modifier acts as private : modifier for the objects which are outside the class but it acts as a public modifier for the class which inherit.

    Example: class object and access modifiers

    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    class bank_account
    {
       private:   // access modifier
       //we can not access account_balance ouside the calss because it is under private access modifier
       int account_balance=0; //initial balance is 0                                
       public:
         //we can only access these members using the objectname and dot operator
          void cash_deposite(int deposite)
            {
                  account_balance+=deposite;
                  cout<<"You have deposited: "<<deposite;
                  cout<<endl;
                  cout<<"Current balance is: "<<account_balance;
                  cout<<endl;
            }
    
           void cash_withdrawl(int withdraw)
            { 
                 if(withdraw< account_balance)
                    {
                      account_balance-=withdraw;
                      cout<<"\nYou withdraw: "<<withdraw <<" ammount";
                      cout<<endl;
                      cout<<"Current balance is: "<<account_balance;
                    }
                 else
                    {
                      cout<<"Account does not have enough balance";
                    }      
             }
    
    };
    
    int main()
    {
       bank_account joya; // create object joya
       joya.cash_deposite(100); // accessing the cash_deposite() method using object name and dot(.) operator
       joya.cash_deposite(400); // deposite 400 extra in Joya account
       joya.cash_withdrawl(200);
       return 0; 
    }

    Output:

    You have deposited: 100
    Current balance is: 100
    You have deposited: 400
    Current balance is: 500
    You withdraw: 200 amount
    Current balance is: 300

    Behind the code In this above example we have made the account_balance private and cash_withdrawl() and cash_deposite() public because we only want the user to have control over the withdraw and deposit methods. Using the bank_account joya; statement we create an object joya and using the joya object we access the cash_deposite() and cash_withdrawl() methods.

    Class Constructor

    A constructor is the special class function member or method which execute automatically when the object of the class gets created.

    Properties of the Constructor

    • The constructor is the special member function
    • It should define under the public access modifier
    • It should not return any value not even void
    • The name of the constructor should be similar to the class name.
    • The constructor is basically used to initialize the object properties.

    Constructor Syntax

    class Class_Name
    {
    public:
    Class_name()
       {
       }
    };
    Example:
    #include <iostream>
    using namespace std;
    
    class SignUp
     {                             
       public:
          SignUp() //constructor
            {
                cout<<"New user has been signed up\n";
            }
    };
    
    int main()
      {
         SignUp sam;
         SignUp monika;
         return 0; 
      }

    Output

    New user has been signed up
    New user has been signed up

    Behind the Code In this, above example, we have created two objects of SignUp class sam and monika, and we are not calling the SingUp() function yet the statement cout<<"New user has been signed up\n"; is executing automatically, this signifies that every time a new object is created the constructor invokes automatically.

    Destructor

    Destructor work opposite of Constructor, and it is used to delete the object and its properties.

    Destructor properties:

    • It is also a class special method which is used to destroy the object.
    • The destructor method name should be similar to the class name with a tilde(~) symbol.
    • Like constructor destructor should not have any return type.
    • Destructor should be defined under public: access modifier.
    • It takes no argument value
    • It called by the compiler automatically when the object is destroyed.

    Destructor Syntax

    class Class_Name
    {
    public:
    ~Class_name() //destructor
       {
        
       }
    };

    Example

    #include <iostream>
    using namespace std;
    class Sample
    {                             
       public:
           ~Sample()    //destructor
              {
                  cout<<"Object has been destroyed";
              }
    };
    
    int main()
    {
       Sample test;
       return 0; 
    }

    Output

    Object has been destroyed

    Behind the code The compiler automatically called the destructor method ~Sample() when there is no need for object.

    Class and Object Quick Summary

    • Class and objects are used to implement Object Oriented Programming concepts in C++.
    • Class is the blueprint of Object which has no existence until its object gets created.
    • A class can have more than one object.
    • When we create an object then only the compiler allocates memory to the class structure.
    • A constructor is a special method that shears the same name as the class and gets invoked when the class object is created.
    • Destruction is also called automatically by the compiler when there is no need for the object.

    People are also reading: