Skip to main content

Introduction to Inheritance in C++

Inheritance in C++ is one of the major aspects of Object Oriented Programming (OOP). It is the process by which one object can inherit or acquire the features of another object.

In C++ class and structure can use inheritance. It means we can make one class (known as derived class) to acquire or inherit the features of another class (known as base class).

Base class has general features common to all the derived classes and derived class (apart from having all the features of the base class) adds specific features. This enables us to form a hierarchy of classes.

Ex. if we define a class ‘computer’ then it could serve as the base class for defining other classes such as ‘laptops’, ‘desktops’ etc.. This is because as you know that laptops have all the features of computers and so have desktops (and so should their classes) but they have their specific features too. So, rather than defining all the features of such classes from scratch, we make them inherit general features from other classes.

General Form of Inheritance:


  class derived-class:access-specifier base-class
  {
    ...
    ...
    ...
  };

Here access-specifier can be any one of the three private, public and protected. These will not be discussed here.

For this article we’ll be using public for all the classes we define. This means, all the public members of the base class will be public to the derived class too.

Now let’s take the example of ‘computer’ class a bit further by actually defining it.


  class computer
  {
    int speed;
    int main_memory;
    int harddisk_memory;

  public:
    void set_speed(int);
    void set_mmemory(int);
    void set_hmemory(int);
    int get_speed();
    int get_mmemory();
    int get_hmemory();
  };

As you can see, the features (properties and functions) defined in the class computer is common to laptops, desktops etc. so we make their classes inherit the base class ‘computer’.


  class laptop:public computer
  {
    int battery_time;
    float weight;

  public:
    void set_battime(int);
    void set_weight(float);
    int get_battime();
    float get_weight();
  };

This way the class laptop has all the features of the base class ‘computer’ and at the same time has its specific features (battery_time, weight) which are specific to the ‘laptop’ class.

If we didn’t use inheritance we would have to define the laptop class something like this:


  class laptop
  {
    int speed;
    int main_memory;
    int harddisk_memory;

    int battery_time;
    float weight;

  public:
    void set_speed(int);
    void set_mmemory(int);
    void set_hmemory(int);
    int get_speed();
    int get_mmemory();
    int get_hmemory();

    void set_battime(int);
    void set_weight(float);
    int get_battime();
    float get_weight();
  };

And then again we would have to do the same thing for desktops and any other class that would need to inherit from the base class ‘computer’.

Thanks to inheritance, we don’t need to do all this!

I think now you’ve got the essence of what inheritance is, so taking the example of the ‘computer’ class a bit more further, we implement it into an example program listed below:


  // Introduction to Inheritance in C++
  // ------------------------
  // An example program to
  // demonstrate inheritance in C++
  #include<iostream.h>

  // base class for inheritance
  class computer
  {
    float speed;
    int main_memory;
    int harddisk_memory;

  public:
    void set_speed(float);
    void set_mmemory(int);
    void set_hmemory(int);
    float get_speed();
    int get_mmemory();
    int get_hmemory();
  };

  // -- MEMBER FUNCTIONS -- 
  void computer::set_speed(float sp)
  {
    speed=sp;
  }

  void computer::set_mmemory(int m)
  {
    main_memory=m;
  }

  void computer::set_hmemory(int h)
  {
    harddisk_memory=h;
  }

  int computer::get_hmemory()
  {
    return harddisk_memory;
  }

  int computer::get_mmemory()
  {
    return main_memory;
  }

  float computer::get_speed()
  {
    return speed;
  }
  // --    ENDS     --

  // inherited class
  class laptop:public computer
  {
    int battery_time;
    float weight;

  public:
    void set_battime(int);
    void set_weight(float);
    int get_battime();
    float get_weight();
  };

  // -- MEMBER FUNCTIONS --
  void laptop::set_battime(int b)
  {
    battery_time=b;
  }

  void laptop::set_weight(float w)
  {
    weight=w;
  }

  int laptop::get_battime()
  {
    return battery_time;
  }

  float laptop::get_weight()
  {
    return weight;
  }
  // --    ENDS    --

  void main(void)
  {
    computer c;
    laptop l;

    c.set_mmemory(512);
    c.set_hmemory(1024);
    c.set_speed(3.60);

    // set common features
    l.set_mmemory(256);
    l.set_hmemory(512);
    l.set_speed(1.8);

    // set specific features
    l.set_battime(7);
    l.set_weight(2.6);

    // show details of base class object
    cout<<"Info. of computer class\n\n";
    cout<<"Speed:"<<c.get_speed()<<"\n";
    cout<<"Main Memory:"<<c.get_mmemory()<<"\n";
    cout<<"Hard Disk Memory:"<<c.get_hmemory()<<"\n";

    //show details of derived class object
    cout<<"\n\nInfo. of laptop class\n\n";
    cout<<"Speed:"<<l.get_speed()<<"\n";
    cout<<"Main Memory:"<<l.get_mmemory()<<"\n";
    cout<<"Hard Disk Memory:"<<l.get_hmemory()<<"\n";
    cout<<"Weight:"<<l.get_weight()<<"\n";
    cout<<"Battery Time:"<<l.get_battime()<<"\n";
  }

Popular posts from this blog

The Best Way(s) to Comment out PHP/HTML Code

PHP supports various styles of comments. Please check the following example: <?php // Single line comment code (); # Single line Comment code2 (); /* Multi Line comment code(); The code inside doesn't run */ // /* This doesn NOT start a multi-line comment block /* Multi line comment block The following line still ends the multi-line comment block //*/ The " # " comment style, though, is rarely used. Do note, in the example, that anything (even a multi-block comment /* ) after a " // " or " # " is a comment, and /* */ around any single-line comment overrides it. This information will come in handy when we learn about some neat tricks next. Comment out PHP Code Blocks Check the following code <?php //* Toggle line if ( 1 ) {      // } else {   ...

How to Create an HTML Form Linked with MySQL Database in PHP

If you're looking for example code and detailed discussion on how to create an HTML form that stores data in a MySQL database using PHP then this post might be what you're looking for. I assume that you're familiar with basic HTML, CSS, PHP coding, and  MySQL. I am going to divide this small project into two parts: The HTML form itself that takes input from the user and the PHP script that saves it into the database A table that displays the user-added data that has been saved in the database. We'll be dealing with the first part in this tutorial. Again I'd like to break this problem into a few parts so that it's easier for you to understand and probably gives you an insight into how breaking up a problem into smaller chunks can help make things clearer in your mind. Let's think about it, there is an HTML form (that is HTML code), then there is the PHP code that deals with the user-input data, and the MySQL database itself. For this tutorial, we'll b...

Fix For Toshiba Satellite "RTC Battery is Low" Error (with Pictures)

RTC Battery is Low Error on a Toshiba Satellite laptop "RTC Battery is Low..." An error message flashing while you try to boot your laptop is enough to panic many people. But worry not! "RTC Battery" stands for Real-Time Clock battery which almost all laptops and PCs have on their motherboard to power the clock and sometimes to also keep the CMOS settings from getting erased while the system is switched off.  It is not uncommon for these batteries to last for years before requiring a replacement as the clock consumes very less power. And contrary to what some people tell you - they are not rechargeable or getting charged while your computer or laptop is running. In this article, we'll learn everything about RTC batteries and how to fix the error on your Toshiba Satellite laptop. What is an RTC Battery? RTC or CMOS batteries are small coin-shaped lithium batteries with a 3-volts output. Most laptops use ...