Answer to Question #224829 in C++ for snr

Question #224829

Create a class Distance consists of 2 integer data members as feet and inch. Define a add() method

to add 2 Distance objects which are passed as arguments. Another add() method passed with single

Distance object to add the invoked object with the passed object. Write a getInput() method to

receive the input from user and check the input, if the user gives negative input or inch value greater

than 11 show the error message and ask the user to enter it again. In main, show the result of adding

2 distance object by calling both add functions.


1
Expert's answer
2021-08-12T06:18:00-0400
#include <iostream>
#include<stdio.h>
#include<string.h>

using namespace std;

class Distance
{
int feet, inch;
 public:
 Distance (int f, int i)
 {
 feet=f;
 inch=i;
 }
 void add();
 void add1();
 void getinput();
};
void Distance::add()
{
 int dist1, dist2;
 inch=dist1+dist2;
}
void Distance::add1()
{
 int dist4;
 feet=30+dist4;
}
void Distance::getinput()
{
 int d=30,dist1, dist2, dist4;
 cout<<"\n**********************";
 cout<<"\n Enter the two distance in terms of inches: ";
 cin>>dist1>>dist2;
 cout<<"\n Enter the in terms of feet: ";
 cin>>dist4;

 inch=dist1+dist2;
 feet=d+dist4;

 if(dist1<0 || dist2<0 || dist4<0)
 {
 cout<<"\nError!!! You have entered a negative input ";
 cout<<"\nWuold you like to enter the value again";
 }
 else if(dist1>11 || dist2>11)
 {
 cout<<"\nError!!! The inch value is greater than 11 ";
 cout<<"\nWuold you like to enter the value again";
 }
 else{
 cout<<"\nThe sum of the two inches: "<<inch;
 cout<<"\nThe distance: "<<feet;
 }
}

int main()
{
    int f, i;
    Distance d(f, i);
    d.getinput();
    d.add();
    d.add1();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS