Close
Close

Add two distances in inch-feet by creating a class named ‘AddDistance’.

   Totobad Kunta

I need help


Answers

  •   

    I have written the following code along with comments for the problem.

    #include <iostream>
    
    using namespace std;
     
    class AddDistance {
        private:
            int feet;
            int inch;
        public:
            void setDistance();
            void getDistance();
            AddDistance addDistance( AddDistance d );
    };
     
    // function to show print distance
    void AddDistance::setDistance() {
        cout << " feet: "; cin >> feet;
        cout << "inches: "; cin >> inch;
    }
     
    // function to get distance from user
    void AddDistance::getDistance() {
        cout << "feet: " << feet;
        cout << " inches: " << inch;
    }
     
    // function to add 2 distances 
    AddDistance AddDistance::addDistance( AddDistance d ) {
        AddDistance dist;
     
        dist.feet = feet + d.feet;
        dist.inch = inch + d.inch;
     
        if(dist.inch >= 12) {
            dist.feet++;
            dist.inch -= 12;        
        }
        return dist;    
    }
    
    int main() {
        AddDistance d1, d2, d3;
     
    	// taking the two distances as input from user
        cout << "Enter length of Distance 1: " << endl;
        d1.setDistance();
         
        cout << "Enter length of Distance 2: " << endl;
        d2.setDistance();
     
        // adding the distances
        d3 = d1.addDistance(d2);
     
    	// displaying resultant distance
        cout << "Sum of Distance 1 and Distance 2:" << endl;
        d3.getDistance();
        return 0;
    }
    

     



  •    amityjyotsna

    That's what we needed, that's really cool, thanks for sharing the post 

      bubble shooter


    • thank you so much
      - priya_krystel

  •    vignesh4u

    java program

    import java.util.*;

    class Distance

    {

    double feet , inches;

    void getdistance() {

    Scanner in = new Scanner(System.in);

    System.out.print("Enter feet> ");

    feet = in.nextDouble();

    System.out.print("Enter inches> ");

    inches = in.nextDouble();

    }

         void addDistance(Distance d1, Distance d2)

    {

    double sumofFeet = d1.feet + d2.feet;

    double sumofInch = d1.inches + d2.inches;

    System.out.println(sumofFeet + " " + sumofInch);

    }

    }

    public class Main

    {

    public static void main (String[] args)

    {

       Distance d1 = new Distance();

       Distance d2 = new Distance();

    //Distance d3 = new Distance();

    System.out.println("Enter first distance ");

    d1.getdistance(); System.out.println("Enter second distance ");

    d2.getdistance(); d1.addDistance(d1,d2); //d3.addDistance(d1,d2);

    } }


    • you can also create a third object to call the getdistance function if you uncomment the d3 object code
      - vignesh4u
    • thank you so much
      - priya_krystel

  •    seoexpertim

    Thank a lot. You have done excellent job. I enjoyed your blog . Nice efforts Botany showflat



Ask Yours
Post Yours
Write your answer