GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Apr-2005, 12:55
redmage redmage is offline
New Member
 
Join Date: Apr 2005
Posts: 3
redmage is on a distinguished road

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:
//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 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  
Old 10-Apr-2005, 14:56
kai85 kai85 is offline
...is NOT a boy!
 
Join Date: Jan 2005
Posts: 58
kai85 is on a distinguished road
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:
int area (void) {return (x*y);
3.i suggest u declare ur constructers inside ur class decleration ...its better if made they're into inline functions... u won't get the errors u mentioned!
4. I think what u mean here is:
CPP / C++ / C Code:
rectangle::rectangle () //a tutorial online said that's how you set defaults
{
  x = 1;
  y = 1;
}
CPP / C++ / C Code:
{
  height = 1;
  width = 1;
}

anyway Hope i helped... (even though im not so sure i did)
  #3  
Old 10-Apr-2005, 17:09
redmage redmage is offline
New Member
 
Join Date: Apr 2005
Posts: 3
redmage is on a distinguished road
Ok, this is where I'm at now:

CPP / C++ / C 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
Code:
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'
Last edited by LuciWiz : 10-Apr-2005 at 17:19. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #4  
Old 10-Apr-2005, 17:39
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
You have lots of misplaced semicolons in your code:

CPP / C++ / C Code:
void rectangle::setWidth(double x;) // 1
{
    if ((x < 0.0;) || (x > 20.0;)) // 2, 3

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:
double getWidth() const;

//...

//      And later
int rectangle::getWidth()

It should be double AND const. You need to specify this too.

CPP / C++ / C Code:
double rectangle::getWidth() const
{
	return Width;
}

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:
rectangle::rectangle (double width, double length)
{
	Width = width;
	Length = length;
}

//or
rectangle::rectangle (double width, double length) : Width(width), Length(length)
{
	// other stuff
}

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  
Old 10-Apr-2005, 18:07
redmage redmage is offline
New Member
 
Join Date: Apr 2005
Posts: 3
redmage is on a distinguished road
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:
#include "Rectangle.h"

Rectangle::Rectangle()
{
	x = 1;
	y = 1;
}


Rectangle::Rectangle(int x, int y)
{
	width = x;
	length = y;
}

int Rectangle::getWidth() 
{ 
	return width; 
}

int Rectangle::getLength() 
{ 
	return length; 
}

int Rectangle::setWidth(int x)
{
	if ((x < 0) || (x > 20)) 
		x = 1;

	return getWidth();
}

int Rectangle::setLength(int y) 
{
    if ((y < 0) || (y > 20))
		y = 1;
	
	return getLength();
}

Header
CPP / C++ / C Code:
class Rectangle 
{
private:
	int x, y;
	int width, length;

public:
	Rectangle ();
    Rectangle (int, int);
	
	int setWidth(int x);
	int setLength(int y);
	int getWidth();
	int getLength();
	
	int area (void) { return (x * y); }
	int perimeter (void) { return (x * 2 + y * 2); }


};

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  
Old 10-Apr-2005, 19:31
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
It seems to me that setWidth() should actually set the width. Otherwise what is it for? In your code:
CPP / C++ / C Code:
int Rectangle::setWidth(int x)
{
  if ((x < 0) || (x > 20)) 
    x = 1;

  return getWidth();
}
checks the value of the parameter x then returns the width from your rectangle class. the input parameter is never used. You might want to consider testing x as you do and setting width to either x or 1 as appropriate.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 08:17.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.