Thread: Functions and Classes - What did I do wrong?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    2

    Functions and Classes - What did I do wrong?

    I am doing an online into to C++ programming class. I know I'm on the right track (ie, using the right tools) 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 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.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #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'

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You don't put semicolons in an if-statement.
    Code:
    if ((x < 0.0;) || (x > 20.0;)
    Check your C++ book for the proper syntax.

    A function definition has to match the function declaration:
    Code:
    double getWidth() const;
    int rectangle::getWidth() <---
    {
       return Width;
    
    }
    While you're at it, you might want to consult a dictionary on the correct spelling of Lenght.
    Last edited by 7stud; 04-10-2005 at 06:06 PM.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    void rectangle::setWidth(double x;)
    {
        ...
    }
    
    void rectangle::setLength(double y;)
    {
        ...
    }
    And you can also remove those semicolons.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes being able to use other classes functions
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2006, 11:19 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. help with classes and member functions
    By syner in forum C++ Programming
    Replies: 4
    Last Post: 07-19-2002, 08:45 PM