AppDividend
Latest Code Tutorials

Class and Object in C++ Example Tutorial

0

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++

Class of animals, all animals can run, and they all have legs. So here running is behavior and legs are part of their characteristics. There are many different animals in this class with different names, but they all possess this behavior and characteristics.

Similarly, a class is just a blueprint, which 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 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 that has its data members (Variables) or Data functions (Methods). For example, a car is a class, and its structure, engines, get fuel, get 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 which can represent the class member or variable. The objects can access all the data members. So, in other words, we can say like 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.” 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 which form the body of the class.

Class class_name
{
   Variable declaration;
   Function declaration
};

There are Access specifiers that 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. The data members and member functions declared inside the public can be accessed by other classes too. 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 are not allowed to access any other functions or object of the program. Only Friend Function can access private data members.

#Protected access in C++:

Similar with 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 available are integer, character, Boolean, floating-point, void, etc. For each type, the keyword is used to declare. Here int, char is the data types that define 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 Bank account as a class and Account name, and the Account number is its members. When the above code is compiled and executed, it produces 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 who are called by the compiler every time the object of that class is instantiated. Constructors have the same name as the class and maybe defined inside or outside the class definition.
There are three types of constructors:

#C++ Destructors

Destructor is another special member function that is called by the compiler when the scope of the object ends.

#Classes and Objects in Detail

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

Sr.NoConcept & Description
1Class 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.

2Class Access Modifiers

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

3Constructor & 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 also a special function which is 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.

5Friend Functions

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

6Inline Functions

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

7this Pointer

Every object has the special pointer this which points to an object itself.

8Pointer to C++ Classes

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

9Static Members of Class

Both the data members and function members of a class can be declared as static.

Finally, Class and Object in C++ Example For Beginners is over.

Leave A Reply

Your email address will not be published.

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