The Wayback Machine - https://web.archive.org/web/20261127235619/https://www.geeksforgeeks.org/cpp/c-classes-and-objects/

C++ Classes and Objects

Last Updated : 27 Nov, 2026

Classes and objects are used to organize data and behavior in object-oriented programming. A class defines a user-defined type, while an object is an instance of that type.

  • Classes support access control using public, private, and protected.
  • Multiple objects can store independent data while using the same member functions.
classes

Classes

A class contains data members and member functions that define its properties and behavior.

  • Data members are variables used to store information.
  • Member functions perform operations on the class data

Creating a Class

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

class ClassName {

public:
data_type member;
void functionName() {
// Function body
}
};

Here, public allows the specified members to be accessed from outside the class. Class members are private by default.

Objects

An object is an instance of a class with its own state and identity. Non-static data members belong to individual objects, while static data members are shared among objects of the class.

  • State represents the values stored in an object's data members.
  • Behavior represents the operations performed using member functions.
  • Identity distinguishes an object from other objects.
dog_object
C++ Objects

Example: Objects in C++ represent real-world entities such as cars, students, employees, or circles. Memory for objects is allocated when they are created.

Creating and Initializing Objects

Objects can be initialized using constructors, which set their initial values when they are created.

Syntax:

ClassName objectName(constructorArguments);

Accessible class members can be accessed using the dot operator (.).

objectName.dataMember;
objectName.memberFunction();

Example: Program to demonstrates class creation, object initialization, member access, and modification.

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

class Dog{
public:
    string name;
    string breed;

    // Constructor
    Dog(string n, string b) : name(n), breed(b) {}

    // Member function
    void display() const{
        cout << "Name: " << name
             << ", Breed: " << breed << endl;
    }
};

int main(){
    
    // Create and initialize objects
    Dog tuffy("Tuffy", "Papillon");
    Dog buddy("Buddy", "Beagle");

    // Access member functions
    tuffy.display();
    buddy.display();

    // Modify a data member
    tuffy.name = "Tommy";

    cout << "Updated Name: " << tuffy.name << endl;
    cout << "Buddy Name: " << buddy.name << endl;

    return 0;
}

Output
Name: Tuffy, Breed: Papillon
Name: Buddy, Breed: Beagle
Updated Name: Tommy
Buddy Name: Buddy

Explanation:

  • The Dog class defines two data members, a constructor, and a member function.
  • tuffy and buddy are objects initialized with different values.
  • The display() function prints each object's details.
  • Updating tuffy.name does not affect buddy.name because the objects have separate non-static data members.
Comment