Object-Oriented Programming in C++
Imagine organizing a school science fair. Each project booth has students, a project name, a category, and a score. You wouldn’t store this information separately, instead, you think of each booth as one object with all its details grouped together.
C++ lets you organize programs the same way using classes and objects.
A class is like the blueprint of a booth, and an object is the actual booth created from it.
Today, you’ll teach your program to create real-world models the way humans naturally think.
Digital Student Profile
You’ll create a class Student that stores:
- Name
- Age
- Grade
Then create an object from this class and print a formatted profile:
1 2 3 4 5 6======================== STUDENT PROFILE Name: Ali Khan Age: 15 Grade: A ========================
This is your first step into object-oriented programming (OOP).
What Is a Class?
A class is a blueprint that defines what data an object will have and what it can do. Think of it like a template for creating many similar objects. It tells C++: “Any student will have these properties.”
Example
1 2 3 4 5class Student { public: string name; int age; };
What Is an Object?
An object is a real instance created from a class. It receives its own values for the properties defined in the class. Different objects can store different information.
Example
1 2Student s1; s1.name = "Ali";
Member Variables
These are the attributes inside a class (like name, age, grade). Objects use these variables to store their own data. Every object keeps its data separate from others.
Member Functions
Functions inside a class define what an object can do. They help process or display the data stored inside an object. They make the object self-contained and capable.
Example
1void display() { cout << name; }
Constructors
A constructor is a special function that initializes an object when it’s created. It sets default or starting values automatically. This saves time and prevents uninitialized data.
Example
1Student() { age = 0; }
Access Modifiers
public means accessible from outside the class. private hides data inside the class and protects it. This is the foundation of encapsulation.
Let’s Build the Student Profile Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30#include <iostream> using namespace std; class Student { public: string name; int age; char grade; void display() { cout << "========================" << endl; cout << " STUDENT PROFILE" << endl; cout << " Name: " << name << endl; cout << " Age: " << age << endl; cout << " Grade: " << grade << endl; cout << "========================" << endl; } }; int main() { Student s; s.name = "Ali Khan"; s.age = 15; s.grade = 'A'; s.display(); return 0; }
How It Works (2–3 lines)
- The class defines what data a student object holds.
- The object s stores actual values for that student.
- The display() function prints the formatted profile.
Test this instantly in the DevsCall AI Code Runner.
Learn Together with AI
Try prompts like:
- “Create a class named Car with brand, model, and year.”
- “Add a constructor to initialize values automatically.”
- “Explain public vs private in simple words.”
- “Write a class with a function that calculates total marks.”
AI can generate new examples or explain anything you’re unsure about.
Practice Time
Write a C++ program that:
- Creates a class Book with:
- title
- author
- price
- Adds a display() function inside the class.
- Creates an object and prints:
1 2 3 4 5 6======================== BOOK DETAILS Title: The Alchemist Author: Paulo Coelho Price: 1200 ========================
Ask AI:
“How do I create multiple objects from the same class in C++?”