OOP Inheritance and Polymorphism and Exception handling

Certainly! Here are examples of each topic with C++ code:
- Inheritance Concept:
cpp
class Vehicle { // Base class // ... }; class Car : public Vehicle { // Derived class inheriting from Vehicle // ... };
- Protected Members:
cpp
class Base { protected: int protectedVariable; }; class Derived : public Base { public: void accessBaseMember() { protectedVariable = 10; // Accessing protected member of the base class } };
- Base Class Initializer List:
cpp
class Base { public: Base(int x) { // Constructor of base class // ... } }; class Derived : public Base { public: Derived(int x, int y) : Base(x) { // Constructor of derived class with base class initializer list // ... } };
- Composition:
cpp
class Engine { // Engine class // ... }; class Car { private: Engine carEngine; // Composition of Engine class in Car class // ... };
- Member Initialization List:
cpp
class MyClass { private: int myVariable; public: MyClass(int x) : myVariable(x) { // Constructor using member initialization list // ... } };
- Order of Initialization:
cpp
class Base1 { public: Base1() { // Constructor of Base1 // ... } }; class Base2 { public: Base2() { // Constructor of Base2 // ... } }; class Derived : public Base1, public Base2 { public: Derived() { // Constructor of Derived class with order of initialization: Base1, Base2 // ... } };
- Inheritance vs. Composition:
cpp
// Inheritance example class Animal { // Animal class // ... }; class Dog : public Animal { // Dog class inheriting from Animal class // ... }; // Composition example class Engine { // Engine class // ... }; class Car { private: Engine carEngine; // Composition of Engine class in Car class // ... };
Summary – Inheritance: Explanation doesn't require code.
A Case for Polymorphism: Explanation doesn't require code.
Dynamic Binding: Explanation doesn't require code.
Pointer Conversion in Inheritance:
cpp
class Base { // Base class // ... }; class Derived : public Base { // Derived class inheriting from Base class // ... }; int main() { Base* basePtr = new Derived(); // Pointer conversion from Derived to Base // ... delete basePtr; }
- Polymorphism Using Dynamic Binding:
cpp
class Animal { public: virtual void makeSound() { // Base class virtual function // ... } }; class Cat : public Animal { public: void makeSound() override { // Derived class implementation of makeSound() // ... } }; int main() { Animal* animalPtr = new Cat(); // Pointer to base class pointing to derived class object animalPtr->makeSound(); // Dynamic binding of makeSound() based on the actual object type // ... delete animalPtr; }
- Virtual Function Specification:
cpp
class Base { public: virtual void myFunction() { // Virtual function in base class // ... } }; class Derived : public Base { public: void myFunction() override { // Derived class implementation of myFunction() // ... } };
- Invoking Virtual Functions:
cpp
class Base { public: virtual void myFunction() { // Virtual function in base class // ... } }; class Derived : public Base { public: void myFunction() override { // Derived class implementation of myFunction() // ... } }; int main() { Base* basePtr = new Derived(); // Pointer to derived class object basePtr->myFunction(); // Invoking virtual function using base class pointer // ... delete basePtr; }
VTable: Explanation doesn't require code.
Virtual Destructors:
cpp
class Base { public: virtual ~Base() { // Virtual destructor in base class // ... } }; class Derived : public Base { public: ~Derived() override { // Derived class destructor // ... } }; int main() { Base* basePtr = new Derived(); // Pointer to derived class object // ... delete basePtr; // Calls the correct destructor using virtual destructor }
- Abstract Class Using Pure Virtual Function:
cpp
class Shape { public: virtual void draw() = 0; // Pure virtual function }; class Circle : public Shape { public: void draw() override { // Implementation of draw() in Circle class // ... } }; int main() { // Shape* shapePtr = new Shape(); // Error: Cannot instantiate an abstract class Shape* shapePtr = new Circle(); // Pointer to derived class object shapePtr->draw(); // Calls the draw() implementation in Circle class // ... delete shapePtr; }
- Employee as an Abstract Class:
cpp
class Employee { public: virtual void calculateSalary() = 0; // Pure virtual function }; class Manager : public Employee { public: void calculateSalary() override { // Implementation of calculateSalary() in Manager class // ... } }; class Engineer : public Employee { public: void calculateSalary() override { // Implementation of calculateSalary() in Engineer class // ... } }; int main() { Employee* employee1 = new Manager(); // Pointer to Manager class object Employee* employee2 = new Engineer(); // Pointer to Engineer class object employee1->calculateSalary(); // Calls the calculateSalary() implementation in Manager class employee2->calculateSalary(); // Calls the calculateSalary() implementation in Engineer class // ... delete employee1; delete employee2; }
- Heterogeneous Collections:
cpp
class Shape { public: virtual void draw() = 0; }; class Circle : public Shape { public: void draw() override { // Implementation of draw() in Circle class // ... } }; class Rectangle : public Shape { public: void draw() override { // Implementation of draw() in Rectangle class // ... } }; int main() { std::vector<Shape*> shapes; shapes.push_back(new Circle()); shapes.push_back(new Rectangle()); for (Shape* shape : shapes) { shape->draw(); // Calls the appropriate draw() implementation based on the object type } for (Shape* shape : shapes) { delete shape; } }
- Summary – Polymorphism: Explanation doesn't require code.
Please note that these code examples are simplified for illustrative purposes and may not cover all aspects or best practices of each topic.