![]() |
|
#1
|
|||
|
|||
Functions and Classes - Where did I go wrong?I am doing an online very beginner programming class. I know I'm on the right track, but the book I have is the wrong one and it's no help. This is the assignment:
Create a class Rectangle. The class has private attributes length and width, each of which defaults to 1. It has member functions that calculate the perimeter and the area of the rectangle. It has set and get functions for both length and width. The set functions (i.e. setLength() and setWidth()) should verify that length and width are larger than 0.0 and less than 20.0. If the parameter does not satisfy, use default value = 1.0 The get functions (i.e. getLength() and getWidth() ) should return the value of the attributes. The constructor should use the set functions to initialize the attributes. The following is main function you should use. The main idea is that, given three rectangles a, b and c, output their lengths, widths, perimeters and areas. Include header files when necessary. I thought I knew how to do it, but for some reason I'm stumbling on... something. I just don't know what's going wrong, and I know it must be really basic. I'm pretty sure I'm missing something, but I'm not sure what to look up in a tutorial to fix what's wrong, you know? CPP / C++ / C Code:
When I compile, I get three errors: "error C2535: '__thiscall rectangle::rectangle(void)' : member function already defined or declared" (that one I get twice) "fatal error C1004: unexpected end of file found Error executing cl.exe." This is of course due tonight (asking online was my last resort after trying to make it work on my own) and if someone can just tell me where I'm going wrong and how I should be tackling it instead I'd appriciate it. Last edited by LuciWiz : 10-Apr-2005 at 17:18.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|||
|
#2
|
|||
|
|||
|
well , first im a newbie myself ... and ive only just started oop, but i do see some major erors in youre code;
first u should know that c++ is case sensitive, if u have a class called rectangle, then when ur creating objects u should use rectangle as the variable type not Rectangle...check ur spelling Return!(return) lenght!(length).... 2. i think u get unexpected end of .... because uve missed a brace here: CPP / C++ / C Code:
4. I think what u mean here is: CPP / C++ / C Code:
CPP / C++ / C Code:
anyway Hope i helped... (even though im not so sure i did) |
|
#3
|
|||
|
|||
|
Ok, this is where I'm at now:
CPP / C++ / C Code:
ERRORS Code:
Last edited by LuciWiz : 10-Apr-2005 at 17:19.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#4
|
||||
|
||||
|
You have lots of misplaced semicolons in your code:
CPP / C++ / C Code:
And more. Please remove them (what were you trying to do?) Also, you defined two of your member functions to return double, but then you changed them to int. The compiler won't like it CPP / C++ / C Code:
It should be double AND const. You need to specify this too. CPP / C++ / C Code:
But now you'll have a problem with the fact that the compiler can't find an implementation for your overloaded constructor. You need to build one: CPP / C++ / C Code:
Or you could do this too in the definition of the class. However, I find this approach cumbersome, since I think this led you to forgetting about it in the first place. Plus it makes your class definition less readable. Other people feel different about this, so you can go with whatever you choose... Best regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#5
|
|||
|
|||
|
Ok, I've gotten the coding itself compile-able, but when I run it I get insanely wrong answers that I'm having trouble tracing:
"a: length = -858993460; width = -858993460area = 1 b: length = 5; width = 4; perimeter = 858993456; area = 687194768 c: length = 888; width = 67; perimeter =858993456; area = 687194768 Press any key to continue" This is the calculating part: CPP / C++ / C Code:
Header CPP / C++ / C Code:
And the main's the same as the given in the original code. Last edited by LuciWiz : 11-Apr-2005 at 03:11.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#6
|
||||
|
||||
|
It seems to me that setWidth() should actually set the width. Otherwise what is it for? In your code:
CPP / C++ / C Code:
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
Recent GIDBlog
Toyota - 2009 May Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| friend functions error | CaptnB | C++ Forum | 2 | 12-Jun-2006 14:39 |
| Fairly simple classes help please | sammacs | C++ Forum | 0 | 30-Nov-2004 10:58 |
| help with classes | bucho | MS Visual C++ / MFC Forum | 3 | 20-Oct-2004 07:16 |
| Help with c++ program with classes | jimmy55 | C++ Forum | 12 | 18-Mar-2004 11:32 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The