Saturday, October 28, 2017

Example of inheritance programming


[1] Define class p with two variables as data members. Define another class d which inherits the data members of p. Multiply two variables and display it.
#include<iostream.h>
#include<conio.h>
class p
{
  protected:
        int  a,b;
  public:
        void input()
        {
                cout<<"\n Enter a,b:";
                cin>>a>>b;
        }
        void display()
        {
                cout<<"\n a : "<<a;
                cout<<"\t b : "<<b;
        }
};
class d : public p
{
        int  c;
  public:
        void input()
        {
                p :: input();
                c = a * b;
        }
        void display()
        {
                p :: display();
                cout<<"\t Mul : "<<c;
        }
};
void main()
{
        clrscr();
        d d1;
        d1.input();
        d1.display();
        getch();
}
OUTPUT:
Enter a,b:10   20
a : 10  b : 20  Mul : 200

No comments:

Post a Comment