Assume that the cell users are two kinds – those with a postpaid option and those with a prepaid option. Postpaid gives a fixed free talk time and the rest is computed at the rate of N2.90 per pulse. Prepaid cards have a fixed talk time.
Define a class Cell_user as a base class and derive the hierarchy of classes. Define member functions and override them wherever necessary to
(i) retrieve the talk time left for each user.
(ii) print the bill in a proper format containing all the information for the postpaid user.
#include <iostream>
#include <string>
using namespace std;
class Cell_user{
protected:
string user;
int talkTime;
public:
Cell_user(string name, int time) : user(name), talkTime(time) {}
int getTalkTime(){
return talkTime;
}
virtual void printBill() = 0;
void talk(int time){
if(talkTime - time > 0){
talkTime -= time;
}
else{
talkTime = 0;
}
}
};
class Postpaid: public Cell_user {
public:
Postpaid(string name, int time) : Cell_user(name, time) {}
void printBill(){
//assuming the free fixed talktime is 30
float bill = 0;
cout<<"Name: "<<user<<endl;
if(talkTime > 30){
bill = (talkTime - 30) * 2.90;
}
cout<<"Type: Postpaid\n";
cout<<"Talktime: "<<talkTime<<endl;
cout<<"Bill: "<<bill<<endl;
}
};
class Prepaid: public Cell_user {
Prepaid(string name, int time) : Cell_user(name, time) {}
};
int main(){
Postpaid lucas("Lucas", 60);
lucas.talk(2);
cout<<"Time left: "<<lucas.getTalkTime()<<"\n\n";
lucas.printBill();
return 0;
}
Comments
Leave a comment