Thread: I need help with making a rectangle using a class!

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    3

    I need help with making a rectangle using a class!

    Hey so I'm having trouble making this work. I do not know how to fix this code and I need some help!
    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    class Rectangle {
        int width, height;
      public:
        void set_values (int,int);
        int area() {
           int answer;
          
           return answer;
        }
        Rectangle(int w, int h) {  // parameterized constructor
      
        }
    };
    
    void Rectangle::set_values (int x, int y) {
      
    }
    
    int main () {  
    
      Rectangle rect1(5,6);
      cout << "area: " << rect1.area() << endl;
    
      Rectangle rect2;
      rect2.set_values (3,4);
      cout << "area: " << rect2.area() << endl;
      return 0;
    }

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    811
    That looks to me like homework, where you've been given sample code to finish, but haven't done touched it yourself. Give a suggestion of how you think this could be solved and we can assist if problems arise.

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    3
    it is homework, I believe that I have to change it so the class Rectangle can receive values and input it as its values.

    I'm not a very avid coder, and I struggle a lot. I would really appreciate it if you can teach me how to do this!

  4. #4
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    811
    Syntax for constructors is:
    Code:
    SomeClass(int a, int b)
    {
        member_a = a;
        member_b = b;
    }
    
    // Modern approach via initialization list, preferred
    SomeClass(int a, int b) : member_a(a), member_b(b)
    { }
    What do you think the area() function needs to compute? It's a mathematical question, nothing about coding.

  5. #5
    Registered User
    Join Date
    Jun 2017
    Posts
    3
    It needs to compute the area of the rectangle?

  6. #6
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    811
    Well yes, but how is the area of a rectangle computed? How does this relate to the data stored in your Rectangle class? You'll need to do the work. Your teacher won't have sent you home with this assignment without first explaining the necessary parts, like working with variables.

    Anyway, I've given you the example for the Constructor. And the first part using regular x = y style assignment works all the same for a set_values kind of function.

    Homework Policy
    Last edited by -Adrian; 06-25-2017 at 05:36 PM.

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    So, I had a lot of fun coding this up. It's so over-engineered that it can't possibly be turned in for a homework assignment: Compiler Explorer - C++

    But that's some type-safe, awesome C++ for ya!

    Boost's strong typedefs are severely underrated. The coolest part is, you can see in the assembly where the compiler calculates the area before the program is even run:
    Code:
    mov     esi, 169

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,609
    It would appear that the entire class instance has been optimized down to a single integer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 06-23-2010, 02:58 AM
  2. Rectangle class
    By blackant in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2009, 08:33 AM
  3. Making Rectangle List Non-Overlapping
    By SMurf in forum Game Programming
    Replies: 2
    Last Post: 05-06-2006, 01:23 PM
  4. Rectangle class
    By edshaft in forum C++ Programming
    Replies: 0
    Last Post: 12-15-2001, 12:29 PM
  5. Class rectangle/need help thanks!!!
    By C++Newbie in forum C++ Programming
    Replies: 11
    Last Post: 11-16-2001, 04:00 PM

Tags for this Thread