C++ Classes and Objects

Last Updated : 27 Aug, 2026

In Object Oriented Programming, classes and objects are basic concepts 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.
classes

C++ Classes

A class in C++ is a user-defined data type that combines data members and member functions into a single unit. It acts as a blueprint for creating objects and helps organize code in an object-oriented way.

  • Data Members: Variables inside the class used to store data.
  • Member Functions: Functions inside the class used to perform operations on the data.

Creating a Class

A class must be defined before creating its objects. In C++, a class is declared using the class keyword.

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

class Car {
public:
    string brand;

    void display() {
        cout << "Car Brand: " << brand;
    }
};

int main() {
    Car car1;

    car1.brand = "Toyota";
    car1.display();

    return 0;
}

Output
Car Brand: Toyota

Explanation: In the above program defines a Car class containing a data member brand and a member function display() to print the car brand. In the main() function, an object car1 is created, the brand is assigned as "Toyota", and the display() function is called to show the output.

C++ Objects

An object in C++ is an instance of a class created to access the data members and member functions defined inside the class. Each object has its own separate copy of data members while sharing the common structure and behavior defined by the class.

  • State: Represents the values stored in data members of the object.
  • Behavior: