|
 |
|
 |
10-16-2006, 03:10 PM
|
#1 (permalink)
|
|
Recruit
Join Date: Sep 2006
Posts: 12
|
class, overloading operators, and Friend function
Okay... I think I know my problem here
tax = rate*income/100;
is not working because I did not set those with specific value
Like: rate(15) or income(4000)
Well... is there any way to make this program so that it would be functional?
I asked my professor for help, but he does not know anymore than I do.
Any help will be apprecated. Save me!
p.s. if you anything related to programming projects from "Problem Solving: The object of programming, 4th edition" by walter savitch, please send it to me.
Result:
Enter the annual income: $4000.00
Enter the state tax rate: 15%
tax rate = 15%
tax = $0.00
net annual income = $4000.00
Code:
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;
//Class for amounts of money in U.S. currency.
class Money
{
public:
friend Money operator +(const Money& amount1, const Money& amount2);
friend Money operator -(const Money& amount1, const Money& amount2);
friend Money operator *(const Money& amount1, const Money& amount2);
friend Money operator /(const Money& amount1, const Money& amount2);
friend istream& operator >>(istream& ins, Money& the_object);
friend ostream& operator <<(ostream& outs, const Money& a_percent);
friend bool operator ==(const Money& amount1, const Money& amount2);
Money(long dollars, int cents);
Money(long dollars);
Money( );
double get_value( ) const;
double value;
void input(istream& ins);
void output(ostream& outs) const;
private:
long all_cents;
};
int digit_to_int(char c);
int main( )
{
Money income, tax, total, rate;
cout << "Enter the annual income: ";
income.input(cin);
cout << "Enter the state tax rate: ";
cin >> rate;
cout << "tax rate = ";
cout << rate;
cout << endl;
tax = rate*income/100;
cout << "tax = ";
tax.output(cout);
cout << endl;
cout << "net annual income = ";
total = income - tax;
total.output(cout);
int exit;
cin >> exit;
return 0;
}
Money operator -(const Money& amount1, const Money& amount2)
{
Money temp;
temp.all_cents = amount1.all_cents - amount2.all_cents;
return temp;
}
Money operator +(const Money& amount1, const Money& amount2)
{
Money temp;
temp.all_cents = amount1.all_cents + amount2.all_cents;
return temp;
}
Money operator *(const Money& amount1, const Money& amount2)
{
Money temp;
temp.all_cents = (amount1.all_cents*amount2.all_cents);
return temp;
}
Money operator /(const Money& amount1, const Money& amount2)
{
Money temp;
temp.all_cents = amount1.all_cents / amount2.all_cents;
return temp;
}
bool operator ==(const Money& amount1, const Money& amount2)
{
return (amount1.all_cents == amount2.all_cents);
}
Money::Money(long dollars, int cents)
{
if(dollars*cents < 0) //If one is negative and one is positive
{
cout << "Illegal values for dollars and cents.\n";
exit(1);
}
all_cents = dollars*100 + cents;
}
Money::Money(long dollars) : all_cents(dollars*100)
{
//Body intentionally blank.
}
Money::Money( ) : all_cents(0)
{
//Body intentionally blank.
}
double Money::get_value( ) const
{
return (all_cents * 0.01);
}
//Uses iostream, cctype, cstdlib:
void Money::input(istream& ins)
{
char one_char, decimal_point,
digit1, digit2; //digits for the amount of cents
long dollars;
int cents;
bool negative;//set to true if input is negative.
ins >> one_char;
if (one_char == '-')
{
negative = true;
ins >> one_char; //read '$'
}
else
negative = false;
//if input is legal, then one_char == '$'
ins >> dollars >> decimal_point >> digit1 >> digit2;
if ( one_char != '$' || decimal_point != '.'
|| !isdigit(digit1) || !isdigit(digit2) )
{
cout << "Error illegal form for money input\n";
exit(1);
}
cents = digit_to_int(digit1)*10 + digit_to_int(digit2);
all_cents = dollars*100 + cents;
if (negative)
all_cents = -all_cents;
}
//Uses cstdlib and iostream:
void Money::output(ostream& outs) const
{
long positive_cents, dollars, cents;
positive_cents = labs(all_cents);
dollars = positive_cents/100;
cents = positive_cents%100;
if (all_cents < 0)
outs << "-$" << dollars << '.';
else
outs << "$" << dollars << '.';
if (cents < 10)
outs << '0';
outs << cents;
}
int digit_to_int(char c)
{
return ( int(c) - int('0') );
}
istream& operator >>(istream& ins, Money& the_object)
{
char percent_sign;
ins >> the_object.value;
ins >> percent_sign;
return ins;
}
ostream& operator <<(ostream& outs, const Money& a_percent)
{
outs << a_percent.value << '%';
return outs;
}
|
|
|
10-18-2006, 01:27 PM
|
#2 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Define tax and tax_rate?
What should the program do?
Any caveats, odds and ends?
What's the whole purpose of your project?
It all starts with communication. The reset will be easy then.
__________________
|
|
|
10-19-2006, 09:20 PM
|
#3 (permalink)
|
|
Recruit
Join Date: Sep 2006
Posts: 12
|
Sorry for a late reply
I am trying to get an annual income after the tax has been subtrated,
tax is the acutal amount($$) and tax rate is percent(%)
The program takes user-input data of "income" and "tax rate"
and calculate the amount of tax and subtract that from the income. (I think used term "net" wrong in the program)
So the question is, "is it possible use overloading operators on the user-input data?
I was succesful when I executed the program without any user input data.
If my explanation is not good enough, then...
Here is the exact question from my book:
"Self-Test Exercise 18 asked you to overload the operator >> and the operator << for a class Percent. Complete and test this exericese. Implement the default constructor and the constructor with one int parameter. Overload the + and - operator to add and sutract percents. Also, overload the * operator to allow multiplication of a percent by an integer.
Write a program to test all the member functions and overloaded operators in your class definintion"
Here is the Self-Test Exercise 18 and I have included it in my program:
istream& operator >>(istream& ins, Money& the_object)
{
char percent_sign;
ins >> the_object.value;
ins >> percent_sign;
return ins;
}
ostream& operator <<(ostream& outs, const Money& a_percent)
{
outs << a_percent.value << '%';
return outs;
}
|
|
|
10-20-2006, 05:25 AM
|
#4 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Aaah...
After this you'll be an expert.
__________________
|
|
|
10-20-2006, 01:18 PM
|
#5 (permalink)
|
|
Recruit
Join Date: Sep 2006
Posts: 12
|
Um... so...
I see that the program has values that have been set
ex: A(12.347, 15)
Code:
int main()
{
Complex A(12.347, 15), B(-2, 5.45), Result;
Result=A+B;
But is there any way to incoporate user-input data?
|
|
|
10-21-2006, 01:07 PM
|
#6 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
I still don't understand what you want to achieve for the client user. Can you provide a demo of code that doesn't work but actually should?
And you didn't follow the objectives for this excercise. Instead, you programmed things that weren't asked. That makes it confusing.
The excercise asks you to make a class "percent"!
Not "Money". Even then the book is vague.
A class "Income" would be more suitable.
Code:
class Income
{
private:
double Gross_;
unsigned TaxRate_;
};
But this is too restrictive in it's design. So even better will be:
Code:
class Money
{
private:
double Amount_;
};
class Percent
{
private:
int Percent_;
};
And this is still vague. It all depends on the use case scenario (the way users use your program).
For both the classes (Money and Percent), you'll need to make the same overloaded operators. But that would be overkill for an excercise.
I'll present you the code as required by excercise 18 in my next post.
__________________
Last edited by Valmont; 10-21-2006 at 02:18 PM.
|
|
|
10-21-2006, 02:55 PM
|
#7 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Full compilable code below.
You need to add the assignment operator and += and -= and *= operators. That would complete the excercise more or less. They are important. There's a special skill involved.
Anyway, here's the code as required by excercise 18.
Code:
#include <iostream>
using namespace std;
class Percent
{
public:
Percent(int pcnt = 0) : pcnt_(pcnt) {}
public:
friend Percent operator+(const Percent& lhs, const Percent& rhs);
friend Percent operator-(const Percent& lhs, const Percent& rhs);
friend Percent operator*(const Percent& p, const int factor);
friend Percent operator*(const int factor, const Percent& p);
private:
int pcnt_;
private:
friend ostream& operator<<(ostream& os, const Percent& p);
friend istream& operator>>(istream& is, Percent& p);
};
Percent operator+(const Percent& lhs, const Percent& rhs)
{
return Percent(lhs.pcnt_ + rhs.pcnt_);
}
Percent operator-(const Percent& lhs, const Percent& rhs)
{
return Percent(lhs.pcnt_ - rhs.pcnt_);
}
Percent operator*(const Percent& p, const int factor)
{
return Percent(p.pcnt_ * factor);
}
Percent operator*(const int factor, const Percent& p)
{
return Percent(p.pcnt_ * factor);
}
ostream& operator<<(ostream& os, const Percent& p)
{
os<<p.pcnt_<<"%";
return os;
}
istream& operator>>(istream& is, Percent& p)
{
is>>p.pcnt_;
return is;
}
int main()
{
Percent CertainPercentage(15), OtherPercentage(12);
cout<<"CertainPercentage:\t"<<CertainPercentage<<endl;
cout<<"OtherPercentage:\t"<<OtherPercentage<<endl<<endl;
cout<<"Cert(15) - Other(12):\t"<<CertainPercentage - OtherPercentage<<endl;
cout<<"50 - Cert(15):\t\t"<<50 - CertainPercentage<<endl;
cout<<"Cert(15) - 33:\t\t"<<CertainPercentage - 33<<endl<<endl;
cout<<"Cert(15) + Other(12):\t"<<CertainPercentage + OtherPercentage<<endl;
cout<<"9 + Other(12):\t\t"<<9 + OtherPercentage<<endl;
cout<<"Other(12) + 14:\t\t"<<OtherPercentage + 14<<endl<<endl;
cout<<"8 * Other(12):\t\t"<<8 * OtherPercentage<<endl;
cout<<"Other(12) * 8:\t\t"<<OtherPercentage * 8<<endl<<endl;
cout<<"Please enter a Percentage (testing the extractor):";
cin>>CertainPercentage;
cout<<"Tested with stream extractor: "<<CertainPercentage<<endl;
return 0;
}
The operator* is ambigous! That means, it could easely mislead client coders.
That operator should be preserved to multiply percentage by percentage.
To multiply a percentage by a factor, add a member called Percent Scalar(const int factor).
The book was wrong by asking you in the first place.
Anyway, add this as a public member of Percent:
Code:
double const ToDouble() const { return (double)pcnt_; }
So you can code like this:
Code:
Percent Tax(22);
double theTax = Tax.ToDouble();
double GrossIncome = 1501.00;
double TaxRate = GrossIncome * (theTax/100.00);
double NetIncome = GrossIncome - TaxRate;
cout<<"Net Income = "<<NetIncome<<endl;
__________________
Last edited by Valmont; 10-21-2006 at 04:03 PM.
|
|
|
10-22-2006, 04:35 PM
|
#8 (permalink)
|
|
Recruit
Join Date: Sep 2006
Posts: 12
|
Satisfied
Okay... Thanks a lot
Maybe I was trying to do something something too weird and confusing. If I had done your way, maybe I could have finished my assignment much easier and faster.
This is the second time you helping me out. I am very grateful (I bet you help a bunch of other novice programmers too) 
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 11:14 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|