C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 10th, 2005, 12:50 PM
redmage redmage is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 2 redmage User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 11 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old April 17th, 2005, 11:34 PM
BoolBooB BoolBooB is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 36 BoolBooB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 35 m 42 sec
Reputation Power: 4
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;
}
 

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Functions and Classes - Where did I go wrong?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT