Inheritance in C++

Inheritance is the property of representing a base class and its derived classes as a hierarchy of classes with the most general functions of the class vested at the base or parent class and then specifying added qualities in each derived class. Below is an example of how we can represent a base class and its child class in C++.

Base Class

class Fruit {
              string shape;
              string taste;
              string smell;
              double weight;
              string name;
     public:
              void set_name(string name);
              string get_name();
              void set_shape(string shape);
              string get_shape();
              void set_taste(string taste);
              string get_taste();
              void set_smell(string smell);
              string get_smell();
              void set_weight(double weight);
              double get_weight();
};

As shown above, we represent the real world object, Fruit, as the base class. Its called the base class since we define the basic features that makes a fruit and what operations can we perform on it to create it – such as to give it a name, shape, taste etc.

Inherited Class

class Citrus_Fruit : public fruit {
              string color;
              bool juicy;
     public:
              void set_color(string color);
              string get_color();
              void set_juicy(bool juicy);
              bool get_juicy();
              
};

As you see above, we created a child class called Citrus_Fruit that inherits from the base or parent class Fruit using the ‘:’ operator. The idea is that we extend the features of a generic concept called Fruit to be inherited by a specific feature namely Citrus_Fruit without having to re-define the high level features (viz. name, taste etc) again. Another fact is that we affirm the rules of encapsulation by limiting the generic and specific properties/operations to either the base or child class.

By inheritance, each object can precisely represent its own classification, with all the general facts rested in the base class. This in turn enables hierarchical classification. Inheritance concept also allows for runtime polymorphism by mechanism of virtual functions.

Below is a complete C++ program that utilizes the concept of inheritance to define the real world concepts such as Fruit and its specific existence as a Citrus_Fruit.

#include <iostream>

using namespace std;

//Base class
class Fruit {
              string shape;
              string taste;
              string smell;
              double weight;
              string name;
     public:
              void set_name(const std::string& input);
              string get_name();
              void set_shape(string shape);
              string get_shape();
              void set_taste(string taste);
              string get_taste();
              void set_smell(string smell);
              string get_smell();
              void set_weight(double weight);
              double get_weight();
};
void Fruit::set_name(const std::string& input)
{      
       name = input;
}
string Fruit::get_name()
{
      return name;
}
void Fruit::set_shape(string s)
{      
      shape = s;
}
string Fruit::get_shape()
{
      return shape;
}
void Fruit::set_smell(string s)
{      
      smell = s;
}
string Fruit::get_smell()
{
      return smell;
}
void Fruit::set_weight(double w)
{      
        weight = w;
}
double Fruit::get_weight()
{
      return weight;
}
void Fruit::set_taste(string t)
{
      
      taste = t;
}
string Fruit::get_taste()
{
      return taste;
}
//Inherited class
class Citrus_Fruit : public Fruit {
              string color;
              string texture;
              
     public:
              void set_color(string color);
              string get_color();
              void set_texture(string texture);
              string get_texture();
              
};

void Citrus_Fruit::set_color(string s)
{      
      color = s;
}
string Citrus_Fruit::get_color()
{
      return color;
}
void Citrus_Fruit::set_texture(string t)  
{
     texture = t;
}
string Citrus_Fruit::get_texture()
{
      return texture;
}
main() {
        
       Citrus_Fruit cf;
       string shape;
       string taste;
       string smell;
       double weight;
       string name;
       string color;
       string texture;

       cout << "Enter the name of a citrus fruit > ";
       cin >> name;
       cf.set_name(name);

       cout << "Enter the shape of the fruit > ";
       cin >> shape;
       cf.set_shape(shape);

       cout << "Enter the taste of the fruit > ";
       cin >> taste;
       cf.set_taste(taste);

       cout << "Enter the smell of the fruit > ";
       cin >> smell;
       cf.set_smell(smell);

       cout << "Enter the weight of the fruit > ";
       cin >> weight;
       cf.set_weight(weight);

       cout << "Enter the color of the fruit > ";
       cin >> color;
       cf.set_color(color);

       cout << "Enter the texture of the fruit > ";
       cin >> texture;
       cf.set_texture(texture);
       
       cout << "The Citrus Fruit you like most is: "
       << cf.get_name()
       << " which is " << cf.get_shape()
       << " in shape and has a "
       << " weight of " << cf.get_weight()
       << ".\n It has " << cf.get_taste()
       << " taste and " << cf.get_color()
       << " color and " << cf.get_texture()
       << " texture.\n";
}

Output:
Enter the name of a citrus fruit > Orange         
Enter the shape of the fruit > Sphere   
Enter the taste of the fruit > Sour_And_Sweet 
Enter the smell of the fruit > Sweet  
Enter the weight of the fruit > 10 
Enter the color of the fruit > Orange 
Enter the texture of the fruit > Smooth                                                                                                              
The Citrus Fruit you like most is --> Orange which is Sphere in shape and has a  weight of 10.   
 It has Sour_And_Sweet taste and Orange color and Smooth texture.   

Add a Comment

Your email address will not be published. Required fields are marked *