c++ programs examples

a.     Define constructor to initialize the data members.

b.     Define a function to add the data members.

c.      Display the result of the addition using object.

c++ programs examples
/* 12. Create a class Distance with feet and inches private data members
       a. Define constructor to initialize the data members.
       b. Define a function to add the data members.
       c. Display the result of the addition using object. */

 #include<iostream.h>
 #include<conio.h>
 class Distance
 {
  private:
   int feet,inches;
  public:
   Distance()
   {
    feet=inches=0;
    }
   void input()
   {
    cout<<"Enter feet="<<endl;
    cin>>feet;
    cout<<"Enter inches="<<endl;
    cin>>inches;
    }
   Distance operator+(Distance d)
   {
    Distance temp;
    temp.feet=feet+d.feet;
    temp.inches=inches+d.inches;
    return temp;
    }
   void display()
   {
    cout<<"Distance is "<<feet<<" feet  and"<<" "<<inches<<" inches"<<endl;
    }
 };
 void main()
 {
  Distance d1,d2,d3;
  clrscr();
  d1.input();
  d2.input();
  d3=d1+d2;
  d3.display();
  getch();
  }

Leave a Reply

Your email address will not be published. Required fields are marked *