In this C++ tutorial, you will learn what classes and objects are, how to define a class, create objects, access data members and member functions, use access specifiers, and initialize objects with constructors.

What is a Class in C++?

A class in C++ is a user-defined type that groups related data and functions into a single unit. The data represents the state of an object, while the functions define the operations that can be performed on that state.

For example, a Student class can store a student’s name, roll number, and section, and it can also provide a function to display those details.

A class is a central feature of object-oriented programming in C++. It provides a way to define your own type and control how its data is accessed.

C++ Class Structure

Following code snippet illustrates the high level structure of a class in C++.

</>
Copy
class ClassName {
   //attributes
   //methods
};

The class keyword starts a class definition. ClassName is the name of the new type. The curly braces contain the class members, and the class definition ends with a semicolon.

A class may contain data members, also called attributes, and member functions, also called methods. Access specifiers such as public, private, and protected control where those members can be accessed.

C++ Class Attributes and Data Members

Attributes are variables declared inside a class. In C++ terminology, they are commonly called data members. Each ordinary object of the class has its own values for these members.

Following class defines a class named Student, with three attributes.

</>
Copy
class Student {
   string name;
   int rollno;
   int section;
};

Here, name, rollno, and section are data members of the Student class. Because no access specifier appears before them, they are private by default.

C++ Class Methods and Member Functions

Functions declared inside a class are called member functions or methods. They can work with the data members of the object on which they are called.

Following example, defines a class named Student with method printDetails().

</>
Copy
class Student {
   //attributes
   string name;
   int rollno;
   int section;

   //methods
   void printDetails(){
      cout << "Name : " << name << endl;
      cout << "Roll Number : " << name << endl;
      cout << "Section : " << name << endl;
   }
};

The method is declared inside the class body, so it is a member function of Student. A member function can directly refer to members of the current object.

C++ public, private, and protected Access Specifiers

By default private is the access modifier of class members, be it attributes or methods. Meaning, you cannot access the class members outside the class. So, if you would like to modify the access to specific modifier(s) or methods, place the required access specifier before those variables or methods as shown below.

In the following example, we have defined a class with name and rollno attributes with public access. section attribute has a private access. Then the method printDetails() is given public access using the public access modifier.

</>
Copy
class Student {
   public:
   string name;
   int rollno;

   private:
   int section;

   public:
   void printDetails(){
      cout << "Name : " << name << endl;
      cout << "Roll Number : " << name << endl;
      cout << "Section : " << name << endl;
   }
};

An access specifier remains in effect for the members that follow it until another access specifier appears or the class definition ends.

  • public members can be accessed from outside the class through an object.
  • private members can be accessed directly only by the class itself and its friends.
  • protected members behave like private members for ordinary outside code but are also accessible to derived classes.

For a C++ class, the default member access is private. This differs from a C++ struct, where the default member access is public.

What is an Object in C++?

An object is an instance of a class. The class defines the type, while each object stores its own state according to that definition.

If Student is a class, then student_1 and student_2 can be two separate objects of that class. Their data members can contain different values even though both objects have the same class type.

Creating an Object of a C++ Class

To create a class object, you have to declare a variable with the class type, just like you declare an integer with variable name and int datatype.

In the following, example, we shall define a class named Student and create an object for this class in the main method.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

class Student {
   public:
   string name;
   int rollno;
   int section;

   public:
   void printDetails(){
      cout << "Name : " << name << endl;
      cout << "Roll Number : " << name << endl;
      cout << "Section : " << name << endl;
   }
};

int main() {
   //create class object
   Student student_1;
}

An object with variable name student_1 of type Student has been created.

The general syntax for creating an object of a class is shown below.

</>
Copy
ClassName objectName;

Accessing C++ Object Members with the Dot Operator

After creating object, you can access the class members using dot operator. You can either assign a value to the attribute, or read the value. When it comes to methods, you can just call them.

Only members that are accessible from the current scope can be used this way. For example, code outside the class cannot directly access a private member.

In the following example, we have defined a class Student, created an object of type Student, assigned values to the attributes of the object and made a call to the member function.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

class Student {
   public:
   string name;
   int rollno;
   int section;

   public:
   void printDetails(){
      cout << "Name : " << name << endl;
      cout << "Roll Number : " << rollno << endl;
      cout << "Section : " << section << endl;
   }
};

int main() {
   //create class object
   Student student_1;

   //modify class object attributes
   student_1.name = "Angel";
   student_1.rollno = 32;
   student_1.section = 3;

   //call functions on class objects
   student_1.printDetails();
}

Output

Name : Angel
Roll Number : 32
Section : 3

The expressions student_1.name, student_1.rollno, and student_1.section refer to data members of that particular object. The expression student_1.printDetails() calls its member function.

Multiple Objects of the Same C++ Class

You can create any number of objects from the same class definition. Each object normally has its own copy of the non-static data members.

</>
Copy
#include <iostream>
#include <string>
using namespace std;

class Student {
public:
    string name;
    int rollno;
};

int main() {
    Student student_1;
    Student student_2;

    student_1.name = "Angel";
    student_1.rollno = 32;

    student_2.name = "Ravi";
    student_2.rollno = 18;

    cout << student_1.name << " - " << student_1.rollno << endl;
    cout << student_2.name << " - " << student_2.rollno << endl;
}

Output

Angel - 32
Ravi - 18

Changing the name or rollno of one object does not change the corresponding members of the other object.

C++ Class Constructors

You can assign values to class attributes while creating a class object. This can be done using constructors of class. Constructors are methods that does not return a value and have the same name as that of Class.

More precisely, a constructor is a special member function that is called when an object is initialized. It has the same name as the class and has no return type, not even void.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

class Student {
   public:
   string name;
   int rollno;
   int section;

   //constructor
   Student(string x, int y, int z) {
      name = x;
      rollno = y;
      section = z;
   }

   public:
   void printDetails(){
      cout << "Name : " << name << endl;
      cout << "Roll Number : " << rollno << endl;
      cout << "Section : " << section << endl;
   }
};

int main() {
   //create class object with call to constructor
   Student student_1("Angel", 32, 3);

   //call functions on class objects
   student_1.printDetails();
}

Output

Name : Angel
Roll Number : 32
Section : 3

The constructor receives three arguments and assigns them to the object’s data members before the object is used.

Default and Parameterized Constructors in C++

A constructor that can be called with no arguments is commonly called a default constructor. A constructor that accepts arguments can be used to initialize an object with specific values.

</>
Copy
#include <iostream>
#include <string>
using namespace std;

class Student {
public:
    string name;
    int rollno;

    Student() {
        name = "Unknown";
        rollno = 0;
    }

    Student(string studentName, int studentRollno) {
        name = studentName;
        rollno = studentRollno;
    }
};

int main() {
    Student student_1;
    Student student_2("Angel", 32);

    cout << student_1.name << " - " << student_1.rollno << endl;
    cout << student_2.name << " - " << student_2.rollno << endl;
}

Output

Unknown - 0
Angel - 32

This class has two constructors with different parameter lists. This is constructor overloading.

C++ Constructor Member Initializer List

C++ also provides a member initializer list for initializing data members before the constructor body executes. It is commonly preferred for direct initialization and is required for some kinds of members, such as references and const data members.

</>
Copy
#include <string>
using namespace std;

class Student {
private:
    string name;
    int rollno;

public:
    Student(string studentName, int studentRollno)
        : name(studentName), rollno(studentRollno) {
    }
};

Defining C++ Member Functions Outside the Class

A member function does not have to be fully defined inside the class body. You can declare it in the class and define it later using the scope resolution operator ::.

</>
Copy
#include <iostream>
using namespace std;

class Rectangle {
public:
    int width;
    int height;

    int area();
};

int Rectangle::area() {
    return width * height;
}

int main() {
    Rectangle box;
    box.width = 5;
    box.height = 4;

    cout << box.area() << endl;
}

Output

20

The definition Rectangle::area() tells C++ that area() belongs to the Rectangle class.

Keeping C++ Class Data Private

A common class design is to keep data members private and provide public member functions for controlled access. This prevents outside code from directly changing internal state in ways the class does not allow.

</>
Copy
#include <iostream>
using namespace std;

class BankAccount {
private:
    double balance;

public:
    BankAccount(double initialBalance) : balance(initialBalance) {
    }

    void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }

    double getBalance() const {
        return balance;
    }
};

int main() {
    BankAccount account(1000);
    account.deposit(250);

    cout << account.getBalance() << endl;
}

Output

1250

The balance member cannot be modified directly from main(). Instead, the class exposes operations such as deposit() and getBalance(). This is a basic example of encapsulation.

C++ Class and Object Relationship

The difference between a class and an object can be summarized as follows.

ClassObject
Defines a user-defined type.Is an instance of that type.
Describes data members and member functions.Contains the state associated with its non-static data members.
Is declared with the class keyword.Is created by declaring or initializing a variable of the class type.
Acts as the common definition for objects of that type.Can hold values different from other objects of the same class.

C++ Classes and Objects: Key Rules

  • A C++ class defines a user-defined type containing data and functions.
  • An object is an instance of a class.
  • Class definitions end with a semicolon after the closing brace.
  • Members of a class are private by default.
  • Public members can be accessed through an object using the dot operator.
  • Private members cannot be accessed directly from ordinary code outside the class.
  • Constructors initialize objects and have the same name as the class.
  • A class can have multiple constructors with different parameter lists.
  • Member functions can be defined inside the class or outside it with the :: operator.
  • Keeping implementation data private and exposing suitable public operations is a common way to encapsulate class state.

C++ Classes and Objects Editorial QA Checklist

  • Verify that every class definition ends with the required semicolon.
  • Check whether data members are intentionally public, private, or protected.
  • Confirm that examples do not access private members directly from outside the class.
  • Check that each constructor has the same name as its class and specifies no return type.
  • Verify that constructor arguments initialize the intended object members.
  • Check that member functions defined outside the class use the correct ClassName::functionName qualification.
  • Confirm that examples with multiple objects demonstrate independent object state correctly.
  • Prefer member initializer lists when an example needs to initialize const members, reference members, or members that should be constructed directly.

Summary of C++ Classes and Objects

In this C++ Tutorial, we learned how classes define user-defined types, how objects are created from those classes, how data members and member functions work, how access specifiers control visibility, how constructors initialize objects, and how private data can be exposed safely through public member functions.