Having trouble with Classes

This is a discussion on Having trouble with Classes within the C++ Programming forums, part of the General Programming Boards category; I went through the chapter of Classes and I though i had understood this pretty well, with class, private, public, ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    45

    Having trouble with Class

    I went through the chapter of Classes and I though i had understood this pretty well, with class, private, public, constructors, destructors ... So I tried to do a program from an exercise that seemed simple, but It compiling a bunch of errors, if anyone could put me on the right track to understand this, it would be great.
    the exercise is:
    Define a class called Plot that has private members of length and width. Include a constructor and a public function that calculates the area and the length of the boundary of the field. Use public functions in a program that computes and displays the area and the length of the boundary of the plot where the length and width are 7 and 9 respectively. Hint: The length of the boundary is 2 * (l + w).
    This is what I have done as far as the program goes:
    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    class Plot
    {
    public:
    
    	double setlength( double l );
    	double setwidth( double w );
    
    	double getlength();
    	double getwidth();
    
    	void area();
    	void perimeter();
    private:
    	double length;
    	double width;
    };
    Plot::Plot(double , double )
    {
      length = l;
      width = w;
    }
    Plot::setlength()
    {
    length = ( l >= 0.0 && l <= 7.0 ) ? l = 0;
    }
    Plot::setwidth()
    {
    	width = ( w >= 0.0 && w <= 9.0 ) ? w = 0;
    }
    void Plot::area()
    {
    	return (l * w);
    }
    void Plot::perimeter()
    {
    	return ( 2 * ( l + w ));
    }
    
    int main()
    {
    Plot rect( 7,  9 );
    cout << " The area of the rectangle is " << Plot.area() << endl;
    cout << " The perimeter of the rectangle is " << Plot.perimeter() << endl;
    	return 0;
    }
    These are the error codes I'm getting:
    Code:
    (27) : error C2511: 'Plot::Plot(double,double)' : overloaded member function not found in 'Plot'
    (11) : see declaration of 'Plot'
    (32) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    (32) : error C2511: 'int Plot::setlength(void)' : overloaded member function not found in 'Plot'
    (11) : see declaration of 'Plot'
    (36) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    (36) : error C2511: 'int Plot::setwidth(void)' : overloaded member function not found in 'Plot'
    (11) : see declaration of 'Plot'
    (41) : error C2065: 'l' : undeclared identifier
    (41) : error C2065: 'w' : undeclared identifier
    (50) : error C2661: 'Plot::Plot' : no overloaded function takes 2 arguments
    (51) : warning C4832: token '.' is illegal after UDT 'Plot'
    (11) : see declaration of 'Plot'
    (51) : error C2275: 'Plot' : illegal use of this type as an expression
    (11) : see declaration of 'Plot'
    (52) : warning C4832: token '.' is illegal after UDT 'Plot'
    (11) : see declaration of 'Plot'
    (52) : error C2275: 'Plot' : illegal use of this type as an expression
    (11) : see declaration of 'Plot'
    If anyone can help me understand what I'm doing wrong I would really appreciate it.
    thanks.
    Last edited by flicka; 10-07-2005 at 11:23 PM.

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    change your code to this:

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    class Plot
    {
    public:
                   Plot(double, double); //ADDED LINE
    
                    void setlength( double l ); //SHOULDNT RETURN DOUBLE
    	void setwidth( double w );  // SAME THING HERE VOID
    
    	double getlength();
    	double getwidth();
    
    	float area();  //RETURN VALUE CHANGED
                    float perimeter();  //SAME THING
    private:
    	double length;
    	double width;
    };
    
    Plot::Plot(double , double )
    {
      length = l;
      width = w;
    }
    
    void Plot::setlength()  //ALWAYS PUT THE RETURN TYPE!!!
    {
    length = ( l >= 0.0 && l <= 7.0 ) ? l = 0;
    }
    void Plot::setwidth()  //RETURN TYPE AGAIN!
    {
    	width = ( w >= 0.0 && w <= 9.0 ) ? w = 0;
    }
    float Plot::area()  //YOUR RETURNING WHY WAS THIS VOID
    {
    	return (l * w);
    }
    float Plot::perimeter()  //SAME THING HERE
    {
    	return ( 2 * ( l + w ));
    }
    
    int main()
    {
    Plot rect = new Plot(7,9);  //INSTANTIATE PLOT
    cout << " The area of the rectangle is " << rect.area() << endl;
    cout << " The perimeter of the rectangle is " << rect.perimeter() << endl;  //YOU CANT CALL CLASS VARIABLES THAT DONT EXIST, USE THE VARIABLE
    	return 0;
    }
    I think that's it, phew!
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Durban thanks I've made the changes but I'm still getting a bunch of errors that I don't understand. If anyone can give a hand to figure this out it would be great.
    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    class Plot
    {
    public:
                   Plot(double, double); //ADDED LINE
    
                    void setlength( double l ); //SHOULDNT RETURN DOUBLE
    	void setwidth( double w );  // SAME THING HERE VOID
    
    	double getlength();
    	double getwidth();
    
    	float area();  //RETURN VALUE CHANGED
                    float perimeter();  //SAME THING
    private:
    	double length;
    	double width;
    };
    
    Plot::Plot(double , double )
    {
      length = l;
      width = w;
    }
    
    void Plot::setlength()  //ALWAYS PUT THE RETURN TYPE!!!
    {
    length = ( l >= 0.0 && l <= 7.0 ) ? l = 0;
    }
    void Plot::setwidth()  //RETURN TYPE AGAIN!
    {
    	width = ( w >= 0.0 && w <= 9.0 ) ? w = 0;
    }
    float Plot::area()  //YOUR RETURNING WHY WAS THIS VOID
    {
    	return (l * w);
    }
    float Plot::perimeter()  //SAME THING HERE
    {
    	return ( 2 * ( l + w ));
    }
    
    int main()
    {
    Plot rect = new Plot(7,9);  //INSTANTIATE PLOT
    cout << " The area of the rectangle is " << Plot.area() << endl;
    cout << " The perimeter of the rectangle is " << Plot.perimeter() << endl;  //YOU CANT CALL CLASS VARIABLES THAT DONT EXIST, USE THE VARIABLE
    	return 0;
    }
    Here are the errors I get:
    Code:
     (30) : error C2065: 'l' : undeclared identifier
    (31) : error C2065: 'w' : undeclared identifier
     (35) : error C2511: 'void Plot::setlength(void)' : overloaded member function not found in 'Plot'
     (11) : see declaration of 'Plot'
     (39) : error C2511: 'void Plot::setwidth(void)' : overloaded member function not found in 'Plot'
     (11) : see declaration of 'Plot'
     (53) : error C2440: 'initializing' : cannot convert from 'Plot *' to 'Plot'
            No constructor could take the source type, or constructor overload resolution was ambiguous
     (54) : warning C4832: token '.' is illegal after UDT 'Plot'
     (11) : see declaration of 'Plot'
     (54) : error C2275: 'Plot' : illegal use of this type as an expression
     (11) : see declaration of 'Plot'
     (55) : warning C4832: token '.' is illegal after UDT 'Plot'
     (11) : see declaration of 'Plot'
     (55) : error C2275: 'Plot' : illegal use of this type as an expression
     (11) : see declaration of 'Plot'
    Thanks.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    void Plot::setlength( double l )
    {
            length = ( l >= 0.0 && l <= 7.0 ) ? l = 0;
    }
    void Plot::setwidth( double w )
    {
    	width = ( w >= 0.0 && w <= 9.0 ) ? w = 0;
    }
    float Plot::area()  //you should change this to return double instead
    {
    	return (length * width);
    }
    float Plot::perimeter()  //you should change this to return double instead
    {
    	return ( 2 * ( length + width ));
    }
    It would work this way. But you should change the returntypes for area() and perimeter() to double.

    Code:
    int main()
    {
       Plot rect = new Plot(7,9);  // you create a pointer to Plot here
       cout << " The area of the rectangle is " << rect->area() << endl;
       cout << " The perimeter of the rectangle is " << rect->perimeter() << endl;
       delete rect. // you have to free the memory
        // you could also use Plot this way 
       Plot orect(7,9);  // you create a Plot on the stack
       cout << " The area of the rectangle is " << orect.area() << endl;
       cout << " The perimeter of the rectangle is " << orect.perimeter() << endl;
       return 0;
    }
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. having trouble using classes
    By rainman39393 in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2008, 03:45 AM
  2. Trouble with private portion of Classes
    By SoBlue in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2007, 05:32 PM
  3. Having Trouble Understanding Classes
    By prog-bman in forum C++ Programming
    Replies: 1
    Last Post: 05-19-2004, 01:44 PM
  4. Trouble Understanding Classes and Objects. Please Help.
    By Jeffcubed in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 02:23 PM
  5. Trouble with classes
    By PorkyChop in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2002, 02:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21