![]() |
| | #1 |
| Registered User Join Date: Apr 2005
Posts: 2
| Functions and Classes - What did I do wrong? "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 so I can fix what's wrong, you know? Code: //this is my coding
#include <iostream>
using namespace std;
class rectangle {
private:
int width, height;
public:
rectangle ();
rectangle (int,int);
int area (void) {return (x*y);
int perimeter (void) {return (x*2 + y*2);}
};
rectangle::rectangle () //a tutorial online said that's how you set defaults
{
x = 1;
y = 1;
}
rectangle::rectangle (int x, int y) //derived from notes
{
width = x;
height = y;
}
//this is what was given
int main()
{
Rectangle a, b(4.0,5.0), c(67.0, 888.0);
cout<< setiosflags(ios::fixed | ios::showpoint);
cout<<setprecision(1);
cout<<"a: length = " << a.getLength() << "; width = " << a.getWidth()
<< "area = " << a.area() <<'\n';
cout<< "b: length = " << b.getLength() << "; width = " << b.getWidth()
<< "; perimeter = " << b.perimeter()
<< "; area = " << b.area() << '\n';
cout << "c: length = " << c.getLenght() << "; width = " << c.getWidth()
<< "; perimeter =" << c.perimeter()
<< "; area = " << c.area() << endl;
return 0;
}
//my coding again
int setWidth() //because it is based on a, b, and c, it is blank, right?
{
if ((x < 0.0) || (x > 20.0))
{
x = 1.0;
}
Return x;
}
//have to use X and Y because Width and Height are private, right?
int setHeight()
{
if ((y < 0.0) || (y > 20.0))
{
y = 1.0;
}
return y;
}
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 doing it instead I'd appriciate it. |
| redmage is offline | |
| | #2 |
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,870
| Your setWidth and setLength functions need to be member functions of the class. The constructor also needs to use these functions to set its values (according to the specifications you provided). So, as a start... Code: class rectangle {
private:
int width, length; // length not height?
public:
rectangle ();
rectangle (int,int);
int area (void) const {return (width*length); }
int perimeter (void) const {return (length*2 + width*2);}
void setWidth(int);
void setLength(int);
int getWidth() const;
int getLength() const;
};
// Add your code appropriate for the setWidth, setLength, getWidth and getLength functions.
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 |
| hk_mp5kpdw is offline | |
| | #3 |
| Registered User Join Date: Apr 2005
Posts: 2
| Ok, this is where I'm at now: Code: //this is my coding
#include <iostream>
#include <iomanip>
using namespace std;
class rectangle {
private:
double Width, Lenght;
public:
rectangle () {Width = 1.0, Lenght = 1.0;};
rectangle (double,double);
double area (void) const {return (Width*Lenght);}
double perimeter (void) const {return (Lenght*2 + Width*2);}
void setWidth(double x);
void setLenght(double y);
double getWidth() const;
double getLenght() const;
};
void rectangle::setWidth(double x;)
{
if ((x < 0.0;) || (x > 20.0;))
{
Width = 1.0;
}
Width = x;
}
void rectangle::setLenght(double y;)
{
if ((y < 0.0;) || (y > 20.0;))
{
Lenght = 1.0;
}
Lenght = y;
}
int rectangle::getWidth()
{
return Width;
}
int rectangle::getLenght()
{
return Lenght;
}
//this is what was given
int main()
{
rectangle a, b(4.0,5.0), c(67.0, 888.0);
cout<< setiosflags(ios::fixed | ios::showpoint);
cout<<setprecision(1);
cout<<"a: length = " << a.getLenght() << "; width = " << a.getWidth()
<< "area = " << a.area() <<'\n';
cout<< "b: length = " << b.getLenght() << "; width = " << b.getWidth()
<< "; perimeter = " << b.perimeter()
<< "; area = " << b.area() << '\n';
cout << "c: length = " << c.getLenght() << "; width = " << c.getWidth()
<< "; perimeter =" << c.perimeter()
<< "; area = " << c.area() << endl;
return 0;
}
ERRORS C:\Documents and Settings\Chris\Cpp1.cpp(24) : error C2143: syntax error : missing ')' before ';' C:\Documents and Settings\Chris\Cpp1.cpp(24) : error C2059: syntax error : ')' C:\Documents and Settings\Chris\Cpp1.cpp(25) : error C2447: missing function header (old-style formal list?) C:\Documents and Settings\Chris\Cpp1.cpp(35) : error C2143: syntax error : missing ')' before ';' C:\Documents and Settings\Chris\Cpp1.cpp(35) : error C2059: syntax error : ')' C:\Documents and Settings\Chris\Cpp1.cpp(36) : error C2447: missing function header (old-style formal list?) C:\Documents and Settings\Chris\Cpp1.cpp(46) : error C2511: 'getWidth' : overloaded member function 'int (void)' not found in 'rectangle' C:\Documents and Settings\Chris\Cpp1.cpp(8) : see declaration of 'rectangle' C:\Documents and Settings\Chris\Cpp1.cpp(57) : error C2511: 'getLenght' : overloaded member function 'int (void)' not found in 'rectangle' C:\Documents and Settings\Chris\Cpp1.cpp(8) : see declaration of 'rectangle' |
| redmage is offline | |
| | #4 |
| Registered User Join Date: Apr 2003
Posts: 2,662
| You don't put semicolons in an if-statement. Code: if ((x < 0.0;) || (x > 20.0;) A function definition has to match the function declaration: Code: double getWidth() const;
int rectangle::getWidth() <---
{
return Width;
}
Last edited by 7stud; 04-10-2005 at 06:06 PM. |
| 7stud is offline | |
| | #5 |
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,870
| Code: void rectangle::setWidth(double x;)
{
...
}
void rectangle::setLength(double y;)
{
...
}
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 |
| hk_mp5kpdw is offline | |
| | #6 |
| Registered User Join Date: Mar 2002
Posts: 1,595
| Look closer at your logic in the setWidth and setHeight functions, too.
__________________ You're only born perfect. |
| elad is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Classes being able to use other classes functions | rainmanddw | C++ Programming | 6 | 01-29-2006 11:19 AM |
| im extreamly new help | rigo305 | C++ Programming | 27 | 04-23-2004 11:22 PM |
| Prime Number Generator... Help !?!! | Halo | C++ Programming | 9 | 10-20-2003 07:26 PM |
| include question | Wanted420 | C++ Programming | 8 | 10-17-2003 03:49 AM |
| help with classes and member functions | syner | C++ Programming | 4 | 07-19-2002 08:45 PM |