Lokang 

C++ and MySQL

Inheritance

Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a new class to inherit properties and behaviors (data members and member functions) from an existing class. The existing class is called the base class (or parent class), and the new class is called the derived class (or child class). Inheritance promotes code reuse, allows for the extension of existing classes, and enables polymorphism.

Why Use Inheritance?

Inheritance provides several benefits, including:

  • Code Reusability: Common attributes and methods can be defined in a base class and reused across multiple derived classes, reducing redundancy.
  • Extensibility: New classes can be created by extending existing ones, allowing for the easy addition of new features.
  • Polymorphism: Derived classes can override base class methods to provide specific implementations, enabling dynamic behavior.

Defining a Base Class and a Derived Class

In C++, a derived class is created using the syntax:

class DerivedClass : accessSpecifier BaseClass {
   // Body of derived class
};

Here’s a simple example demonstrating inheritance:

// Base class
class Animal {
public:
   void eat() {
       cout << "This animal is eating." << endl;
   }
};
// Derived class
class Dog : public Animal {
public:
   void bark() {
       cout << "The dog is barking." << endl;
   }
};

In this example:

  • Animal is the base class with a method eat().
  • Dog is the derived class that inherits from Animal and adds a new method bark().

Creating Objects of the Derived Class

Objects of the derived class can access both the inherited members from the base class and the members defined in the derived class.

int main() {
   Dog myDog;
   // Calling the inherited method
   myDog.eat();
   // Calling the derived class method
   myDog.bark();
   return 0;
}

In this code:

  • myDog is an object of the Dog class.
  • The object can call both the eat() method from the Animal class and the bark() method from the Dog class.

Types of Inheritance

C++ supports several types of inheritance, including:

  • Single Inheritance: A derived class inherits from only one base class.
  • Multiple Inheritance: A derived class inherits from more than one base class.
  • Hierarchical Inheritance: Multiple derived classes inherit from a single base class.
  • Multilevel Inheritance: A class is derived from a class which is also derived from another class.
  • Hybrid Inheritance: A combination of two or more types of inheritance.

Example of Multiple Inheritance:

class Mammal {
public:
   void walk() {
       cout << "This mammal is walking." << endl;
   }
};
class Bird {
public:
   void fly() {
       cout << "This bird is flying." << endl;
   }
};
// Derived class
class Bat : public Mammal, public Bird {
public:
   void hangUpsideDown() {
       cout << "The bat is hanging upside down." << endl;
   }
};

In this example:

  • Bat is a derived class that inherits from both Mammal and Bird.
  • Bat objects can access methods from both base classes.

Access Specifiers and Inheritance

The access specifier used in the inheritance declaration (public, protected, private) affects how the members of the base class are accessible in the derived class.

  • Public Inheritance: Public and protected members of the base class become public and protected members of the derived class, respectively.
  • Protected Inheritance: Public and protected members of the base class become protected members of the derived class.
  • Private Inheritance: Public and protected members of the base class become private members of the derived class.

Example of public inheritance:

class Animal {
public:
   void sleep() {
       cout << "This animal is sleeping." << endl;
   }
};
class Cat : public Animal {
public:
   void meow() {
       cout << "The cat is meowing." << endl;
   }
};

In this example:

  • Cat inherits from Animal using public inheritance, so sleep() is accessible as a public method of Cat.

Method Overriding

A derived class can override a base class method to provide a specific implementation. This is done by defining a method in the derived class with the same signature as the one in the base class.

class Animal {
public:
   virtual void sound() {
       cout << "This animal makes a sound." << endl;
   }
};
class Dog : public Animal {
public:
   void sound() override {
       cout << "The dog barks." << endl;
   }
};

In this example:

  • The sound() method in Dog overrides the sound() method in Animal.
  • The virtual keyword ensures that the correct method is called based on the object type.

The virtual Keyword and Polymorphism

Polymorphism allows one interface to be used for a general class of actions. The virtual keyword in C++ enables polymorphism, where the derived class's method is called even when an object is referenced through a pointer to the base class.

int main() {
   Animal* animalPtr;
   Dog myDog;
   animalPtr = &myDog;
   // Calls the Dog's sound() method
   animalPtr->sound();
   return 0;
}

Here:

  • Even though animalPtr is of type Animal*, it calls the sound() method of Dog due to polymorphism.

Summary and Best Practices

Inheritance is a powerful tool in OOP that allows classes to inherit properties and behaviors from other classes, promoting code reuse and extensibility. It is important to choose the appropriate type of inheritance and carefully manage access specifiers to ensure that class hierarchies remain clear and maintainable. Method overriding and the use of virtual functions are crucial for achieving polymorphism, which allows for dynamic and flexible code.

This detailed course content should equip students with a strong understanding of inheritance in C++, enabling them to apply these concepts effectively in their programming projects.