Difference between Base class and Derived class in C++

In C++, a base class and a derived class are fundamental concepts in object-oriented programming, where a derived class inherits properties and behaviors from a base class.

What is a Base Class?

A base class (or parent class) is a class that serves as the foundation for one or more derived classes. It defines common attributes and behaviors that can be shared by multiple classes. The base class is designed to be a generalized representation, and it often includes member variables and member functions. However, the base class can also include virtual functions that can be overridden by derived classes.

Example of a base class:
class Animal { public: Animal(std::string name, int age) : name_(name), age_(age) {} void eat() { std::cout << name_ << " is eating." << std::endl; } void sleep() { std::cout << name_ << " is sleeping." << std::endl; } private: std::string name_; int age_; };

In this example, Animal is a base class with common attributes and behaviors that can be shared among various animal types.

What is a Derived Class?

A derived class (or child class) is a class that inherits properties and behaviors from a base class. It can add additional attributes and behaviors specific to its own type while reusing the attributes and behaviors defined in the base class. Derived classes can also override base class functions to provide their own implementations.

Example of a derived class:
class Dog : public Animal { public: Dog(std::string name, int age, std::string breed) : Animal(name, age), breed_(breed) {} void bark() { std::cout << name_ << " is barking." << std::endl; } private: std::string breed_; };

In this example, Dog is a derived class that inherits from the Animal base class. It adds its own attribute, breed_, and behavior, bark(), specific to dogs, while still inheriting the eat() and sleep() methods from the base class.

"is-a" relationship

The relationship between a base class and a derived class is often described as an "is-a" relationship. For example, a Dog is a type of Animal. This inheritance mechanism promotes code reusability and the organization of classes into hierarchies, allowing you to create specialized classes that build upon the functionality of more general classes.

Full Source
#include <iostream> #include <string> // Base class class Animal { public: Animal(std::string name, int age) : name_(name), age_(age) {} void eat() { std::cout << name_ << " is eating." << std::endl; } void sleep() { std::cout << name_ << " is sleeping." << std::endl; } private: std::string name_; int age_; }; // Derived class class Dog : public Animal { public: Dog(std::string name, int age, std::string breed) : Animal(name, age), breed_(breed) {} void bark() { std::cout << name_ << " is barking." << std::endl; } private: std::string breed_; }; int main() { Animal genericAnimal("Generic Animal", 5); genericAnimal.eat(); genericAnimal.sleep(); Dog myDog("Buddy", 3, "Golden Retriever"); myDog.eat(); myDog.sleep(); myDog.bark(); return 0; }

Conclusion

Base classes serve as the foundation for one or more derived classes, defining common attributes and behaviors. Derived classes inherit these characteristics while having the flexibility to add their specific attributes and behaviors, creating a hierarchy that promotes code reuse and organization.