64 696
Assignments Done
99,4%
Successfully Done
In September 2018

Answer to Question #50417 in C++ for zexy

Question #50417
a. Design a PhoneCall class that holds a phone number to which a call is placed, the length
of the call in minutes, and the rate charged per minute. Overload extraction and insertion
operators for the class.
b. Overload the == operator to compare two PhoneCalls. Consider one PhoneCall to be
equal to another if both calls are placed to the same number.
c. Create a main()function that allows you to enter 10 PhoneCalls into an array. If a
PhoneCall has already been placed to a number, do not allow a second PhoneCall to the
same number. Save the file as PhoneCall.cpp.
Expert's answer
#include <string>
#include <iostream>
using namespace std;
class PhoneCall {
private:
    string number;
    // In minutes
    int callLength;
    // Per minute
    double rate;
public:
    bool operator==(const PhoneCall &r) {
        return this->number == r.number;
    }
    friend istream& operator>> (istream& in, PhoneCall & phoneCall);
    friend ostream& operator<< (ostream& out, const PhoneCall &phoneCall);
};
istream& operator>> (istream& in, PhoneCall & phoneCall) {
    in >> phoneCall.number >> phoneCall.callLength >> phoneCall.rate;
    return in;
}
ostream& operator<< (ostream& out, const PhoneCall &phoneCall) {
    out << "Phone call: \n";
    out << "Number: " << phoneCall.number << endl;
    out << "Time(minutes): " << phoneCall.callLength << endl;
    out << "Rate(per minute): " << phoneCall.rate << endl;
    return out;
}
int main() {
    const int arrSize = 10;
    PhoneCall calls[arrSize];
    cout << "Enter " << arrSize << " phone calls(format: number length rate): \n";
    for (int i = 0; i < 10; ++i) {
        cin >> calls[i];
        // Check if there is a call to such number
        bool isCall = false;
        for (int j = i - 1; j >= 0; --j) {
            if (calls[i] == calls[j]) {
                isCall = true;
                break;
            }
        }
        if (isCall) {
            cout << "Call to this number already exists. Try once more.\n";
            --i;
        }
    }
    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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions