Need Help With Creating An Object
Moderator: Moderators
-
- Inmate
- Posts: 128
- Joined: Sun Jan 27, 2002 4:17 pm
Need Help With Creating An Object
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>
-
- Inmate
- Posts: 2369
- Joined: Mon Oct 02, 2000 12:09 am
- Location: Silverdale, WA
Re: Need Help With Creating An Object
<!--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>
- XMEN Gambit
- Site Admin
- Posts: 4122
- Joined: Thu Nov 18, 1999 12:00 am
Re: Need Help With Creating An Object
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> <IMG SRC="http://www.xmenclan.org/images/x.gif" BORDER=0> at: 1/28/03 5:18:43 pm<br></i>
-
- Inmate
- Posts: 128
- Joined: Sun Jan 27, 2002 4:17 pm
Re: Need Help Creating An Object
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>
- XMEN Gambit
- Site Admin
- Posts: 4122
- Joined: Thu Nov 18, 1999 12:00 am
-
- Inmate
- Posts: 128
- Joined: Sun Jan 27, 2002 4:17 pm
Re: Need Help With Creating An Object
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>
Re: Need Help With Creating An Object
(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>
-
- Posts: 60
- Joined: Mon Apr 15, 2002 2:51 pm
Re: Need Help With Creating An Object
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>
- Spinning Hat
- Inmate
- Posts: 2564
- Joined: Wed Jun 07, 2000 10:06 am
- Location: Minneapolis, MN
- Contact:
Re: Need Help With Creating An Object
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>
- XMEN Gambit
- Site Admin
- Posts: 4122
- Joined: Thu Nov 18, 1999 12:00 am
Re: Need Help With Creating An Object
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>
-
- Inmate
- Posts: 367
- Joined: Fri Jun 08, 2001 2:34 pm
Re: Need Help With Creating An Object
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> <td> <img src="http://www.thzclan.com/hosted/jester/si ... </p><i></i>
-
- Inmate
- Posts: 128
- Joined: Sun Jan 27, 2002 4:17 pm
Re: Need Help With Creating An Object
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>
- XMEN Gambit
- Site Admin
- Posts: 4122
- Joined: Thu Nov 18, 1999 12:00 am
Re: Need Help With Creating An Object
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>
-
- Inmate
- Posts: 128
- Joined: Sun Jan 27, 2002 4:17 pm
Re: Need Help With Creating An Object
#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>        int _Loan;<br>        double _Rate;<br>        int _Years;<br>        double _Monthly;<br>        double _PayBack;<br>public:<br>        void setLoan(int);<br>        void setRate(double);<br>        void setYears(int);<br>        double getMonthly();<br>        double getPayBack();<br><br>};<br><br>#endif;<br><br>void Mortgage::setLoan(int Loan)<br>{<br>        _Loan = 0;<br>        //Input validation, must be between 0 and 1000000<br>        if (Loan < 0 || Loan > 1000000)<br>                {//If not between 0 and 1000000, set to 1000000<br>                cout << "Loan must be between 0 and $1,000,000 dollars" << endl;<br>                _Loan = 1000000;<br>                }<br>        else<br>                //Else set to equal input<br>                _Loan = Loan;<br>}<br><br><br>void Mortgage::setRate(double Rate)<br>{<br>        _Rate = 0;<br>        //Input validation, must be between 0.00 and 0.12<br>        if (Rate < 0 || Rate > 0.12)<br>                {//If not between 0.00 and 0.12, set to 0.12<br>                cout << "Rate must be between 0 and 12 percent" << endl;<br>                _Rate = 0.12;<br>                }<br>        else<br>                //Else set to equal input<br>                _Rate = Rate;<br>}<br><br><br>void Mortgage::setYears(int Years)<br>{<br>        _Years = 0;<br>        //Input validation, must be between 5 and 50<br>        if (Years < 5 || Years > 50)<br>                {//If not between 5 and 50, set to 50<br>                cout << "Years must be between 5 and 50" << endl;<br>                _Years = 50;<br>                }<br>        else<br>                //Else set to equal input<br>                _Years = Years;<br>}<br><br><br>double Mortgage::getMonthly()<br>{<br>        _Monthly = 0;<br>        //Calculates monthly cost<br>        _Monthly = (_Loan * (_Rate / 12) * _Years) / (_Years - 1);<br>        return _Monthly;<br>}<br><br><br>double Mortgage::getPayBack()<br>{<br>        _PayBack = 0;<br>        //Calculates complete amount payed back by the end<br>        while (_Loan > 0)<br>        {<br>                _PayBack += getMonthly();<br>                _Loan -= getMonthly();<br>        }<br>        return _PayBack;<br>}<br> <p>-LoS-TimberWolf{FF}<br>Vehicle Specilist</p><i></i>
- XMEN Gambit
- Site Admin
- Posts: 4122
- Joined: Thu Nov 18, 1999 12:00 am
Re: Need Help With Creating An Object
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>