OOP Inheritance and Polymorphism and Exception handling

Info Thread

OOP Inheritance and Polymorphism and Exception handling




Certainly! Here are examples of each topic with C++ code:

  1. Inheritance Concept:

cpp

class Vehicle { // Base class // ... }; class Car : public Vehicle { // Derived class inheriting from Vehicle // ... };

  1. Protected Members:

cpp

class Base { protected: int protectedVariable; }; class Derived : public Base { public: void accessBaseMember() { protectedVariable = 10; // Accessing protected member of the base class } };

  1. 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 // ... } };

  1. Composition:

cpp

class Engine { // Engine class // ... }; class Car { private: Engine carEngine; // Composition of Engine class in Car class // ... };

  1. Member Initialization List:

cpp

class MyClass { private: int myVariable; public: MyClass(int x) : myVariable(x) { // Constructor using member initialization list // ... } };

  1. 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 // ... } };

  1. 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 // ... };

  1. Summary – Inheritance: Explanation doesn't require code.

  2. A Case for Polymorphism: Explanation doesn't require code.

  3. Dynamic Binding: Explanation doesn't require code.

  4. 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; }

  1. 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; }

  1. 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() // ... } };

  1. 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; }

  1. VTable: Explanation doesn't require code.

  2. 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 }

  1. 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; }

  1. 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; }

  1. 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; } }

  1. 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.

Tags

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !