Afzal Badshah, PhD

Classes and Objects in C++: Beginner’s Guide with Real-Life Examples

Programming is about solving real-life problems. Imagine you’re designing a software to manage a car showroom, or a student database. In both cases, you deal with real-world entities like cars and students. Each of these has data (like name, color, roll number) and behavior (like start the car, register a course).

To represent such entities in programming, we use two powerful tools:

Let’s break this down step by step.

What is a Class?

A class is a template or blueprint that describes what data (attributes) and what actions (functions/methods) an object of that type will have.

In real life:
Class is like a design of a car. You define what all cars will have: engine, color, model, and what they can do: start, stop, accelerate.

In code, it looks like this (C++):

#include <iostream>
using namespace std;

class Car {
public:
    // Attributes (data)
    string color;
    string model;
    int year;

    // Function (behavior)
    void startEngine() {
        cout << "Engine started!" << endl;
    }

    void displayDetails() {
        cout << "Model: " << model << ", Year: " << year << ", Color: " << color << endl;
    }
};
Example of Class and its Objects

Explanation:

What is an Object?

An object is a real instance of a class. It’s like building an actual car from the design.

You can create multiple cars (objects) from one class.

Difference between classes and objects:

#include <iostream>
using namespace std;

int main() {
    // Create object car1
    Car car1;
    car1.color = "Red";
    car1.model = "Toyota Corolla";
    car1.year = 2022;
    car1.startEngine();
    car1.displayDetails();

    // Create another object car2
    Car car2;
    car2.color = "Blue";
    car2.model = "Honda Civic";
    car2.year = 2021;
    car2.startEngine();
    car2.displayDetails();

    return 0;
}

Output:

Engine started!
Model: Toyota Corolla, Year: 2022, Color: Red
Engine started!
Model: Honda Civic, Year: 2021, Color: Blue

Key points:

Structure of a Class

Every class has:

Let’s now apply this to another example: a Student.

#include <iostream>
using namespace std;

class Student {
public:
    string name;
    int rollNumber;
    string course;

    void introduce() {
        cout << "Hello, my name is " << name << " and I study " << course << endl;
    }
};

Now create student objects:

int main() {
    Student s1;
    s1.name = "Ali";
    s1.rollNumber = 101;
    s1.course = "Computer Science";
    s1.introduce();

    Student s2;
    s2.name = "Sara";
    s2.rollNumber = 102;
    s2.course = "Software Engineering";
    s2.introduce();

    return 0;
}

Output:

Hello, my name is Ali and I study Computer Science
Hello, my name is Sara and I study Software Engineering

Real-World Mapping

Real Life ConceptProgramming Equivalent
Design of a carClass
A specific car in showroomObject (car1, car2)
Name, color, engineAttributes (model, year)
Start engine, driveFunctions (startEngine())
Student record formClass Student
A real studentObject (s1, s2)

Why is it Useful?

Classes and objects bring many advantages that make programs more efficient, organized, and easier to manage. Here are some of the main reasons why they are useful:

Practice Task for Students

Create a class Book with attributes: title, author, and year.
Add a method showDetails() to print the book’s information.
Create two book objects and show their details.

Exit mobile version