Inheritance in C++

 Inheritance:

It is the process of joining two or more classes and we can access the values from both classes.

Types of Inheritance:

1) Single inheritance

2) Multilevel inheritance

3) Multiple inheritances

4) Hierarchical inheritance

5) Hybrid inheritance

Benefits of inheritance:

  • Software reusability
  • Information hiding
  • Code sharing

Syntax:

  1. class a

    {

              //Base class

    }

    class b: public a

    {

              //Derived class

    }

    Object creation:

    Derived_class_name object_name;

    object_name.function(parameter list); //If use constructor then NO need this

Example:

  1. #include<iostream.h>

    #include<conio.h>

    class item // Base class

    {

    public:

    char item_name[10];

    int qty;

    float price;

    double amt;

    item() //Constructor

    {

    cout<<"\n Enter item name : ";

    cin>>item_name;

    cout<<"\n Enter number of quantity : ";

    cin>>qty;

    cout<<"\n Enter item price : ";

    cin>>price;

    amt=qty*price;

    }

    };

    class display : public item // Derived class

    {

    public:

    display()

    {

    cout<<"\n \n Output of Item details with total amount........... "<<endl<<endl;

    cout<<"\n Item name : "<<item_name<<endl;

    cout<<"\n Quantity : "<<qty<<endl;

    cout<<"\n Item price : "<<price<<endl;

    cout<<"\n Total amout : "<<amt<<endl;

    }

    };

    void main()

    {

    clrscr();

    display d;//Object

    getch();

    }

Output:

Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me