Object-Oriented Programming (OOP) is a paradigm in computer science that uses "objects" to design applications and computer programs. It utilizes several key concepts to create reusable and modular code. In C++, OOP is a powerful tool that allows for the modeling of real-world entities and their interactions. This study note will cover the essential aspects of OOP in C++ as per the CBSE syllabus.
A class is a blueprint for creating objects. It defines a datatype by bundling data and methods that work on the data into one single unit.
class Car {
public:
string brand;
string model;
int year;
void displayInfo() {
cout
<< "Brand: " << brand << ", Model: " << model << ", Year: " << year << endl;
}
};
An object is an instance of a class. It is created using the class definition and can access the class's methods and attributes.
int main() {
Car car1;
car1.brand = "Toyota";
car1.model = "Corolla";
car1.year = 2020;
car1.displayInfo();
return 0;
}
Note
In C++, the public
keyword allows members of the class to be accessible from outside the class.
Encapsulation is the concept of wrapping data and methods that operate on the data within one unit, e.g., a class. It helps in protecting the data from outside interference and misuse.
class Student {
private:
string name;
int age;
public:
void setName(string n) {
name = n;
}
void setAge(int a) {
age = a;
}
void display() {
cout
<< "Name: " << name << ", Age: " << age << endl;
}
};
Note
The private
keyword restricts access to the class members from outside the class.
Inheritance is a mechanism by which one class can inherit the properties and methods of another class. This promotes code reuse and establishes a relationship between the parent (base) and child (derived) class.
class Animal {
public:
void eat() {
cout
<< "Eating..." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Barking..." << endl;
}
};
Example
In the above example, the Dog
class inherits from the Animal
class. Thus, an object of Dog
can call both eat
and bark
methods.
Polymorphism means "many forms" and it allows functions or methods to process objects differently based on their data type or class. It is mainly achieved through function overloading and operator overloading.
Function overloading allows multiple functions with the same name but different parameters.
class Print {
public:
void display(int i) {
cout
<< "Integer: " << i << endl;
}
void display(double d) {
cout << "Double: " << d << endl;
}
};
Operator overloading allows you to redefine the way operators work for user-defined types.
class Complex {
public:
int real, imag;
Complex operator + (const Complex& obj) {
Complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
};
Abstraction is the concept of hiding the complex implementation details and showing only the necessary features of an object.
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};
class Circle : public Shape {
public:
void draw() {
cout << "Drawing Circle" << endl;
}
};
Note
A class containing at least one pure virtual function is called an abstract class and cannot be instantiated.
Tip
Always use access specifiers (public
, private
, protected
) to encapsulate data and methods properly.
Common Mistake
A common mistake is to forget to initialize class members, which can lead to undefined behavior.
Understanding the core concepts of OOP in C++ is essential for creating robust, reusable, and maintainable code. By leveraging classes, objects, inheritance, polymorphism, encapsulation, and abstraction, you can model real-world problems effectively.
This study note provides a foundational understanding of OOP in C++. Practice by writing and experimenting with small programs to solidify these concepts.