Class and Object in C++: The Complete Guide

0
354
Class and Objects in C++ Tutorial With Example For Beginners

C++ is an object-oriented programming language. Even though it’s not pure, c++ language mainly deals with the OOPS concept. The classes and objects are the most important feature of C++ that leads to Object-Oriented programming.

Class is the user-defined data type, which holds its data members and member functions, which can be accessed and used by creating the instance of that Class called objects. The variables inside the class definition are called data members, and the functions are called member functions.

Class and Object in C++

A class is just a blueprint that declares and defines characteristics and behavior, namely the data members and member functions, respectively. And all the objects of this Class will share these characteristics and behavior.

Class of animals: All animals can run and have legs. So, running is behavior, and legs are part of their characteristics. Many animals in this Class have different names, but they all possess this behavior and characteristics.

Class in C++

A class can be mainly defined as a pillar or building block of object-oriented programming language. It’s a user-defined data type with data members (Variables) or Data functions (Methods). For example, a car is a class, and its structure, engines, fuel, and speed are its variables and members. When a class is created, it does not allocate memory.

Object in C++

An object can be defined as an instance of a class or piece of code representing the class member or variable. The objects can access all the data members.

So, in other words, we can say that a Class is a group of objects. As per the requirement, an object can be created many times where a class is declared once. When the object is created, it allocates memory.

Declaring Class And Objects

The Class can be declared using the keyword “class.” For example, see the following syntax of Class in c++.

Syntax

Class class_name

Here Class is the keyword, and the class name is user-defined. The variables and members can be declared inside the curly brackets that form the Class’s body.

Class class_name
{
   Variable declaration;
   Function declaration
};

Access specifiers can be declared inside the Class for the data members. They give some restrictions for other functions not to access the class members. They are as follows:

Public access in C++

Data members are available to everyone. Other classes can also access the data members and member functions declared inside the public. It can be accessed from anywhere in the program.

Private access in C++

Data members can be accessed only by the functions inside the Class. Private members cannot access any other functions or objects of the program. Only Friend Function can access private data members.

Protected access in C++:

Similar to Private but can be accessed by the subclass.

Class class_name
{
  Private:
   Variable declaration;
   Function declaration;
  Public:
   Variable declaration;
   Function declaration;
  Protected:
   Variable declaration;
   Function declaration;
};

Declaring Class Members

All data members or variables are declared along with the data type. Data types are nothing but the type of variable declared. Data types are integer, character, Boolean, floating-point, void, etc. For each type, the keyword is used to declare. For example, int, char is the data type that defines integer and character values.

Class class_name
{
 int i;
 char name;
};

After declaring the Class and its variables, objects can be created.

Declaring Class Member Functions

When we declare a function within the class definition, it has its function like any other variable. It can access all the data members, and the object can operate on it.

class class_name 
{
  public:
   int i;
   int j;
  int getValue(void) //Declaring function
  {
   return i * j;
  }
}

Declaring Objects in C++

By creating an object, it holds all the data defined inside the specified Class and allocates memory. The object can access the data members using the dot(.) operator.

Syntax

Class_name Object_name;

Real-Time Example For Class And Objects

Consider a Bank account as a class and Account name, and the Account number is its members. The above code is compiled and executed to produce the following result.

#include<iostream>
using namespace std;
class Bankaccount
{
   public:
      int accnumber,accbalance;
       int display()
        {
        cout<<"Account number is: "<<accnumber;
        cout<<"\nAccount balance is: "<<accbalance;
        }
};
int main() {
   Bankaccount a;
   Bankaccount b;

   a.accnumber = 123456;
   a.accbalance =50;

   b.accnumber = 67890;
   b.accbalance = 2000;
   cout<<"Account details of A\n\n"<<endl;
   a.display();
    cout<<"\nAccount details of B\n\n";
    b.display();
   return 0;
}

See the following output.

Class and Objects in C++ Tutorial

C++ Constructors

Constructors are special class members called by the compiler every time the object of that Class is instantiated. Constructors have the same name as the Class and may be defined inside or outside the class definition.

There are three types of constructors:

C++ Destructors

Destructor is another special member function called by the compiler when the object’s scope ends.

Classes and Objects in Detail

So far, you have a fundamental idea about C++ Classes and Objects. There are other interesting concepts related to C++ Classes and Objects, which we will discuss in various sub-sections listed below.

Sr.No Concept & Description
1 Class Member Functions

A member function of the Class is the function that has its definition or its prototype within a class definition like any other variable.

2 Class Access Modifiers

A class member can be defined as public, private, or protected. By default, the members would be assumed as private.

3 Constructor & Destructor

A class constructor is a special function in the Class that is called when the new object of the Class is created. The destructor is a special function called when the created object is deleted.

4

Copy Constructor

A copy constructor is a constructor that creates the object by initializing it with the object of the same Class which has been created previously.

5 Friend Function

The friend function is permitted full access to private and protected members of a class.

6 Inline Functions

With the inline function, the compiler tries to expand the code in the function’s body in place of a call to the function.

7 this Pointer

Every object has a special pointer, “this”, which points to an object itself.

8 Pointer to C++ Classes

A pointer to a class is done the same as a pointer to a structure. A class is just a structure with functions in it.

9 Static Members of Class

Both a class’s data and function members can be declared static.

That’s it for this tutorial.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.