Software Development Exam  >  Software Development Notes  >  DSA in C++  >  Inheritance in C++

Inheritance in C++ | DSA in C++ - Software Development PDF Download

Introduction

Inheritance is a powerful concept in object-oriented programming that allows you to create new classes by deriving properties and behaviors from existing classes. In this article, we will explore the fundamentals of inheritance in C++, focusing on how it can be applied in data structures and algorithms (DSA) programming. We'll cover the syntax, types of inheritance, code examples, and sample problems to help solidify your understanding.

What is Inheritance?

Inheritance is a concept in object-oriented programming that allows you to define a new class based on an existing class. The new class, called the derived class or child class, inherits properties and behaviors from the existing class, known as the base class or parent class. This inheritance relationship forms an "is-a" relationship between the classes.

Syntax and Types of Inheritance

In C++, inheritance is declared using the 'class' keyword, followed by a colon and the access specifier (public, protected, or private), and the name of the base class.

The syntax for single inheritance is as follows:

class DerivedClass : access-specifier BaseClass {

  // class members and methods

};

Code Examples

Let's dive into code examples to better understand the different types of inheritance.

There are different types of inheritance:

  • Single inheritance: A derived class inherits from a single base class.
  • Multilevel inheritance: A derived class becomes the base class for another derived class.
  • Multiple inheritance: A derived class inherits from multiple base classes.

Single Inheritance

In single inheritance, a derived class inherits the properties and behaviors of a single base class. Here's an example:

#include <iostream>


// Base class

class Shape {

public:

  void display() {

    std::cout << "This is a shape." << std::endl;

  }

};


// Derived class

class Circle : public Shape {

public:

  void draw() {

    std::cout << "Drawing a circle." << std::endl;

  }

};


int main() {

  Circle circle;

  circle.display(); // Accessing base class member function

  circle.draw();    // Accessing derived class member function

  return 0;

}

Output:

This is a shape.

Drawing a circle.

Code Explanation:

  • We have a base class 'Shape' with a member function 'display()', which simply outputs a message.
  • The derived class 'Circle' is declared with the 'public' access specifier, indicating that the derived class inherits the public members of the base class.
  • In the 'main()' function, we create an object of the derived class 'Circle'.
  • We can access the member function 'display()' from the base class using the derived class object, as it is inherited.
  • Additionally, the derived class has its own member function 'draw()', which outputs a message specific to the derived class.

Multilevel Inheritance

In multilevel inheritance, a derived class becomes the base class for another derived class. Here's an example:

#include <iostream>


// Base class

class Animal {

public:

  void eat() {

    std::cout << "Animal is eating." << std::endl;

  }

};


// Derived class

class Mammal : public Animal {

public:

  void walk() {

    std::cout << "Mammal is walking." << std::endl;

  }

};


// Derived class

class Dog : public Mammal {

public:

  void bark() {

    std::cout << "Dog is barking." << std::endl;

  }

};


int main() {

  Dog dog;

  dog.eat();  // Accessing base class member function

  dog.walk(); // Accessing derived class member function

  dog.bark(); // Accessing derived class member function

  return 0;

}

Output:

Animal is eating.

Mammal is walking.

Dog is barking.

Code Explanation:

  • We have a base class 'Animal' with a member function 'eat()'.
  • The derived class 'Mammal' inherits from the base class 'Animal' and adds a new member function 'walk()'.
  • The derived class 'Dog' inherits from the derived class 'Mammal' and adds a new member function 'bark()'.
  • In the 'main()' function, we create an object of the derived class 'Dog'.
  • We can access the member functions 'eat()' and 'walk()' from the base class and the intermediate derived class, respectively.
  • Additionally, the derived class 'Dog' has its own member function 'bark()'.

Multiple Inheritance

Multiple inheritance allows a derived class to inherit from multiple base classes. Here's an example:

#include <iostream>


// Base class

class Vehicle {

public:

  void drive() {

    std::cout << "Driving a vehicle." << std::endl;

  }

};


// Base class

class Animal {

public:

  void eat() {

    std::cout << "Animal is eating." << std::endl;

  }

};


// Derived class

class Car : public Vehicle, public Animal {

public:

  void honk() {

    std::cout << "Car is honking." << std::endl;

  }

};


int main() {

  Car car;

  car.drive(); // Accessing base class member function

  car.eat();   // Accessing base class member function

  car.honk();  // Accessing derived class member function

  return 0;

}

Output:

Driving a vehicle.

Animal is eating.

Car is honking.

Code Explanation:

  • We have two base classes, 'Vehicle' and 'Animal', with their respective member functions.
  • The derived class 'Car' inherits from both 'Vehicle' and 'Animal' using multiple inheritance.
  • In the 'main()' function, we create an object of the derived class 'Car'.
  • We can access the member functions 'drive()' and 'eat()' from the respective base classes.
  • Additionally, the derived class 'Car' has its own member function 'honk()'.

Sample Problems and Solutions

Here are a couple of sample problems that involve inheritance:

Problem 1: Create a base class 'Person' with a member function 'displayInfo()'. Derive a class Student from 'Person' and add a member function 'displayGrade()'. Implement the classes and demonstrate their usage.

#include <iostream>


class Person {

public:

  void displayInfo() {

    std::cout << "This is a person." << std::endl;

  }

};


class Student : public Person {

public:

  void displayGrade() {

    std::cout << "Student's grade is A." << std::endl;

  }

};


int main() {

  Student student;

  student.displayInfo();

  student.displayGrade();

  return 0;

}

Output:

This is a person.

Student's grade is A.

Problem 2: Create a base class 'Shape' with a member function 'calculateArea()'. Derive classes 'Rectangle' and 'Circle' from 'Shape' and implement their respective 'calculateArea()' functions. Display the areas of a rectangle and a circle.

#include <iostream>


class Shape {

public:

  virtual void calculateArea() {

    std::cout << "Calculating area of a shape." << std::endl;

  }

};


class Rectangle : public Shape {

public:

  void calculateArea() override {

    int length = 5;

    int width = 3;

    int area = length * width;

    std::cout << "Area of rectangle: " << area << std::endl;

  }

};


class Circle : public Shape {

public:

  void calculateArea() override {

    float radius = 2.5;

    float area = 3.1415 * radius * radius;

    std::cout << "Area of circle: " << area << std::endl;

  }

};


int main() {

  Rectangle rectangle;

  Circle circle;

  rectangle.calculateArea();

  circle.calculateArea();

  return 0;

}

Output:

Area of rectangle: 15

Area of circle: 19.6344

Conclusion

Inheritance is a powerful tool in DSA programming that allows us to create hierarchies of classes, enabling code reuse and organizing our code in a logical manner. By understanding and implementing inheritance correctly, we can design more robust and efficient data structures and algorithms. Remember to practice and explore different examples to strengthen your understanding of inheritance in C++. 

The document Inheritance in C++ | DSA in C++ - Software Development is a part of the Software Development Course DSA in C++.
All you need of Software Development at this link: Software Development
153 videos|115 docs|24 tests

Top Courses for Software Development

153 videos|115 docs|24 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Previous Year Questions with Solutions

,

Summary

,

shortcuts and tricks

,

Inheritance in C++ | DSA in C++ - Software Development

,

practice quizzes

,

MCQs

,

past year papers

,

pdf

,

Free

,

ppt

,

Semester Notes

,

video lectures

,

mock tests for examination

,

Inheritance in C++ | DSA in C++ - Software Development

,

Inheritance in C++ | DSA in C++ - Software Development

,

Objective type Questions

,

Sample Paper

,

Extra Questions

,

Important questions

,

Viva Questions

,

Exam

,

study material

;