Introduction to Classes and Objects
In modern software development, Classes and Objects form the very essence of Object-Oriented Programming (OOP). These tools help developers encapsulate data and operations in a single unit that mimics real-world entities. If you’re following the Code With Kamlesh course, this blog by Kamlesh Singad will help you build strong foundational skills in OOP through easy-to-understand theory and practical code examples.
What Are Classes in C++?
A class is a user-defined data type in C++ that bundles data and functions. Think of it as a blueprint for creating objects.
class Student {
public:
int roll;
string name;
};
Here, Student is a class with two members: roll and name.
Also Read: Conditional Statements in C/C++: Mastering if, else if, and switch-case

What Are Objects in C++?
An object is an instance of a class. Using the blueprint (class), we create real entities (objects).
Student s1;
Now s1 is an object with its own roll and name.
Why Use Classes and Objects?
- Encapsulation: Group related data and functions
- Reusability: Create multiple objects from one class
- Maintainability: Logical, modular code
- Real-World Modeling: Represent entities like employees, books, etc.
Class Declaration Syntax
class ClassName {
public:
// data members
// member functions
};
Example:
class Car {
public:
string brand;
void startEngine() {
cout << "Engine started!" << endl;
}
};
Object Declaration Syntax
Car myCar; // Object of class Car
Access Specifiers in C++
- public: Accessible from outside the class
- private: Accessible only within the class
- protected: Accessible by base and derived classes
Also Read: Loops in C/C++: For, While, and Do-While – A Comprehensive Guide

Defining Member Functions
Inside the class:
class Demo {
public:
void show() {
cout << "Inline function!" << endl;
}
};
Outside the class:
class Demo {
public:
void show();
};
void Demo::show() {
cout << "Function outside class" << endl;
}
Constructors and Destructors
Constructor: Called when an object is created
Destructor: Called when an object goes out of scope
class Person {
public:
Person() { cout << "Constructor\n"; }
~Person() { cout << "Destructor\n"; }
};
Constructor Overloading
class Rectangle {
public:
Rectangle() { width = height = 0; }
Rectangle(int w, int h) { width = w; height = h; }
private:
int width, height;
};
Object Lifecycle
{
Person p1; // Constructor called
} // Destructor called
Encapsulation Example
class Account {
private:
double balance;
public:
void deposit(double amount) { balance += amount; }
double getBalance() { return balance; }
};
Arrays and Pointers to Objects
Student students[5]; // Array of objects
Student *ptr = &students[0]; // Pointer to object
ptr->roll = 101;
Friend Function and Static Members
class Box {
private:
int width;
friend void printWidth(Box);
};
void printWidth(Box b) {
cout << "Width: " << b.width;
}
Static Member:
class Counter {
static int count;
};
Real-Life Example: Employee Class
class Employee {
public:
int id;
string name;
double salary;
void display() {
cout << id << " " << name << " " << salary << endl;
}
};
Passing and Returning Objects
void show(Student s) { cout << s.roll; }
Student getStudent() {
Student s;
s.roll = 100;
return s;
}
Practical Project: Student Info System
- Class
Student - Fields: name, roll, grade
- Methods: input(), display()
Demonstrate:
- Constructors
- Arrays
- Member functions
- Access control
Also Read: Functions in C/C++: Declaration, Definition, and Calling

Best Practices
- Keep data members private
- Use constructor initialization lists
- Prefer inline functions only for small operations
Common Mistakes
- Accessing private data directly
- Forgetting to define destructors
- Not using initialization lists
FAQs
What’s the difference between class and object?
Class is a blueprint; object is a specific instance of it.
Do classes support encapsulation?
Yes, by using private members and public methods.
Can constructors be overloaded?
Yes, with different parameter types or counts.
Are destructors mandatory?
Not always, but useful for cleaning dynamic memory.
Can we use objects with arrays?
Yes, object arrays are commonly used.
Conclusion
Classes and Objects are the pillars of Object-Oriented Programming. Mastering their declaration and implementation prepares you to build scalable, modular applications. Follow this guide by Kamlesh Singad on Code With Kamlesh to become confident with real-world class design in C++.
In the next part of the course, we’ll explore Inheritance and Polymorphism, expanding the OOP toolkit even further.




