Inheritance In C++ Csit 2nd Sem

C++ Inheritance notes Bsc csit Second semester



-ON THIS PAGE 📃 YOU WILL FIND HANDWRITTEN NOTES OF C++ inheritance AND Also A reference about Inheritance in c++ 

Here is a pdf of Handwritten notes about inheritance in C++

It is very interesting and important topic on C++


Reference on Inheritance in c++

Inheritance in c++

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit the properties and methods of another class. In C++, inheritance is implemented using the ":" operator, which is followed by the base class name.

For example, consider the following class hierarchy:

class Animal {

  public:

    void move() {

      cout << "Animal is moving" << endl;

    }

};

class Dog: public Animal {

  public:

    void bark() {

      cout << "Woof!" << endl;

    }

};

In this example, the Dog class is derived from the Animal class, which means that it inherits all of the public and protected members of the Animal class. This means that objects of the Dog class have access to the move() method, in addition to the bark() method that is specific to the Dog class.

One important concept in inheritance is the idea of polymorphism, which allows derived classes to override the behavior of their base class methods. For example, we could override the move() method in the Dog class like this:

class Dog: public Animal {

  public:

    void move() {

      cout << "Dog is running" << endl;

    }

    void bark() {

      cout << "Woof!" << endl;

    }

};

Now, when we call the move() method on an object of the Dog class, it will execute the overridden version of the method, rather than the version inherited from the Animal class.

Another important concept in inheritance is the idea of access specifiers, which control the visibility and accessibility of class members. In C++, there are three access specifiers: public, protected, and private. Members declared as public are accessible to any code, while members declared as protected are only accessible to the class and its derived classes. Members declared as private are only accessible to the class itself.

For example, we could modify the Animal class to include a private member like this:

class Animal {

  private:

    int age;

  public:

    void setAge(int a) {

      age = a;

    }

    int getAge() {

      return age;

    }

    void move() {

      cout << "Animal is moving" << endl;

    }

};

In this example, the age member is declared as private, which means that it is only accessible to the Animal class itself. We can still access the age member indirectly using the setAge() and getAge() methods, which are declared as public and are therefore accessible to any code.

Inheritance is a powerful tool in C++ that allows you to create complex class hierarchies and reuse code across multiple classes. It is an essential concept to understand when learning OOP in C++.

Types of Inheritance in c++

There are several types of inheritance in C++, which allow you to specify the relationship between a base class and a derived class. Here are some of the most common types of inheritance, along with examples:

1. Single inheritance: 

This is the most basic type of inheritance, in which a derived class has only one base class. For example:

class Animal {

  // base class

};


class Dog: public Animal {

  // derived class

};


2. Multiple inheritance: 

This type of inheritance allows a derived class to have multiple base classes. For example:

class Animal {

  // base class

};


class Vehicle {

  // base class

};


class Dog: public Animal, public Vehicle {

  // derived class

};

3. Multilevel inheritance: 

This type of inheritance involves a chain of derived classes, where one derived class is the base class for another derived class. For example:

class Animal {

  // base class

};


class Dog: public Animal {

  // derived class

};


class Labrador: public Dog {

  // derived class

};

4. Hierarchical inheritance: 

This type of inheritance involves a single base class that is inherited by multiple derived classes. For example:

class Animal {

  // base class

};


class Dog: public Animal {

  // derived class

};

class Cat: public Animal {

  // derived class

};

5. Hybrid inheritance: 

This type of inheritance is a combination of two or more types of inheritance, such as multiple inheritance and hierarchical inheritance. For example:

class Animal {

  // base class

};


class Vehicle {

  // base class

};


class Dog: public Animal, public Vehicle {

  // derived class

};


class Cat: public Animal {

  // derived class

};


THANKS 🙏 FOR VISITING MY BLOG💖

KEEP SHARING WITH YOUR FRIENDS 😍 


IF YOU WANT TO CONNECT WITH ME PERSONALLY

YOU CAN DM ME ON FACEBOOK

Facebook Link 🔗

Post a Comment

أحدث أقدم