OOP
...

Object-oriented programming is a programming paradigm that emphasizes the use of objects and their interactions to design and write software. C++ is a popular programming language that supports OOP concepts such as abstraction, polymorphism, inheritance, and encapsulation.

Let's start with the basics:

1. Abstraction:
...

Abstraction is the process of hiding the implementation details of a class while showing only the necessary information to the user. In C++, abstraction is achieved using classes and interfaces. A class is a user-defined data type that encapsulates data members and member functions. An interface is a collection of abstract methods that define the behavior of an object.

Here's an example of an abstract class in C++:

class Shape {
public:
   virtual void draw() = 0; // pure virtual function
};

In this example, Shape is an abstract class that has a pure virtual function called draw. A pure virtual function is a virtual function that has no implementation in the base class and must be implemented in the derived class. This makes Shape an abstract class because it cannot be instantiated.

2. Polymorphism:
...

Polymorphism is the ability of an object to take on many forms. In C++, polymorphism is achieved using virtual functions and function overloading.

Here's an example of polymorphism using virtual functions in C++:

class Animal {
public:
   virtual void makeSound() {
      cout << "The animal makes a sound" << endl;
   }
};

class Dog: public Animal {
public:
   void makeSound() {
      cout << "The dog says woof" << endl;
   }
};

class Cat: public Animal {
public:
   void makeSound() {
      cout << "The cat says meow" << endl;
   }
};

int main() {
   Animal* animal1 = new Dog();
   Animal* animal2 = new Cat();
   
   animal1->makeSound();
   animal2->makeSound();
   
   delete animal1;
   delete animal2;
   
   return 0;
}

In this example, Animal is a base class that has a virtual function called makeSound. Dog and Cat are derived classes that override the makeSound function. When we create a new Dog object and a new Cat object, and call the makeSound function on them, the appropriate derived class function is called, demonstrating polymorphism.

3. Inheritance:
...

Inheritance is the process of creating a new class from an existing class. In C++, inheritance is achieved using the keyword "public", "private", and "protected".

Here's an example of inheritance in C++:

class Person {
public:
   string name;
   int age;
   void display() {
      cout << "Name: " << name << endl;
      cout << "Age: " << age << endl;
   }
};

class Student: public Person {
public:
   int studentId;
   void displayStudent() {
      display();
      cout << "Student ID: " << studentId << endl;
   }
};

int main() {
   Student s;
   s.name = "John";
   s.age = 20;
   s.studentId = 12345;
   s.displayStudent();
   
   return 0;
}

In this example, Person is a base class that has two data members and a member function. Student is a derived class that inherits the data members and member function from Person and adds a new data member and member function. When we create a new Student object and call the displayStudent function on it, both the base class and derived class member functions are called, demonstrating inheritance.

4. Encapsulation:
...

Encapsulation is the process of hiding the implementation details of a class while providing a public interface for accessing the class. In C++, encapsulation is achieved using access specifiers.

Here's an example of encapsulation in C++:

class BankAccount {
private:
   string accountNumber;
   double balance;
   
public:
   void deposit(double amount) {
      balance += amount;
   }
   
   void withdraw(double amount) {
      balance -= amount;
   }
   
   double getBalance() {
      return balance;
   }
};

int main() {
   BankAccount b;
   b.deposit(1000);
   b.withdraw(500);
   cout << "Balance: " << b.getBalance() << endl;
   
   return 0;
}

In this example, BankAccount is a class that has two private data members and three public member functions. The private data members are hidden from the user and can only be accessed through the public member functions, providing encapsulation.