Hi there, we’re Harisystems
"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".
For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.
1C++ Inheritance
C++ inheritance is a powerful feature of object-oriented programming (OOP) that allows you to create new classes based on existing classes. Inheritance promotes code reuse, modularity, and supports the "is-a" relationship between classes. In this article, we will explore the usage of inheritance in C++ with examples.
1. Base and Derived Classes
In C++, a base class is the class from which other classes can be derived. A derived class is a class that is derived from a base class, inheriting its members and behaviors. Here's an example:
#include <iostream>
#include <string>
// Base class
class Animal {
protected:
std::string name;
public:
Animal(const std::string& _name) : name(_name) {}
void eat() {
std::cout << name << " is eating." << std::endl;
}
};
// Derived class
class Dog : public Animal {
public:
Dog(const std::string& _name) : Animal(_name) {}
void bark() {
std::cout << name << " is barking." << std::endl;
}
};
int main() {
Dog dog("Buddy");
dog.eat();
dog.bark();
return 0;
}
In the above code, we define a base class Animal
with a protected member variable name
and a member function eat()
. We then define a derived class Dog
that inherits from the Animal
class using the public
access specifier. The Dog
class adds a member function bark()
. In the main()
function, we create an object of the Dog
class and call both the eat()
and bark()
member functions.
2. Access Specifiers and Inheritance
C++ provides access specifiers (public
, private
, and protected
) to control the access to base class members in derived classes. Here's an example:
#include <iostream>
class Vehicle {
protected:
int wheels;
public:
Vehicle(int _wheels) : wheels(_wheels) {}
void displayWheels() {
std::cout << "Number of wheels: " << wheels << std::endl;
}
};
class Car : public Vehicle {
private:
int doors;
public:
Car(int _wheels, int _doors) : Vehicle(_wheels), doors(_doors) {}
void displayDoors() {
std::cout << "Number of doors: " << doors << std::endl;
}
};
int main() {
Car car(4, 2);
car.displayWheels();
car.displayDoors();
return 0;
}
In the above code, we define a base class Vehicle
with a protected member variable wheels
and a member function displayWheels()
. We then define a derived class Car
that inherits from the Vehicle
class using the public
access specifier. The Car
class adds a private member variable doors
and a member function displayDoors()
. In the main()
function, we create an object of the Car
class and call both the displayWheels()
and displayDoors()
member functions.
3. Overriding Base Class Members
Derived classes can override base class members to provide their own implementation. Here's an example:
#include <iostream>
class Shape {
public:
virtual void displayInfo() {
std::cout << "This is a shape." << std::endl;
}
};
class Circle : public Shape {
public:
void displayInfo() override {
std::cout << "This is a circle." << std::endl;
}
};
int main() {
Shape* shape = new Circle();
shape->displayInfo();
delete shape;
return 0;
}
In the above code, we define a base class Shape
with a virtual member function
displayInfo()
. We then define a derived class Circle
that overrides the displayInfo()
member function. In the main()
function, we create a pointer of type Shape
and assign it to a new Circle
object. We then call the displayInfo()
member function using the pointer. This demonstrates dynamic polymorphism, where the derived class's implementation is invoked at runtime based on the actual object type.
Inheritance is a powerful feature in C++ that allows you to create new classes based on existing ones. It promotes code reuse, modularity, and supports the "is-a" relationship between classes. Utilize inheritance to design and implement complex systems with ease in your C++ programs.
4.5L
Learners
20+
Instructors
50+
Courses
6.0L
Course enrollments
Future Trending Courses
When selecting, a course, Here are a few areas that are expected to be in demand in the future:.
Future Learning for all
If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.
View CoursesMost Popular Course topics
These are the most popular course topics among Software Courses for learners