Thread: Object-oriented Rectangle program

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    98

    Object-oriented Rectangle program

    Hi all, I'm new to C++ and have jumped right into object-oriented. Some of the concepts and syntax have me confused like no other. If anyone could take a look at the code an offer pointers/guidane it would be greatly appreciated.

    The jist of the assignment is to have code with a class Rectangle that calculates the area and perimeter and determines whether or not the obect is a square. There are some querky quideline such as have 3 overload constructor functions width 0-2 parameters.

    Here is the code:

    Code:
    //Rectangle.cpp
    //Rectangle class member functions
    #include
    <iostream>
    #include
    <iomanip>
    #include
    "Rectangle.h"
    using
    namespace std;
    Rectangle::Rectangle()
    {
        setLength(1.0);
        setWidth(1.0);
    }
    Rectangle::Rectangle(length)
    {
        setLength();
        setWidth(1.0);
    }
    Rectangle::Rectangle(length, width)
    {
        setLength();
        setWidth();
    }
    double
     Rectangle::calcArea()
    {
        
    return length*width;
    }
    double
     Rectangle::calcPerimeter()
    {
        
    return (2*length)+(2*width);
    }
    void
     Rectangle::setLength (double l)
    {
        
    if (l >= 0 && l <= 20)
            length = l;
        
    else
        {
            length = 1.00;
            cout<<
    "The length entered is invalid and has been set to 1.00";
        }
    }
    void
     Rectangle::setWidth (double w)
    {
        
    if (w >= 0 && w <= 20)
            width = w;
        
    else
        {
            width = 1.00;
            cout<<
    "The width entered is invalid and has been set to 1.00";
        }
    }
    double
     Rectangle::getLength() const
    {
        
    return length;
    }
    double
     Rectangle::getWidth() const
    {
        
    return width;
    }
    void
     Rectangle::booleanSquare()
    {
        
    /*
        if (length-width < .0001)
        */
            
    }
    void
     printData()
    {
        cout<< 
    "Length: " << length;
        cout<< 
    "Width: " << width;
        cout<< 
    "Area: " << area;
        cout<< 
    "Perimeter: " << perimeter;
    }
    

    Code:
    //Rectangle.h
    //Defintion of class Rectangle
    #ifndef
     RECTANGLE_H
    #define
     RECTANGLE_H
    class
     Rectangle
    {
    public
    :
        
    //overloaded constructor functions
        Rectangle ();
        Rectangle (
    double length);
        Rectangle (
    double length, double width);
        
        
    //set functions
        
    void setLength( double );
        
    void setWidth( double );
        
        
    //get functions
        
    double getLength() const;
        
    double getWidth() const;
        
    //calculation functions
        
    double calcPerimeter() const;
        
    double calcArea() const;
        
    //test for square
        
    void booleanSquare ();
        
    //print functions
        
    void printData();
    private
    :
        
    double length;
        
    double width;
    };
    #endif
    Code:
    
    
    Code:
    
    //Program to calculate the perimeter and area of a rectangle
    
    #include
    <iostream>
    
    #include
    "Rectangle.h"
    
    using
    namespace std;
    
    int
     main()
    
    {
        Rectangle object1;
        Rectangle object2 (7.1, 3.5);
        Rectangle object3 (6.3);
        Rectangle object4 (21, 22);
        Rectangle object5 = object2;
        
    void printData();
    
        
    /*
    
        Rectangle object1 (5.4, 10.5);
        Rectangle object4 (15.6, 15.6);
        */
    
        
    void printData();
    
    }
    


    A couple direct questions I have so far.

    How can I redefine the values of objects in main (one of the guidelines)

    I'm obviously way off on how the Rectangle functions should be written/work I'm receiving errors on most of them. Illegal reference to non static member.

    Why are my print function identifiers innaccurat?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Why are my print function identifiers innaccurat?
    When you use a member function of a class, you need, with one exception, an object to perform operations on. In other words, the object you call it with will provide the data members your member function will modify or use.

    Calling a member function with an object looks like this:
    Code:
    // This is object construction.
    Rectangle object;
    
    object.printData();
    Where you define the functions themselves, in Rectangle.cpp, you have to provide the types of parameters you use in constructors and member functions. Names are optional (edit: in header files), but it is a common good practice to leave them in, for documentation purposes. The compiler will use the number of, order of, and type of parameters to do static type checking. A program that omits this information is not well formed and will cause compiler errors.

    For the future: You are also not typing preprocessor directives correctly.
    Code:
    #include
    <iostream>
    
    #include
    "Rectangle.h"
    #includes and #defines and etc need to be on 1 line.

    I'm attaching your code to my post. I indented it for you and straightened out the #includes so now compiler errors will deal with the other things I talked about.

    Good luck.
    Attached Files Attached Files
    Last edited by whiteflags; 02-17-2013 at 07:01 PM.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    I actually had my #includes typed properly, I don't know why it did that. I'm guessing you answered my question but I'm going to ask it more specifically.

    In my header file I declare my 2nd overloaded constructor function with the type and the name.
    Code:
    
    Rectangle (double length);


    In my rectangle.cpp in my function header
    Code:
    Rectangle::Rectangle(length)
    
    


    but I recieve this error:

    error C2597: illegal reference to non-static member 'Rectangle::length'


  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If, in your class definition (as it's called; weird, I know), you have:

    Rectangle (double length);

    Then, in your accompanying .cpp file, you must have (note that the type is there)
    Code:
    Rectangle::Rectangle(double length)
    {
    	// Code goes here
    }
    Writing Rectangle::Rectangle (length) (by omitting the type), you are actually trying to do a function call. For this to work, the function must be declared static (ie static T Rectangle(double length) where T is some type). This will not work for constructors or destructors.

    Also, your formation is horrible. I suggest you read up on some common style (eg Allman) and use it. You can also use code prettifiers. It's up to you, but you can't submit code like this anywhere.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    My indentations don't look like that in my compiler, it copied funny I don't know why.

    My print function in my .cpp says the length,width, area, and perimeter are undeclared, are the statements written incorrectly?

    Can anyone tell me why my calc functions are receiving the error:
    overloaded member function not found in 'Rectangle'


  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You told the compiler that the printData function was global in your .cpp file:

    void printData()

    should be

    void Rectangle::printData()

    As for the calc functions, you forgot to add "const", which really makes it another function that what you've declared, so:

    double Rectangle::calcPerimeter()

    should be

    double Rectangle::calcPerimeter() const

    Same for the other.

    As for the formatation issue... it's good that it looks good in your editor, but less so that it looks like that when you post it. You really have to find a way around that problem. It's hard to read.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    Oh silly mistake on the print function. I tried to copy my code to notepad then here but with the same issues so I'll keep tinkering.

    My area and perimeter still come up unidentified, do I have to type length*perimeter instead of area in the print function, if so that seems like a waste of the calc functions.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include <iostream>
    
    class Rectangle
    {
    public:
    	Rectangle(double area, double length);
    	double CalcPerimeter() const;
    private:
    	double m_length, m_area;
    };
    
    Rectangle::Rectangle(double area, double length): m_area(area), m_length(length) {}
    
    double Rectangle::CalcPerimeter() const
    {
    	std::cout << "Hello World!\n";
    	return m_length * m_area;
    }
    
    int main()
    {
    	Rectangle MyRectangle(13, 37);
    	std::cout << "Perimeter: " << MyRectangle.CalcPerimeter() << std::endl;
    }}
    Perhaps you have some "invisible" newlines in your code that your editor ignores?
    Last edited by Elysia; 02-17-2013 at 09:36 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    It is my last section of code that doesnt want to copy properly but here is the updated.

    I'm not sure what you mean by invisible newlines either.

    I notice you print the perimeter in main but I am supposed to use the print function to display the information for the rectangle. i.e. the print function should give the length, width, area, perimeter, and whether or not the object is a square(when I get that far lol)
    Code:
    //Program to calculate the perimeterand area of a rectangle
    
    #include<iostream>
    #include"Rectangle.h"
    
    usingnamespace std;
    
    int main()
    {
           Rectangleobject1;
           Rectangleobject2 (7.1, 3.5);
           Rectangleobject3 (6.3);
           Rectangleobject4 (21, 22);
           Rectangleobject5 = object2;
    
    /*
           Rectangleobject1 (5.4, 10.5);
           Rectangleobject4 (15.6, 15.6);
           */
           object1.printData();
           object2.printData();
           object3.printData();
           object4.printData();
           object5.printData();
    
    
    }
    
    Code:
    //Rectangle.h
    //Defintion of class Rectangle
    
    #ifndef RECTANGLE_H
    #define RECTANGLE_H
    
    class Rectangle
    {
    public:
    
    //overloaded constructor functions
           Rectangle();
           Rectangle(double length);
           Rectangle(double length, doublewidth);
    
    //set functions
    void setLength( double);
    void setWidth( double);
    
    //get functions
    double getLength() const;
    double getWidth() const;
    
    //calculation functions
    double calcPerimeter() const;
    double calcArea() const;
    
    //test for square
    void booleanSquare ();
    
    //print functions
    void printData();
    
    private:
    
    double length;
    double width;
    
    };
    
    #endif
    
    Code:
    //Rectangle.cpp
    //Rectangle class member functions
    #include<iostream>
    #include<iomanip>
    #include"Rectangle.h"
    
    usingnamespace std;
    
    Rectangle::Rectangle()
    {
        setLength(1.0);
        setWidth(1.0);
    }
    Rectangle::Rectangle(double length)
    {
        setLength(length);
        setWidth(1.0);
    }
    Rectangle::Rectangle(double length, double width)
    {
        setLength(length);
        setWidth(width);
    }
    double Rectangle::calcArea() const
    {
        
    return length*width;
    }
    doubleRectangle::calcPerimeter() const
    {
        
    return (2*length)+(2*width);
    }
    voidRectangle::setLength (double l)
    {
        
    if (l >= 0 && l <= 20)
            length = l;
        
    else
        {
            length = 1.00;
            cout<<
    "The length entered is invalid and has been set to 1.00";
        }
    }
    void Rectangle::setWidth (double w)
    {
        
    if (w >= 0 && w <= 20)
            width = w;
        
    else
        {
            width = 1.00;
            cout<<
    "The width entered is invalid and has been set to 1.00";
        }
    }
    double Rectangle::getLength() const
    {
        
    return length;
    }
    double Rectangle::getWidth() const
    {
        
    return width;
    }
    void Rectangle::booleanSquare()
    {
        
    /*
        if (length-width < .0001)
        */
            
    }
    void Rectangle::printData()
    {
        cout<< 
    "Length: " << length;
        cout<< 
    "Width: " << width;
        cout<< 
    "Area: " << area;
        cout<< 
    "Perimeter: " << perimeter;
    }
    


  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The only problems I find are

    cout<< "Area: " << area;
    cout<< "Perimeter: " << perimeter;

    You don't have member variables or local variables named area and perimeter. Presumably you meant to call the functions that calculate those values.
    By "invisible" newlines, I meant that perhaps there are newlines in your code, just that your editor ignores them. It's impossible to see after you've pasted the code here, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Please remember that length and width are the only members of rectangle that you have. So you will have to call calcPerimeter and calcArea internally, in the PrintData function to get those values.

    And you need local variables to store them, optionally.

    When it comes to calling functions, there is a saying. "Use it or lose it, store it or ignore it."
    Code:
    cout << "Perimeter: " << calcPerimeter() << endl; // Use it or lose it
    
    double area = calcArea(); // store it or ignore it
    cout << "Area: " << area << endl;
    Losing it and ignoring it are not demonstrated, because all you have to do is ignore, or forget to store, the return value.

    Also, this is an aside, but it is good that your perimeter formula is 2l + 2w -- Elysia did something weird in his code, and I didn't know if he just learned a different way (?) ... I was about to call myself a dumb American.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    Also, this is an aside, but it is good that your perimeter formula is 2l + 2w -- Elysia did something weird in his code, and I didn't know if he just learned a different way (?) ... I was about to call myself a dumb American.
    Don't worry about it. I did not put much thought into that code.
    It was a simple example, not one meant to be correct in any way (for example, why should the result be a double if both parameters are integers?).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    Thanks white flags, I had never heard that saying but it will definately stick. I have my code compilng with no error but it shows no output when I debug. I don't know if it is my compiler or my code because this is the first time I have use Visual Studio and I am not sure if it installed correctly.

    If anybody else can run it and let me know the results that would be greatly appreciated.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    When I ran your corrected example, I got

    The length entered is invalid and has been set to 1.00The width entered is inval
    id and has been set to 1.00Length: 1Width: 1Area: 1Perimeter: 4Length: 7.1Width:
    3.5Area: 24.85Perimeter: 21.2Length: 6.3Width: 1Area: 6.3Perimeter: 14.6Length:
    1Width: 1Area: 1Perimeter: 4Length: 7.1Width: 3.5Area: 24.85Perimeter: 21.2

    Perhaps it is that you can't see the result because the console window disappears too fast. In that case, take a look at this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    I tried your solution to no avail Elsyia. The output window come up but with no output. I receive this at the bottom when I try to debug:
    'Project1.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
    'Project1.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
    'Project1.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
    'Project1.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
    'Project1.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
    The thread 'Win32 Thread' (0x2ddc8) has exited with code -1073741510 (0xc000013a).
    The program '[160524] Project1.exe: Native' has exited with code -1073741510 (0xc000013a

    If anyone knows what may cause that it would help if not its ok, I don't expect you guys to solve my technical issues.

    But I do have a program related question. I am supposed to redefine two of the objects and then print their outputs. How do I redefine an object. I have what I tried in comment brackets on my main.cpp and received an error when trying that.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Object Oriented C
    By CSaw in forum C Programming
    Replies: 6
    Last Post: 07-06-2011, 05:06 AM
  2. Employee Object Oriented Program (3 files)
    By Bulls2012 in forum C++ Programming
    Replies: 1
    Last Post: 05-24-2011, 06:17 PM
  3. Object oriented SDL
    By jamort in forum Game Programming
    Replies: 3
    Last Post: 01-31-2011, 09:07 AM
  4. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  5. object oriented C
    By FlatLost in forum C Programming
    Replies: 4
    Last Post: 11-08-2005, 06:22 AM