Rewrite this program with the following operators overloaded: >> << + - * / == !
ID: 3716015 • Letter: R
Question
Rewrite this program with the following operators overloaded:
>> << + - * / == !=
#include
#include
#include
using namespace std;
class PayRaise
{
public:
PayRaise(char *n="",float o=0.0,float p=0.0); //Constructor has to have the same name as class ~PayRaise(); //same thing with the destructor
~PayRaise(); //Must have a Prototype of the Destructor
void printData();
void calcBonus(float);
private:
char name[20];
float oldpay,raiseper,raiseamt,newpay;
};
PayRaise::PayRaise(char*n,float o,float rp)
{
strcpy(name,n);
oldpay=o;
raiseper=rp;
raiseamt=oldpay*raiseper/100;
newpay=oldpay+raiseamt;
}
PayRaise::~PayRaise()
{
cout<<"The name of "<
}
void PayRaise::printData()
{
cout<
cout<<"The person's name is "<
cout<<"The old pay amount is $"<
cout<<"The raise percentage is "<
cout<<"The raise amount is $"<
cout<<"The new pay is $"<
}
void PayRaise::calcBonus(float bonusamt)
{
oldpay+=bonusamt;
raiseamt=oldpay*raiseper/100;
newpay=oldpay+raiseamt;
}
int main()
{
PayRaise p("CorbyBryan",50000,10);
p.printData();
cout<
float bonus;
cin>>bonus;
p.calcBonus(bonus);
cout<
p.printData();
cout<
return 0;
}
Explanation / Answer
#include <iostream>
#include <cstring>
using namespace std;
class PayRaise
{
public:
PayRaise(string n="",float o=0.0,float p=0.0); //Constructor has to have the same name as class ~PayRaise(); //same thing with the destructor
~PayRaise(); //Must have a Prototype of the Destructor
void printData();
void calcBonus(float);
PayRaise operator +(float increment);
PayRaise operator -(float decrement);
PayRaise operator *(float increment);
PayRaise operator /(float decrement);
PayRaise operator =(PayRaise p);
bool operator ==(PayRaise p);
bool operator !=(PayRaise p);
friend ostream &operator<<( ostream &,const PayRaise & );
friend istream& operator>>( istream &input, PayRaise & );
private:
string name;
float oldpay,raiseper,raiseamt,newpay;
};
PayRaise::PayRaise(string n,float o,float rp)
{
name = n;
oldpay=o;
raiseper=rp;
raiseamt=oldpay*raiseper/100;
newpay=oldpay+raiseamt;
}
PayRaise::~PayRaise()
{
}
void PayRaise::printData()
{
cout<<" The person's name is "<<name;
cout<<" The old pay amount is $"<<oldpay;
cout<<" The raise percentage is "<<raiseper;
cout<<" The raise amount is $"<<raiseamt;
cout<<" The new pay is $"<<newpay;
}
void PayRaise::calcBonus(float bonusamt)
{
oldpay+=bonusamt;
raiseamt=oldpay*raiseper/100;
newpay=oldpay+raiseamt;
}
//overload << and >> operators
ostream &operator<<( ostream &output,const PayRaise &p )
{
output << " Name : "<<p.name<<" Old Pay : $"<<p.oldpay<<" New Pay: $ "<<p.newpay;
return output;
}
istream &operator>>( istream &input, PayRaise &p ) {
input >> p.name>> p.oldpay >> p.raiseper>>p.raiseamt>>p.newpay;
return input;
}
PayRaise PayRaise::operator +(float increment)
{
this->newpay = this->oldpay + increment;
return *this;
}
PayRaise PayRaise::operator -(float decrement)
{
this->newpay = this->oldpay - decrement;
return *this;
}
PayRaise PayRaise::operator *(float increment)
{
this->newpay = this->oldpay * increment;
return *this;
}
PayRaise PayRaise::operator /(float decrement)
{
this->newpay = this->oldpay / decrement;
return *this;
}
PayRaise PayRaise::operator =(PayRaise p)
{
this->name = p.name;
this->oldpay = p.oldpay;
this->newpay = p.newpay;
this->raiseper = p.raiseper;
this->raiseamt = p.raiseamt;
return *this;
}
bool PayRaise::operator ==(PayRaise p)
{
if(this->newpay == p.newpay && this->oldpay == p.oldpay)
return true;
else
return false;
}
bool PayRaise::operator !=(PayRaise p)
{
if(this->newpay != p.newpay && this->oldpay != p.oldpay)
return true;
else
return false;
}
int main()
{
PayRaise p("CorbyBryan",50000,10);
cout<<p;
cout<<" Enter the bonus : ";
float bonus;
cin>>bonus;
p.calcBonus(bonus);
cout<<endl;
cout<<p;
cout<<endl;
PayRaise p1("John",65000,9);
cout<<p1;
if(p == p1)
cout<<" Both get same pay raise";
else
cout<<" The pay raise is different";
p1 = p1+223.2;
cout<<p1;
p1 = p1*1.5;
cout<<p1;
return 0;
}
Output:
Name : CorbyBryan Old Pay : $50000 Raise percent : 10 Raise Amount :$5000 New Pay: $ 55000
Enter the bonus :
Name : CorbyBryan Old Pay : $55600 Raise percent : 10 Raise Amount :$5560 New Pay: $ 61160
Name : John Old Pay : $65000 Raise percent : 9 Raise Amount :$5850 New Pay: $ 70850
The pay raise is different
Name : John Old Pay : $65000 Raise percent : 9 Raise Amount :$5850 New Pay: $ 65223.2
Name : John Old Pay : $65000 Raise percent : 9 Raise Amount :$5850 New Pay: $ 97500
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.