Class, Object & Encapsulation in C++
Definition:
Class: A class is a user-defined data type in C++ that acts as a blueprint for creating objects. It
groups data members (variables) and member functions (methods) into a single unit.
Object: An object is an instance of a class that stores its own data and can call the functions
defined in the class.
■ In short: Class = Blueprint, Object = House built from the blueprint.
Syntax:
class ClassName {
// data members
// member functions
};
int main() {
ClassName obj; // object creation
obj.function(); // object accessing function
}
1. Simple Class & Object
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Student s1; // object creation
s1.name = "Piyush";
s1.age = 20;
s1.display();
}
Output:
Name: Piyush, Age: 20
2. Class with Constructor
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
Student(string n, int a) { // constructor
name = n;
age = a;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Student s1("Piyush", 20); // constructor runs
s1.display();
}
Output:
Name: Piyush, Age: 20
3. Class with Destructor
#include <iostream>
using namespace std;
class Student {
public:
Student() {
cout << "Constructor Called" << endl;
}
~Student() {
cout << "Destructor Called" << endl;
}
};
int main() {
Student s1; // constructor runs
} // destructor runs automatically
Output:
Constructor Called
Destructor Called
4. Class with Inheritance
#include <iostream>
using namespace std;
class Person {
public:
string name;
void setName(string n) { name = n; }
};
class Student : public Person {
public:
int age;
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Student s1;
s1.setName("Piyush");
s1.age = 20;
s1.display();
}
Output:
Name: Piyush, Age: 20
5. Class with Friend Function
#include <iostream>
using namespace std;
class Student {
private:
int marks;
public:
Student(int m) { marks = m; }
friend void showMarks(Student s); // friend function
};
void showMarks(Student s) {
cout << "Marks: " << s.marks << endl;
}
int main() {
Student s1(95);
showMarks(s1); // friend function accessing private data
}
Output:
Marks: 95
Encapsulation in C++
Definition: Encapsulation is the process of wrapping data (variables) and functions (methods)
together into a single unit called a class. It also restricts direct access to some data members by
using access specifiers (private, protected, public).
■ Encapsulation = Data Hiding + Data Binding
Key Features:
1. Data Hiding → sensitive data is kept private.
2. Controlled Access → only through public functions (getters/setters).
3. Improved Security → prevents accidental changes.
4. Modularity → separates implementation from use.
5. Maintainability → easier to modify without affecting external code.
Access Specifiers:
- Private → data/functions accessible only inside the class.
- Public → accessible from outside the class.
- Protected → accessible inside class + derived classes.
Advantages of Encapsulation:
1. Protects data from unauthorized access (security).
2. Increases code reusability and maintainability.
3. Provides flexibility through getter & setter functions.
4. Hides implementation details (supports abstraction).
5. Makes code modular and organized.
Real-life Example:
- ATM Machine: You press buttons (public methods), but you don’t know how the internal logic
works (private data).
#include <iostream>
using namespace std;
class Student {
private:
int marks; // private data (hidden from outside)
public:
// setter function
void setMarks(int m) {
if(m >= 0 && m <= 100) // validation
marks = m;
else
cout << "Invalid Marks!" << endl;
}
// getter function
int getMarks() {
return marks;
}
};
int main() {
Student s1;
s1.setMarks(95); // setting marks using public method
cout << "Marks: " << s1.getMarks() << endl; // accessing marks
return 0;
}
Output:
Marks: 95