Ad

Tuesday, July 31, 2007

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";
  }

19 comments:

  1. Anonymous3:58 PM

    very clear and simple example explaining concept of inheritance
    Thanks.

    ReplyDelete
  2. A great example of inheritance. Really Appreciate it. hayyan

    ReplyDelete
  3. Anonymous6:56 PM

    Good stuff by arvind Gupta. You Rock

    ReplyDelete
  4. Hi hayyan & Anonymous,

    Thanks very much!

    ReplyDelete
  5. Abhishek3:54 PM

    amazing excellent,
    i have no any words,,

    Thank You So Much....

    ReplyDelete
  6. Hi Abhishek,

    It's my pleasure!

    ReplyDelete
  7. Anonymous10:50 PM

    thanks!

    ReplyDelete
  8. dave otewa10:56 AM

    thanks for the good example

    ReplyDelete
  9. Anonymous5:42 PM

    IS IT ARVIND GUPTA who has placed dis???
    thanks a lot... it was very informative.. meghna

    ReplyDelete
  10. Anonymous10:03 PM

    very very good work bro........
    i appreciate it.......
    keep it up

    regard
    Sajid Khan

    ReplyDelete
  11. Anonymous12:21 AM

    nice example

    ReplyDelete
  12. Anonymous4:36 PM

    nice example......

    ReplyDelete
  13. Sanjay Mishra Katni M.Sc.(C.S.)2:55 PM

    Execellant Example with detail info.

    ReplyDelete
  14. Mrs. D . Geetha4:58 PM

    thaks yar. the way in which u xplain is simple and understandable.

    ReplyDelete
  15. Anonymous8:02 PM

    thank u buddy ....

    ReplyDelete
  16. Anonymous8:03 PM

    thank you

    ReplyDelete
  17. Anonymous12:29 PM

    Very good thnkx

    ReplyDelete
  18. very good bro it is the simplest,neat and clean example

    ReplyDelete
  19. its the most easy clear and simple example i have ever seen.....thank u very much for sharing....

    ReplyDelete

You are free to comment anything, although you can comment as 'Anonymous' it is strongly recommended that you supply your name. Thank You.

Please don't use abusive language.