c++ programs examples

C++ Program – Create a class Distance with feet and inches private data members.

a.     Define constructor to initialize the data members.

b.     Overload the ++ operator of returning class type.

c.      Create the objects and increment the objects, then show the result.

c++ programs examples
 /* 20. Create a class Distance with feet and inches private data members.
	a. Define constructor to initialize the data members.
	b. Overload the increment operator of returning class type.
	c. Creates the objects and increments the objects, then show the
	   results. */

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

Leave a Reply

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