Open In App

C++ Classes and Objects

Last Updated : 21 Oct, 2025
Comments
Improve
Suggest changes
978 Likes
Like
Report

In Object Oriented Programming, classes and objects are basic concepts of that are used to represent real-world concepts and entities.

  • A class is a template to create objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects.
  • An object is an instance of a class. For example, the animal type Dog is a class, while a particular dog named Tommy is an object of the Dog class.
class

C++ Classes

A class is a user-defined data type, which holds its own data members and member functions that can be accessed and used by creating an instance of that class. A C++ class is like a blueprint for an object.

Create a Class : A class must be defined before its use. C++ class is defined using the keyword class keyword.

C++
#include <iostream>
using namespace std;

// Class definition
class GfG
{
  public:
    // Data member
    int val;

    // Member function
    void show()
    {
        cout << "Value: " << val << endl;
    }
};

int main()
{

    // Object will declareed here
    return 0;
}

In the above, GfG class is created with a data member val and member function show(). Here, member function is defined inside the class, but they can also be just declared in the class and then defined outside using scope resolution operator ::

The above is called class definition or template.

C++ Objects

When a class is defined, only the specification (attributes and behaviour) for the object is defined. No memory is allocated to the class definition. To use the data and access functions defined in the class, we need to create its objects.

Objects are the actual entities that are created as an instance of a class. There can be as many objects of a class as desired. For example, in the above, we discussed the class of Cars. If we create an actual car based on the properties of the Car class, the car we made is the object of that class.

Create Object : Once the class is defined, we can create its object in the same way we declare the variables of any other inbuilt data type.

  • An object is created by declaring it with the class name followed by the object name, like className objectName;.

Member Access : Members of the class can be accessed inside the class itself simply by using their assigned name.

  • To access them outside, the (.) dot operatoris used with the object of the class.
  • Class members are accessed through the object using object.member for variables and object.member() for functions.

Local Class

Classes are generally declared in global scope and are accessible to every function or other classes once they are defined. But C++ also provides facility to define a class within a function. It is called local class in C++ and is only accessible in that function.

Nested Class

A nested class is a class defined within another enclosing class. As a member of the enclosing class, it has the same access rights as any other member. The members of the enclosing class do not have special access to the members of the nested class; the standard access rules apply.

C++
#include <iostream>
using namespace std;

class Outer
{
  public:
    // Nested class inside Outer
    class Inner
    {
      public:
        void display()
        {
            cout << "This is the Inner class" << endl;
        }
    };

    void show()
    {
        cout << "This is the Outer class" << endl;
    }
};

int main()
{
    // Creating object of Outer class
    Outer outerObj;
    outerObj.show();

    // Creating object of Inner class using Outer
    Outer::Inner innerObj;
    innerObj.display();

    return 0;
}

Output
This is the Outer class
This is the Inner class

Enum Class

Enum classes in C++ are a safer and more organized way of using enums. They help to group related constants together while avoiding naming problems and ensuring better type safety.

Class vs Object

The following table lists the primary differences between the classes and objects in C++:

ClassObject
A blueprint or template for creating objects.An instance of a class with actual values.
No memory is allocated for a class until an object is created.Memory is allocated when an object is created.
Conceptual entity describing structure and behaviour.A real-world entity created from the class.
Defines properties and functions common to all objects of that type.Stores specific data and manipulates it using class functions.
Represents a general concept or type.Represents a specific instance of the class.

Explore