Introduction to C++ | OOP Concepts
C++ OOP Concepts
Object-Oriented Programming (OOP) is a programming paradigm that is based on the concept of "objects", which can contain data and code that manipulates the data. In C++, the main OOP concepts include Classes and Objects, Encapsulation, Inheritance, Polymorphism, and Abstraction. These concepts help in designing more efficient, reusable, and maintainable software systems.
What is Object-Oriented Programming (OOP)?
OOP is a way to structure a program by bundling related properties and behaviors into individual objects. This allows for code modularity, scalability, and reuse. OOP concepts focus on creating and managing objects that interact with each other to solve problems.
Core OOP Concepts in C++
- Classes and Objects
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
1. Classes and Objects
Class is a blueprint for creating objects. A class defines properties (data members) and methods (functions) that represent the behaviors of the object. An Object is an instance of a class.
Example:
Code Example: Class and Object
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int year;
void displayDetails() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
Car myCar; // Creating an object of the class Car
myCar.brand = "Toyota";
myCar.year = 2020;
myCar.displayDetails(); // Calling the member function
return 0;
}
Output:
2. Encapsulation
Encapsulation is the bundling of data (variables) and methods (functions) that operate on the data into a single unit called class. It also restricts direct access to some of the object's components, which is a means of preventing unintended interference and misuse of the data. Access to the data is typically done via getter and setter functions.
Example of Encapsulation:
Code Example: Encapsulation
#include <iostream>
using namespace std;
class BankAccount {
private:
double balance;
public:
void setBalance(double amount) {
if (amount >= 0) {
balance = amount;
}
}
double getBalance() {
return balance;
}
};
int main() {
BankAccount account;
account.setBalance(1000.50);
cout << "Account balance: " << account.getBalance() << endl;
return 0;
}
Output:
3. Inheritance
Inheritance allows a new class (derived class) to inherit the properties and methods of an existing class (base class). It is used to represent hierarchical relationships between classes.
Example of Inheritance:
Code Example: Inheritance
#include <iostream>
using namespace std;
class Vehicle {
public:
void displayType() {
cout << "Vehicle type: General" << endl;
}
};
class Car : public Vehicle { // Car class inherits from Vehicle
public:
void displayDetails() {
cout << "Car details: Sedan, 4-door" << endl;
}
};
int main() {
Car myCar;
myCar.displayType(); // Calling base class method
myCar.displayDetails(); // Calling derived class method
return 0;
}
Output:
Car details: Sedan, 4-door
4. Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common base class. It enables a single function or method to work in different ways depending on the object that calls it. There are two types of polymorphism in C++: Compile-time (method overloading, operator overloading) and Run-time (virtual functions).
Example of Run-time Polymorphism using Virtual Functions:
Code Example: Polymorphism
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() {
cout << "Animal makes a sound" << endl;
}
};
class Dog : public Animal {
public:
void sound() override {
cout << "Dog barks" << endl;
}
};
int main() {
Animal* animalPtr;
Dog myDog;
animalPtr = &myDog;
animalPtr->sound(); // Virtual function call
return 0;
}
Output:
5. Abstraction
Abstraction is the concept of hiding the complex implementation details and showing only the necessary features of an object. In C++, it is achieved using abstract classes and pure virtual functions. An abstract class cannot be instantiated directly, and it must be inherited by other classes to implement its virtual functions.
Example of Abstraction using Abstract Class:
Code Example: Abstraction
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing a Circle" << endl;
}
};
int main() {
Circle myCircle;
myCircle.draw();
return 0;
}
Output:
Pro Tip:
💡 Pro Tip
OOP concepts help you create modular, reusable, and maintainable code. By following principles like encapsulation, inheritance, and polymorphism, your C++ programs will become more flexible and easier to extend in the future. Always strive for good abstraction to hide complexity and improve code readability.