Question

In: Computer Science

The class Distance is defined below: class Distance //English Distance class { private: int feet; float...

The class Distance is defined below:

class Distance //English Distance class
{
private:
int feet;
float inches;
public: //constructor (no args)
Distance() : feet(0), inches(0.0)
{ } //constructor (two args)
Distance(int ft, float in) : feet(ft), inches(in)
{ }
};

Overloading the following operators:
a. + to add two Distance objects using member function.
b. - to subtract two Distance objects using friend function
c. << and >>
Use the following main function to test your class Distance.

int main()
{
Distance dist1, dist3, dist4; //define distances
cin>>dist1;
Distance dist2(11, 6.25); //define, initialize dist2
dist3 = dist1 + dist2; //single '+' operator
dist4 = dist1 - dist2; //friend '-' operators
//display all lengths
cout << "dist1 = ";
cout<< dist1 << endl;
cout << "dist2 = ";
cout<< dist2 << endl;
cout << "dist3 = ";
cout<< dist3 << endl;
cout << "dist4 = ";
cout<< dist4 << endl;
return 0;
}

PLEASE DO THIS WITH C++

Also, using declaration of array and using [] notation for accessing element of array

Solutions

Expert Solution


#include <iostream>
#include <iomanip>
#include <bits/stdc++.h>
using namespace std;

class Distance // English Distance class
{
private:
int feet;
float inches;

public: // constructor (no args)
Distance(): feet(0), inches(0.0)
{
} // constructor (two args)
Distance(int ft, float in): feet(ft), inches(in)
{
}
// Define stream insertion operator for Distances.

friend ostream& operator<<(ostream& str, Distance& d);

// Define stream extraction operator for Distances.

friend istream& operator>>(istream& str, Distance& d);

Distance operator-(Distance& rhs);

Distance operator+(Distance& rhs);
};

// Define + that operates on Distance + Distance.

Distance Distance::operator+(Distance& rhs)
{
int sum_feet;
double sum_inches;
sum_feet = feet + rhs.feet;
sum_inches = inches + rhs.inches;
if (sum_inches >= 12.0)
{
sum_inches -= 12.0;
++sum_feet;
}
Distance d(sum_feet, sum_inches);
return d;
}

// Define stream insertion operator for Distances.
ostream& operator<<(ostream& str, Distance& d)
{
str << d.feet << "'" << d.inches << "\"";
return str;
}
// Define stream extraction operator for Distances.

istream& operator>>(istream& str, Distance& d)
{
str >> d.feet;
if (str.get() != '\'')
{
cerr << "*** Error extracting Distance.\n";
exit(1);
}
str >> d.inches;
if (str.get() != '"')
{
cerr << "*** Error extracting Distance.\n";
exit(1);
}
return str;
}

// Define + that operates on Distance + Distance.
Distance Distance::operator-(Distance& rhs)
{
int diff_feet;
float diff_inches;
float sum = feet + inches;
float rsum = rhs.feet + rhs.inches;

float diff = abs(sum - rsum);
diff_feet = diff / 12;
diff_inches = diff - diff_feet;
Distance d(diff_feet, diff_inches);
return d;
}

int main()
{

Distance dist1, dist3, dist4; // define distances
cin >> dist1;
Distance dist2(11, 6.25); // define, initialize dist2
dist3 = dist1 + dist2; // single '+' operator
dist4 = dist1 - dist2; // friend '-' operators
// display all lengths
cout << "dist1 = ";
cout << dist1 << endl;
cout << "dist2 = ";
cout << dist2 << endl;
cout << "dist3 = ";
cout << dist3 << endl;
cout << "dist4 = ";
cout << dist4 << endl;

return 0;
}

========================================

========================================

Output:

========================================


Related Solutions

package dealership; public abstract class Vehicle { private float dealerPrice; private int year; public Vehicle(float d,...
package dealership; public abstract class Vehicle { private float dealerPrice; private int year; public Vehicle(float d, int y) { dealerPrice = d; year = y; } public float getDealerPrice() { return dealerPrice; } public int getYear() { return year; } public abstract float getStickerPrice(); } In the space below write a concrete class Car in the dealership package that is a subclass of Vehicle class given above. You will make two constructors, both of which must call the superclass constructor....
public class Square {    public static final int NUM_OF_SIDES = 4;    private float length;...
public class Square {    public static final int NUM_OF_SIDES = 4;    private float length;       public Square(float l) {        setLength(l);    }       public void setLength(float l) {        if(l >= 0) {            length = l;        }    }       public float getLength() {        return length;    }           //TODO - Add method to calculate add return area          //TODO -...
public class Clock { private int hr; private int min; private int sec; public Clock() {...
public class Clock { private int hr; private int min; private int sec; public Clock() { setTime(0, 0, 0); } public Clock(int hours, int minutes, int seconds) { setTime(hours, minutes, seconds); } public void setTime(int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if(0 <= seconds && seconds < 60) sec =...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price)...
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price) { this.brand = brand; this.price = price; } public String getBrand() { return brand; } public float getPrice() { return price; } } package compstore; public class DeskTopDeals{ // assume proper variables and other methods are here public void dealOfTheDay(Desktop[] items, int numItems){ /**************************** * your code would go here * ****************************/ } } Given the above Desktop class, write code that should go...
package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price)...
package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price) { this.contractor = contractor; this.price = price; } public String getContractor() { return contractor; } public float getPrice() { return price; } } package construction; public class ContractorBids{ // assume proper variables and other methods are here public void winningBid(Bid[] bids, int numBids){ /**************************** * your code would go here * ****************************/ } } You are doing renovations on your building, and multiple contractors...
In the following class public class Single { private float unique; ... } Create constructor, setter...
In the following class public class Single { private float unique; ... } Create constructor, setter and getter methods, and toString method. Write a program that request a time interval in seconds and display it in hours, minutes, second format. NEED THESE ASAP
public class Date { private int dMonth; //variable to store the month private int dDay; //variable...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to...
public class IntNode               {            private int data;            pri
public class IntNode               {            private int data;            private IntNode link;            public IntNode(int data, IntNode link){.. }            public int     getData( )          {.. }            public IntNode getLink( )           {.. }            public void    setData(int data)     {.. }            public void    setLink(IntNode link) {.. }         } All questions are based on the above class, and the following declaration.   // Declare an empty, singly linked list with a head and a tail reference. // you need to make sure that head always points to...
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT