[ale] [OT] C++ Question

Jim Popovitch jimpop at rocketship.com
Mon Dec 2 12:05:50 EST 2002


What does the code for showmoney() look like?

-Jim P.

> -----Original Message-----
> From: Jonathan Glass
> Sent: Monday, December 02, 2002 12:01 PM
> 
> 
> money3.showmoney(); returns garbage.  It's like I am not either (a)
> initializing the variable correctly (this possibility has been
> eliminated via liberal cout's), or (b) not returning the correct
> variable (maybe I need a pointer?).  I have another statement (AddMoney)
> which works fine...just this operator overload doesn't work.
> 
> Here's my test code:
> CMoney CMoney::operator+ ( CMoney& t)
> {
>   // money3 = money1 + money2
>   CMoney temp;
>   temp.showmoney();
>   this->showmoney();
>   t.showmoney();
>   temp.m_cents= this->m_cents + t.m_cents;
>   temp.showmoney();
>   this->showmoney();
>   t.showmoney();
>   return (temp);
> }
> 
> int main(int argc, char* argv[])
> {
>   CMoney money1;
>   money1.AddMoney(0,101);
>   CMoney money2;
>   money2.AddMoney(0,100);
>   CMoney money3;
>   money3.ShowMoney();
>   money3=money1+money2;
>   cout << "MONEY1: ";
>   money1.ShowMoney();
>   cout << endl << "MONEY2: ";
>   money2.ShowMoney();
>   cout << endl;
>   cout << "MONEY3: ";
>   money3.ShowMoney();
>   return 0;
> }
> 
> And I get the following for output
> 0 dollars	0 cents
> 0 dollars	0 cents
> 1 dollars	1 cents
> 1 dollars	0 cents
> 0 dollars	1 cents
> 1 dollars	1 cents
> 1 dollars	0 cents
> MONEY1:0 dollars	0 cents
> MONEY2:0 dollars	0 cents
> MONEY3:-8589933 dollars	-858993300
> 
> ARGH!!  
> 
> Thanks again
> 
> Jonathan
> 
> On Mon, 2002-12-02 at 11:46, Jim Popovitch wrote:
> > Hi Jonathan,
> > 
> > This should help... if not what specific error are you getting?
> > 
> > 
> http://hpcf.nersc.gov/vendor_docs/ibm/vac/language/ref/overloading
> .cplr318.h
> > tm
> > 
> > 
> > -Jim P.
> > 
> > > -----Original Message-----
> > > From: ale-admin at ale.org [mailto:ale-admin at ale.org]On Behalf 
> Of Jonathan
> > > Glass
> > > Sent: Monday, December 02, 2002 11:39 AM
> > > To: ale at ale.org
> > > Subject: [ale] [OT] C++ Question
> > >
> > >
> > > I'm working on my final project, and am having the hardest 
> time with the
> > > overloading the "+" operator for a custom class.  Any 
> thoughts?  I'll be
> > > happy to attach the full source file on request.
> > >
> > > TIA
> > >
> > > -----
> > >
> > > CMoney CMoney::operator+ ( CMoney& t)
> > > {
> > >         // money3 = money1 + money2
> > >         CMoney temp;
> > >
> > >         temp.m_cents= this->m_cents + t.m_cents;
> > >         return temp;
> > > }
> > >
> > > int main(int argc, char* argv[])
> > > {
> > >         CMoney money1;
> > >         money1.AddMoney(0,101);
> > >         CMoney money2;
> > >         money2.AddMoney(0,100);
> > >         CMoney money3;
> > >         money3.ShowMoney();
> > >         money3=money1+money2;
> > >         cout << "MONEY1: ";
> > >         money1.ShowMoney();
> > >         cout << endl << "MONEY2: ";
> > >         money2.ShowMoney();
> > >         cout << endl;
> > >         cout << "MONEY3: ";
> > >         money3.ShowMoney();
> > >         return 0;
> > > }
> > >
> > > -----
> > > Jonathan Glass
> > > Systems Support Specialist II
> > > Institute for Bioengineering & Bioscience
> > > Georgia Institute of Technology
> > > 404.385.0127
> > >
> > > _______________________________________________
> > > Ale mailing list
> > > Ale at ale.org
> > > http://www.ale.org/mailman/listinfo/ale
> > >
> > 
> > 
> > _______________________________________________
> > Ale mailing list
> > Ale at ale.org
> > http://www.ale.org/mailman/listinfo/ale
> -- 
> Jonathan Glass
> Systems Support Specialist II
> Institute for Bioengineering & Bioscience
> Georgia Institute of Technology
> 404.385.0127
> 
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale
> 

_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale






More information about the Ale mailing list