Table of contents | |
Introduction | |
What is Inheritance? | |
Syntax and Types of Inheritance | |
Code Examples | |
Sample Problems and Solutions |
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.
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.
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
};
Let's dive into code examples to better understand the different types of inheritance.
There are different types of 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:
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:
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:
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
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++.
153 videos|115 docs|24 tests
|
|
Explore Courses for Software Development exam
|