Need Help With Creating An Object

Wanna understand E=mc2, English grammer, or maybe just build a computer? We can help!

Moderator: Moderators

LoSTimberWolf
Inmate
Posts: 128
Joined: Sun Jan 27, 2002 4:17 pm

Need Help With Creating An Object

Post by LoSTimberWolf »

I am creating an object for my assignment in C++, and it is layed out like all the examples in the book. I know the finaly equation in it is flawed, but I can't test it until the entire object starts working. The main driver is given to us, no tampering with it.<br><br>So here is my problem, I have four veriables that I initialize in the object but when I compile the program they are not. As I have said, my object has everything in it that the examples have, so I can't see why they are not being initialized. Any ideas? <p></p><i></i>
XMEN Ashaman DTM
Inmate
Posts: 2369
Joined: Mon Oct 02, 2000 12:09 am
Location: Silverdale, WA

Re: Need Help With Creating An Object

Post by XMEN Ashaman DTM »

<!--EZCODE QUOTE START--><blockquote><strong><em>Quote:</em></strong><hr>I have four veriables that I initialize in the object but when I compile the program they are not<hr></blockquote><!--EZCODE QUOTE END--><br><br>I'm confused...<br><br> <p><br><img src=http://www.xmenclan.org/avatars/ashaman2.jpg width=150 align=left><br><left><a href=http://www.dragontalonmercs.com>Dragon Talon Mercenary</a><br><a href=http://www.xmenclan.org>Member of the XMEN</a><br>Have a Nice Day!<br><br>"Politics, war, martial arts;<br>these are tools to bend the <br>universe to your will."</left><br><br><br><p><i>Edited by: <a href=http://pub3.ezboard.com/uxmengambit.sho ... ge=EN>XMEN Gambit</a> <img src=http://www.xmenclan.org/images/x.gif> at: 7/14/02 8:16:26 pm</i></p></p><i></i>
User avatar
XMEN Gambit
Site Admin
Posts: 4122
Joined: Thu Nov 18, 1999 12:00 am

Re: Need Help With Creating An Object

Post by XMEN Gambit »

WHERE are the variables being initialized?<br><br>If the variables are members of an object, then to get them initialized properly you'll need to 1) initialize them in the constructor of the object and 2) actually create an instance of the object, and reference those variables inside the object to test. <br><br>Note that it's usually a bad idea to reference an objects variables directly from outside; you should use GET and SET methods to do so.<br><br>*edit*<br>if you want to send me a copy of your code I can discuss it more intelligently. gambit@xmenclan.org<br> <p><!--EZCODE IMAGE START--><img src="http://www.xmenclan.org/xmengambit.gif"/><!--EZCODE IMAGE END--><br>XMEN member<br>Card-carrying DTM<br>OKL Fish-napper<br><br>Though a program be but three lines long, someday it will have to be maintained.<br><!--EZCODE ITALIC START--><em> The Tao of Programming</em><!--EZCODE ITALIC END--></p><i>Edited by: <A HREF=http://pub141.ezboard.com/bxmenclan.sho ... ambit>XMEN Gambit</A>&nbsp; <IMG SRC="http://www.xmenclan.org/images/x.gif" BORDER=0> at: 1/28/03 5:18:43 pm<br></i>
Image
LoSTimberWolf
Inmate
Posts: 128
Joined: Sun Jan 27, 2002 4:17 pm

Re: Need Help Creating An Object

Post by LoSTimberWolf »

Email sent, with two attachments. The email is from my UTD account. I am now on my way home so if you reply I will not be able to reach it for about an hour or so. <p></p><i></i>
User avatar
XMEN Gambit
Site Admin
Posts: 4122
Joined: Thu Nov 18, 1999 12:00 am

Re: Need Help Creating An Object

Post by XMEN Gambit »

reply sent. <p></p><i></i>
Image
LoSTimberWolf
Inmate
Posts: 128
Joined: Sun Jan 27, 2002 4:17 pm

Re: Need Help With Creating An Object

Post by LoSTimberWolf »

error C2628: 'Mortgage' followed by 'void' is illegal (did you forget a ';'?)<br>error C2065: '_Loan' : undeclared identifier<br>error C2065: '_Rate' : undeclared identifier<br>error C2065: '_Years' : undeclared identifier<br>Error executing cl.exe.<br><br>This is what I am getting trying to compile my program. I have my variables declared in my object, and there is no problem with 2 of them. The information Gambit has given me corrected what ever was the problem with those two, but the others still refuse to exist. Any more ideas? <p>-LoS-TimberWolf{FF}<br>Vehicle Specilist</p><i></i>
Scorch
Inmate
Posts: 215
Joined: Thu Mar 16, 2000 12:02 am

Re: Need Help With Creating An Object

Post by Scorch »

(This message was left blank) <p></p><i>Edited by: <A HREF=http://pub141.ezboard.com/bxmenclan.sho ... >Scorch</A> at: 1/29/03 1:25:37 am<br></i>
LoS Malthus
Posts: 60
Joined: Mon Apr 15, 2002 2:51 pm

Re: Need Help With Creating An Object

Post by LoS Malthus »

How long is the code? Can you post it?<br><br> <p>---------<br>...the only real difference between the locals and visitors seems to be length of the vacation...</p><i></i>
User avatar
Spinning Hat
Inmate
Posts: 2564
Joined: Wed Jun 07, 2000 10:06 am
Location: Minneapolis, MN
Contact:

Re: Need Help With Creating An Object

Post by Spinning Hat »

although I d understand what is going on fundamentally, I hae no experience with C++, or C, or anything else but HTML and Javascript, It's probles like those that remind me everyday why I enjoy networking s much more. <!--EZCODE EMOTICON START ;) --><img src=http://www.ezboard.com/images/emoticons/wink.gif ALT=";)"><!--EZCODE EMOTICON END--> <p><!--EZCODE CENTER START--><div style="text-align:center"><!--EZCODE FONT START--><span style="color:red;font-family:arial;font-size:xx-large;"><!--EZCODE BOLD START--><strong>I HATE EZBOARD!!!</strong><!--EZCODE BOLD END--></span><!--EZCODE FONT END--></div><!--EZCODE CENTER END--></p><i></i>
Image
"Never, Never, Never quit." - Winston Churchill
"Men don't like to cuddle. They only cuddle if it leads to.. You know.. Lower cuddling." - Ray Romano
"Tell your wife that she looks pretty, even if she looks like a dump truck." - Ricky, age 10
User avatar
XMEN Gambit
Site Admin
Posts: 4122
Joined: Thu Nov 18, 1999 12:00 am

Re: Need Help With Creating An Object

Post by XMEN Gambit »

Ok, TW, I was able to get it to compile, but it hangs after the first line is printed.<br><br>First off, you really need to split that Mortgage class off into its own header and cpp file. More on this at the end.<br><br>To get it to compile, I first made those set methods members of the Mortgage class by placing "Mortgage::" in front of them. I presume you did the same, because when I tried to compile, I got the same errors you report.<br><br>Your problem now is that the class Mortgage is closed off (by end bracket "}") before the method definitions. Because you've got all that stuff in the header file, you'll need to 1) comment out the method declarations, such as "void setLoan(int);", then 2) move the class-closing bracket and #endif statement to the bottom of the .h file, and 3) place a semicolon after the bracket and before the endif.<br><br>That should get you to compile. You've got an endless loop in getPayBack that you'll need to take care of, though; that's why it hangs.<br><br>Now then, because you've completely defined the class in the header file, instead of merely declaring it, the entire class gets compiled into every file where you include the .h - duplicating it each time. Very wasteful of memory, though of course with something this small that's only used once it doesn't really matter. In addition, if you ever use any statics, which exist once for any given class no matter how many times you instatiate it, then these individual copies of the classes appear different to the compiler because they exist in different places. So your static variables would be useless, and only as good as normal instance variables. <p><!--EZCODE IMAGE START--><img src="http://www.xmenclan.org/xmengambit.gif"/><!--EZCODE IMAGE END--><br>XMEN member<br>Card-carrying DTM<br>OKL Fish-napper<br><br>Though a program be but three lines long, someday it will have to be maintained.<br><!--EZCODE ITALIC START--><em> The Tao of Programming</em><!--EZCODE ITALIC END--></p><i></i>
Image
Jester DTM
Inmate
Posts: 367
Joined: Fri Jun 08, 2001 2:34 pm

Re: Need Help With Creating An Object

Post by Jester DTM »

Exactly what I was going to say, Gamibt just beat me to it. <p><table border="0" cellspacing="0" cellpadding="0"><tr><td><a href="http://www.thzclan.com/cam" target="_blank"><img src="http://www.thzclan.com/cam/jester/thb_ccam.jpg" border="0"></a></td><td><img src="http://www.thzclan.com/kirabby/Eebay/spam.gif" align="middle"></td><td><b><a href="mailto:jester@thzclan.com">996</a></b><br><b><a href=http://www.thzclan.com>Cerberus of THZ*</a></b><br><b><a href=http://www.xmenclan.org>XMEN</a></b><br><b><a href=http://www.dragontalonmercs.com>Dragon Talon Merc</a> </b><br><b><a href=http://www.thzclan.com/hosted/planetside>Allied Tribal Forces</a></b></td>&nbsp; <td> <img src="http://www.thzclan.com/hosted/jester/si ... </p><i></i>
LoSTimberWolf
Inmate
Posts: 128
Joined: Sun Jan 27, 2002 4:17 pm

Re: Need Help With Creating An Object

Post by LoSTimberWolf »

My teacher wants us to have everything in the header file. He is big on making objects in JAVA, and it looks like he will make us build off the object later to create more.<br><br>So far I got it compiling, but by leaving the }; and #endif; where they were.<br><br>The final equation, even though it is flawed, was intended to find out how much had to be paid to payback the loan at a certain rate. The idea was to take the equation from monthly and put it into a loop so it would continue until the loan equaled zero. Now that the program will run, I can now work on that until it looks like the example output given to me. <p></p><i>Edited by: <A HREF=http://pub141.ezboard.com/bxmenclan.sho ... berWolf</A> at: 1/29/03 1:32:18 pm<br></i>
User avatar
XMEN Gambit
Site Admin
Posts: 4122
Joined: Thu Nov 18, 1999 12:00 am

Re: Need Help With Creating An Object

Post by XMEN Gambit »

Hmm, sounds odd. I'd like to see your revised source, if I may.<br><br>I spent more than the last two years writing Java, and the fact your teacher likes it is fine - but what does that have to do with putting the entire class definition in the header file? You'll never see it done that way, and for good reason. I'm not ranting at you, but I think your teacher should instruct you in ways that won't confuse you now OR later on. <!--EZCODE EMOTICON START :) --><img src=http://www.ezboard.com/images/emoticons/smile.gif ALT=":)"><!--EZCODE EMOTICON END--> <br><br>It sounds like your program is designed to produce is an amortization schedule. Very common exercise. <p><!--EZCODE IMAGE START--><img src="http://www.xmenclan.org/xmengambit.gif"/><!--EZCODE IMAGE END--><br>XMEN member<br>Card-carrying DTM<br>OKL Fish-napper<br><br>Though a program be but three lines long, someday it will have to be maintained.<br><!--EZCODE ITALIC START--><em> The Tao of Programming</em><!--EZCODE ITALIC END--></p><i></i>
Image
LoSTimberWolf
Inmate
Posts: 128
Joined: Sun Jan 27, 2002 4:17 pm

Re: Need Help With Creating An Object

Post by LoSTimberWolf »

#include <iostream><br>#include <math.h><br><br>#ifndef MORTGAGE_H<br>#define MORTGAGE_H<br><br>//Object created named Mortgage<br>class Mortgage<br>{<br>private:<br>&nbsp &nbsp &nbsp &nbsp int _Loan;<br>&nbsp &nbsp &nbsp &nbsp double _Rate;<br>&nbsp &nbsp &nbsp &nbsp int _Years;<br>&nbsp &nbsp &nbsp &nbsp double _Monthly;<br>&nbsp &nbsp &nbsp &nbsp double _PayBack;<br>public:<br>&nbsp &nbsp &nbsp &nbsp void setLoan(int);<br>&nbsp &nbsp &nbsp &nbsp void setRate(double);<br>&nbsp &nbsp &nbsp &nbsp void setYears(int);<br>&nbsp &nbsp &nbsp &nbsp double getMonthly();<br>&nbsp &nbsp &nbsp &nbsp double getPayBack();<br><br>};<br><br>#endif;<br><br>void Mortgage::setLoan(int Loan)<br>{<br>&nbsp &nbsp &nbsp &nbsp _Loan = 0;<br>&nbsp &nbsp &nbsp &nbsp //Input validation, must be between 0 and 1000000<br>&nbsp &nbsp &nbsp &nbsp if (Loan < 0 || Loan > 1000000)<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp {//If not between 0 and 1000000, set to 1000000<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp cout << "Loan must be between 0 and $1,000,000 dollars" << endl;<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp _Loan = 1000000;<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp }<br>&nbsp &nbsp &nbsp &nbsp else<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp //Else set to equal input<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp _Loan = Loan;<br>}<br><br><br>void Mortgage::setRate(double Rate)<br>{<br>&nbsp &nbsp &nbsp &nbsp _Rate = 0;<br>&nbsp &nbsp &nbsp &nbsp //Input validation, must be between 0.00 and 0.12<br>&nbsp &nbsp &nbsp &nbsp if (Rate < 0 || Rate > 0.12)<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp {//If not between 0.00 and 0.12, set to 0.12<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp cout << "Rate must be between 0 and 12 percent" << endl;<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp _Rate = 0.12;<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp }<br>&nbsp &nbsp &nbsp &nbsp else<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp //Else set to equal input<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp _Rate = Rate;<br>}<br><br><br>void Mortgage::setYears(int Years)<br>{<br>&nbsp &nbsp &nbsp &nbsp _Years = 0;<br>&nbsp &nbsp &nbsp &nbsp //Input validation, must be between 5 and 50<br>&nbsp &nbsp &nbsp &nbsp if (Years < 5 || Years > 50)<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp {//If not between 5 and 50, set to 50<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp cout << "Years must be between 5 and 50" << endl;<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp _Years = 50;<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp }<br>&nbsp &nbsp &nbsp &nbsp else<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp //Else set to equal input<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp _Years = Years;<br>}<br><br><br>double Mortgage::getMonthly()<br>{<br>&nbsp &nbsp &nbsp &nbsp _Monthly = 0;<br>&nbsp &nbsp &nbsp &nbsp //Calculates monthly cost<br>&nbsp &nbsp &nbsp &nbsp _Monthly = (_Loan * (_Rate / 12) * _Years) / (_Years - 1);<br>&nbsp &nbsp &nbsp &nbsp return _Monthly;<br>}<br><br><br>double Mortgage::getPayBack()<br>{<br>&nbsp &nbsp &nbsp &nbsp _PayBack = 0;<br>&nbsp &nbsp &nbsp &nbsp //Calculates complete amount payed back by the end<br>&nbsp &nbsp &nbsp &nbsp while (_Loan > 0)<br>&nbsp &nbsp &nbsp &nbsp {<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp _PayBack += getMonthly();<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp _Loan -= getMonthly();<br>&nbsp &nbsp &nbsp &nbsp }<br>&nbsp &nbsp &nbsp &nbsp return _PayBack;<br>}<br> <p>-LoS-TimberWolf{FF}<br>Vehicle Specilist</p><i></i>
User avatar
XMEN Gambit
Site Admin
Posts: 4122
Joined: Thu Nov 18, 1999 12:00 am

Re: Need Help With Creating An Object

Post by XMEN Gambit »

Hmm, thought I tried that. And it ought to work this way. <br><br>But I bet I added the semicolon last, after I'd moved the closing brackets. That would make sense. And this way is more "proper" than the other with no declarations. <p></p><i></i>
Image
Post Reply