C++ is an object-oriented programming language that allows developers to create classes and objects. Inheritance is one of the key features of C++ that allows developers to create new classes based on existing classes. Inheritance is a mechanism that allows a new class to be based on an existing class, inheriting all the properties and methods of the existing class. This feature is used to create a hierarchy of classes, where each class inherits from the class above it in the hierarchy.
Inheritance is a powerful feature of C++ that allows developers to create complex programs with ease. It allows developers to reuse code and create new classes based on existing classes. Inheritance is used to create a hierarchy of classes, where each class inherits from the class above it in the hierarchy. This hierarchy is known as the inheritance tree.
There are different types of inheritance in C++, including single inheritance, multiple inheritance, and hierarchical inheritance. Single inheritance is when a class inherits from only one class. Multiple inheritance is when a class inherits from more than one class. Hierarchical inheritance is when a class inherits from more than one class, but those classes are related in a hierarchical manner.
Let's take a look at an example of single inheritance:
class Animal {
public:
void eat() {
cout << "I am eating" << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Woof!" << endl;
}
};
int main() {
Dog myDog;
myDog.eat(); // Output: I am eating
myDog.bark(); // Output: Woof!
return 0;
}
In this example, we have a base class called Animal that has a method called eat(). We then create a derived class called Dog that inherits from the Animal class. The Dog class has a method called bark(). We create an object of the Dog class and call the eat() and bark() methods.
Another example of inheritance is multiple inheritance:
class Shape {
public:
virtual double area() = 0;
};
class Rectangle : public Shape {
public:
Rectangle(double w, double h) : width(w), height(h) {}
double area() {
return width * height;
}
private:
double width;
double height;
};
class Circle : public Shape {
public:
Circle(double r) : radius(r) {}
double area() {
return 3.14 * radius * radius;
}
private:
double radius;
};
class Square : public Rectangle, public Circle {
public:
Square(double s) : Rectangle(s, s), Circle(s/2) {}
};
int main() {
Square mySquare(4);
cout << "Area of square: " << mySquare.area() << endl; // Output: Area of square: 12.56
return 0;
}
In this example, we have a base class called Shape that has a pure virtual method called area(). We then create two derived classes called Rectangle and Circle that inherit from the Shape class. We then create a derived class called Square that inherits from both the Rectangle and Circle classes. We create an object of the Square class and call the area() method, which calculates the area of the square.
In conclusion, inheritance is a powerful feature of C++ that allows developers to create new classes based on existing classes. It allows developers to reuse code and create new classes with ease. There are different types of inheritance in C++, including single inheritance, multiple inheritance, and hierarchical inheritance. Each type of inheritance has its own advantages and disadvantages, and developers should choose the type of inheritance that best suits their needs.