×

Advanced Coding & Software Engineering Program

Duration: 1 Year (12 Months)

Join our premium 1-year program to master cutting-edge technologies and become an industry-ready Software Engineer!

Course Coverage

  • Languages: C, C++, Java, JavaScript, Python
  • Web Technologies: HTML, CSS, Bootstrap 5, MERN Stack, Full Stack Development
  • Databases: MySQL, MongoDB
  • Data Science Libraries: Pandas, NumPy
  • Development Tools: Visual Studio Code, IntelliJ IDEA, PyCharm, Postman, Git, GitHub
  • Cloud Platforms: Vercel, MongoDB Atlas

Program Highlights

  • Live Classes: Interactive sessions with real-time doubt resolution
  • Hands-On Sessions: Practical coding exercises to build real-world skills
  • Industry Experts: Learn from professionals with years of experience
  • Live Project: Work on real-world projects to apply your skills
  • Get Certificate: Earn a professional certificate upon program completion

Course Fee: Only ₹1020 / month
Limited Period Offer!

Classes and Objects in C++ – Complete Tutorial



Last Updated on: 11th Dec 2025 18:13:22 PM

C++ is an object-oriented programming language, and the fundamental building blocks of OOP are Classes and Objects.
Understanding these two concepts is essential for learning advanced OOP concepts like Inheritance, Encapsulation, Abstraction, and Polymorphism.

 

This tutorial gives you a deep, clear, and real-world explanation .

 

What is a Class in C++?

A class in C++ is a user-defined data type that works as a blueprint, template, or model for creating objects.
It groups data members (variables) and member functions (methods) into a single unit.

 

Key Points About Class

  • It does not occupy memory until an object is created.

  • It defines the properties and behaviors of an object.

  • It helps in encapsulating data and organizing code.

  • It allows you to create multiple objects from the same blueprint.

 

Formal Definition

“A class is a user-defined data type that binds data and functions together and provides an abstract representation of an entity.”

 

Real-Life Example of a Class

Consider a Car.

A class defines what every car should have:

 

Attributes (Data Members)

  • brand

  • color

  • price

  • speed

  • fuel type

 

Behaviors (Member Functions)

  • start()

  • stop()

  • accelerate()

  • brake()

No real car exists yet — this is only a design.
When you create an actual car using this design, that is an object.

 

Syntax of a Class in C++

class ClassName {
private:
    // private data and functions
public:
    // public data and functions
protected:
    // protected data and functions
};
  

 

Example of a Class

class Student {
public:
    string name;
    int age;

    void showInfo() {
        cout << "Name: " << name << endl;
        cout << "Age: " << age << endl;
    }
};
 

 

The Structure of a Class (Diagram)

 ---------------------------
|          Class            |
|---------------------------|
|  Data Members (Variables) |
|---------------------------|
|  Member Functions (Methods)|
 ---------------------------

 

What is an Object in C++?

An object is an instance of a class.
It is the real-world entity created using the class blueprint.
When a class is defined, no memory is allocated. Memory is allocated only when an object is created.

 

Key Points About Object

  • Object = real entity, class = design plan

  • Each object has its own copy of data members

  • Multiple objects can be created from one class

  • Objects interact using functions

  • Memory is allocated in stack or heap

 

Real-Life Example of an Object

From the Car class, objects can be:

  • Car car1 = Toyota, red, petrol

  • Car car2 = BMW, black, diesel

  • Car car3 = Tesla, white, electric

Every real car is an object with different data, but the underlying class is the same.

 

Creating Objects in C++

 

Syntax

ClassName objectName;
 

Example

Student s1;     // object 1
Student s2;     // object 2

 

Complete Example – Class and Object in C++

#include <iostream>
using namespace std;

class Student {
public:
    string name;
    int roll;

    void display() {
        cout << "Name: " << name << endl;
        cout << "Roll No: " << roll << endl;
    }
};

int main() {

    Student s1;  // Creating object s1
    s1.name = "Aman";
    s1.roll = 101;

    Student s2;  // Creating object s2
    s2.name = "Riya";
    s2.roll = 102;

    s1.display();
    s2.display();

    return 0;
}

 

How Objects Work in Memory (Very Important)

 

When you declare:

Student s1;

 

Memory is allocated like:

s1:
  name → memory allocated
  roll → memory allocated

 

Each object gets its own copy of data members but shares the same class functions.

 

Accessing Members Using Dot (.) Operator

 

Syntax

objectName.dataMember
objectName.memberFunction()

 

Example

s1.name = "Aman";
s1.display();

 

Types of Member Function Access

Type Meaning
 Public  Accessible anywhere
 Private  Accessible only inside class
 Protected  Accessible inside class + derived classes

 

Private Members and Public Functions (Encapsulation Basics)

Real-life example:
A Bank Account should not allow the user to directly modify the balance.

 

Example:

class Bank {
private:
    int balance;

public:
    void deposit(int amount) {
        balance += amount;
    }

    void showBalance() {
        cout << "Balance: ₹" << balance << endl;
    }
};

 

Constructor with Class and Object (Very Useful)

Constructors are automatically called when objects are created.

 

Example:

class Car {
public:
    Car() {
        cout << "Car object created!" << endl;
    }
};

 

Multiple Objects of One Class

Car c1, c2, c3;

 

All three objects share the same blueprint but have different data.

 

Array of Objects

Student s[3];

 

Useful in school-management, employee database, etc.

 

Objects as Function Arguments

You can pass an object to a function:

void display(Student s) { ... }

 

Returning Objects from Functions

Student getStudent() { ... }

 

 

Real-Life Use Cases of Class & Object in Software

Real-Life System Class Example Object Example
E-commerce Product Laptop, Mobile
Bank Account A/c 12345
Education System Student Rahul, Neha
Hospital System Patient Patient #101
Car Showroom Car BMW X5, Swift ZXI

 

Summary

  • A Class is a blueprint describing data + functions.

  • An Object is a real instance created from that blueprint.

  • Objects have their own data, but share class functions.

  • Access class members using the dot operator.

  • Memory is allocated only when object is created.

  • Class = Design, Object = Real entity.

 

Keep practicing — you're doing amazing!

Happy Coding!    yes


Online - Chat Now
Let’s Connect

Inquiry Sent!

Your message has been successfully sent. We'll get back to you soon!

iKeySkills Logo