Simple example of Inheritance

#include <iostream>
using namespace std;
class A{
      public:
      int i,j;
      void ij()
      {
           cout<<"i and j is: "<<i<<" "<<j<<endl;
      }
};
class B : public A{
      public:
             int k;
             void kk(){
                  cout<<"k is: "<<k<<endl;
             }
             void sum(){
                  cout<<"The sum of i(new value), j(new value) and 
k(new value) is: "<<i+j+k<<endl;
             }
};
int main()
{
    
    int i,j,k;
    A aa;
    B bb;
    aa.i=7;
    aa.j=5;
    aa.ij();
    bb.k=100;
    bb.kk();
    bb.i=8;
    bb.j=9;
    bb.k=10;
    bb.sum();
    system("pause");
    return 0;
}


This is the output:
i and j is: 7 5
k is: 100
The sum of i(new value), j(new value) and k(new value) is: 27

0 comments:

Post a Comment