| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Functions and Classes - Where did I go wrong?
I am doing an online beginner (intro to C++) 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 O_o 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 spending alot of time trying to make it work on my own with little luck) and if someone can just tell me where I'm going wrong and how to tackle it I'd appriciate it. |
|
#2
|
|||
|
|||
|
Code:
#include <iostream>
using namespace std;
class rectangle {
private:
int width, height;
public:
rectangle (const int x, const int y);
int area (void) {return (width*height)};
int perimeter (void) {return (width*2 + height*2);};
void setWidth(const int x)
{
if (x > 0 && x < 20) {
width = x};
} else {
width = 1;
}
}
void setHeight(const int y) {
if (y > 0 && y < 20)
{
height = y;
} else {
height = 1;
}
}
int getWidth(void) {return width};
int getHeight(void) {retutn height};
};
rectangle::rectangle (const int x = 1, const int y = 1)
{
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;
}
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Functions and Classes - Where did I go wrong? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|