I am trying to understand the concepts of friend
functions, overloaded operators, and inheritance in C++. I am very confused on the specifics used for coding, since I am fairly new in programming, and working in the Visual Studio C++ environment in writing code.
The following is the project details for writing a program in Visual Studio C++:
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. In this program, overload the==
operator to compare twoPhoneCall
s. Consider onePhoneCall
to be equal to another if both calls are placed to the same number. Also, create amain()
function that allows you to enter tenPhoneCall
s into an array. If aPhoneCall
has already been placed to a number, do not allow a secondPhoneCall
to the same number.
I really need some feedback on the following code:
#include <iostream>
#include <string>
using namespace std;
class PhoneCall {
private:
string phonenumber;
double perminuterate;
double calldurationminutes;
public:
bool operator==( const PhoneCall &n ) const;
friend ostream & operator<<( ostream &f, const PhoneCall &n );
friend istream & operator>>( istream &f, PhoneCall &n );
};
bool PhoneCall::operator==( const PhoneCall &n ) const {
return phonenumber == n.phonenumber;
};
ostream & operator<<( ostream &f, const PhoneCall &n ) {
f << "Phone number: " << n.phonenumber <<
", Duration: " << n.calldurationminutes <<
" minutes, Rate: " << n.perminuterate << endl;
return f;
}
istream & operator>>( istream &f, PhoneCall &n ) {
f >> n.phonenumber;
f >> n.calldurationminutes;
f >> n.perminuterate;
return f;
}
int main( ) {
PhoneCall a[10];
cout << "Enter 10 phone numbers, duration in minutes, and the per-minute rates." <<
endl << "Separate each with a space and then hit enter to complete it." << endl;
for ( int i= 0; i < 10; ) {
cin >> a[i];
int j;
for ( j= 0; j < i; ++j )
if ( a[i] == a[j] ) {
cout << "Duplicate number information ignored. Try again." << endl;
break;
}
if ( j == i ) ++i;
}
for ( int i= 0; i < 10; ++i )
cout << a[i];
system("pause");
return 0;
}
main()
is a little strange. \$\endgroup\$