Long-distance carrier charge following rate for telephone
Question
C++
a Long-distance carrier charge the following rate for telephone call
starting time rate per minute
00:00-06:59 0.05
07:00-19:00 0.45
19:01-23:59 0.20
write the program that ask for the starting time and the number of minutes of the call and display the charges.
instruction:
define the longdistance classes composed of two attributes, one is an instance of the contactinfo class that stores the name and phone number of the owner of the invoice (both attributes are dynamically created) and the other is an instance of the time class which is composed of minutes and seconds. validate entry in hours (0-24) and minutes (0-60) the classes have to contain.
constructor, destructor, copy constructor, mutator functions, accessor function, member function, member operator (=)
, istream.ostream Operator (<<, >>)
Summary
In the given question we have to ask the user for the call he/she is going to do, for which he is asked for the name and the phone number. After this information is has been taken it will checked for the starting time for the rate per minute on the phone call and the number of minutes on the call is also be taken by the user and then after all this execution the total charges for the call by the user is printed on the screen as output.
Explanation
Basically here we have taken the class name as contactinfo with the data members name as ‘name’ and ‘phone’. Also we have set the methods setter and getter for the each attributes. We have also set the constructors for the define class contactinfo. Next we have set the function named as getContactInfo to have the details of the user, and extraction operator is also overloaded to print the contact details of the user. Next we have defined a new class named as Time which has its data members as hours and minutes. And we have set the constructors, mutters and assessors to each attribute of the class. We have used the agetTime() function to take the input from the user as its starting time. And finally we have overloaded the extraction operator to display the time. We have created the new class named as long distance to have the object of time and of the class contactinfo as its data member. And finally at the last we have display method which will print the total charges of the call by the user on the screen as output.
Code
#include <iostream> using namespace std; /* to implement the contact info class */ class contactinfo { public: /* members */ string name, phone; contactinfo(){} /* constructor */ contactinfo(string n,string p) { name=n; phone=p; } /* to get the the contact info */ void getContactInfo() { cout<<"Enter name: "; cin>>name; cout<<"Enter phone number: "; cin>>phone; } /* accessor for name */ string getName() { return name; } /* accessor for phone number */ string getPhone() { return phone; } /* mutator for name */ void setName(string n) { name=n; } /* mutator for phone number */ void setPhone(string p) { phone=p; } /* copy constructor */ contactinfo(contactinfo &c) { name=c.name; phone=c.phone; } friend ostream &operator<<(ostream &output, const contactinfo &c); ~contactinfo(){} }; /* overloading the extraction operator */ ostream &operator<<(ostream &output, const contactinfo &c) { output<<"Name: "<<c.name<<"\nPhone number: "<<c.phone<<endl; } /* to implement the time class */ class Time { public: /* members */ int hours,minutes; /* constructor */ Time(){ hours=0; minutes=0; } /* constructor */ Time(int h, int m) { hours=h; minutes=m; } /* copy constructor */ Time(Time &t) { hours=t.hours; minutes=t.minutes; } /* accessor for hours */ int getHours() { return hours; } /* accessor for minutes */ int getMinutes() { return minutes; } /* mutator for hours */ void setHours(int h) { hours=h; } /* mutator for minutes */ void setMinutes(int m) { minutes=m; } /* to get the data */ void getTime() { cout<<"Enter starting time:"<<endl; while(true) { cout<<"Enter hours: "; cin>>hours; if(hours>=0 && hours<=24) break; cout<<"Invalid hours!! Try again"<<endl; } while(true) { cout<<"Enter minutes: "; cin>>minutes; if(minutes>=0 && minutes<=60) break; cout<<"Invalid minutes!! Try again"<<endl; } } friend ostream &operator<<(ostream &output, const Time &t); ~Time(){} }; /* overloading the extraction operator */ ostream &operator<<(ostream &output, const Time &t) { output<<"Time: "<<t.hours<<":"<<t.minutes<<endl; } /* to implement the long distance class */ class longdistance { public: /* members */ contactinfo c; Time t; float rate; int numOfMins; /* to get the data */ void getdata() { c.getContactInfo(); t.getTime(); cout<<"Enter the number of minutes of call: "; cin>>numOfMins; } /* to calculate the rate per minute based on starting time */ void ratePerMinute() { if(t.hours>=0 && t.hours<7) rate=0.05; else if(t.hours<19 || (t.hours==19 && t.minutes==0)) rate=0.45; else rate=0.20; } /* to display the output */ void display() { cout<<endl; cout<<c<<t; cout<<"Number of minutes: "<<numOfMins<<endl; cout<<"Rate per minute: "<<rate<<endl; cout<<"Total charges: "<<numOfMins*rate<<endl; } }; /* driver code */ int main() { longdistance l; l.getdata(); l.ratePerMinute(); l.display(); }
Output
Also read, for the C++ program show on the next page, answer the questions that follow