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....
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...
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...
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
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
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2;...
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2; long ris; public ProductThreads(String name, int [] v1, int [] v2, int begin, int end) { setName(name); this.v1 = v1; this.v2 = v2; this.begin = begin; this.end = end; this.ris = 0; } public void run() { System.out.println("Thread " + Thread.currentThread().getName() + "[" + begin + "," + end + "] started"); ris = 1; for(int i = begin; i <= end; i++) ris...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data field named id for the account. ■ A private float data field named balance for the account. ■ A private float data field named annualInterestRate that stores the current interest rate. ■ A constructor that creates an account with the specified id (default 0), initial balance (default 100), and annual interest rate (default 0). ■ The accessor and mutator methods for id, balance, and...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT