Rectangle Class with postfix

I am having a tough time trying to take my rectangle class program and implement a postfix. Is that even possible?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "pch.h"
#include <iostream>
#include <conio.h>
#include <ctype.h>
#include <cmath>
#pragma warning(default:4716)  

using namespace std;

class rectangle
{
	int length;
	int width;

public:
	int getArea(int l, int w)
	{
		int area;
		area = l * w;
		cout << "The area or the rectangle is: " << area << "\n";
	}
};

int main()
{
	int l, w;
	rectangle rect;
	cout << "Enter the rectangle length: ";
	cin >> l;
	cout << "Enter the rectangle width: ";
	cin >> w;
	rect.getArea(l, w);
	return 0;
}
What exactly are you asking..? https://en.wikipedia.org/wiki/Postfix

BTW, your getArea function says that it returns an int, but you have no return statement.
return statement != print statement. return area;
I am trying to add a postfix to this code. I know based on reading that postfix is something processed after the fact and was thinking if there was a way to add +1 to whatever someone enters and then recalculate the new area but using postfix functions.
You're referring to the postfix increment operator (my_var++)?

Uh, something like this?
1
2
3
4
5
6
7
	cout << "Enter the rectangle length: ";
	cin >> l;
	l++; // increments l by 1
	cout << "Enter the rectangle width: ";
	cin >> w;
	w++; // increments w by 1
	
Yes but would I define l++ and w++ in the class?
you have some 'weirdness' going on here.
class rectangle provides 2 members, length and width, but you don't use them, you use l and w.

presumably, you would have a setter and getter function for length and width:

void setWidth(int w) {width = w;}

and your area function would not have parameters.
the code becomes
rectangle r;
r.setWidth(3);
r.setLength(5);
cout << r.getArea(); //3*5 = 15

having the class increment these itself automatically is doable, but it would be an 'unusual' design. There are times you want to do that (eg, a counter that auto-increments when something is done) but no one expects a 'normal' rectangle to just change its dimensions behind the scenes.

alternatively, you can do away with the class entirely and just have a function.
that is what you have now, sans the class:
int getArea(int l, int w)
{
return l*w;
}

which is perfectly valid. C++ does not demand a class for everything and stand alone functions have uses.
Last edited on
Nicely put. Was going to suggest the same but was too lazy.
Ah. I see. Well I can certainly change my class. I have to have a class for this assignment I am just very confused on how I take my rectangle class and the data entered and apply this postfix request to it. The textbook we have is simply awful, but then I think the majority of textbooks are written from the view of the author who normally can code in their sleep and they do not take into account the end-user. Much like programmers I have found within my org. Thus being a SQL guru, I always and I mean always dumb everything down for those I train. Not that they are dumb. Just beginners and I feel it is important to help everyone and anyone that asks. I cannot tell you how many of those I train ask me why I am on Corp. America and not teaching in a classroom. Pay. That is the reason. If I am unable to figure out this whole postfix thing I will just code whatever I think wrong or right and submit. I only have until probably tomm am to get this done as a hurricane is about to hit me head on and I will lose Internet by the time assignment is due. At least I am trying to understand and not giving up until the last possible moment.
well, the right thing to do here is probably going to look like

r.setWidth(r.getWidth()+1); //getters and setters approach

or write an increment method and it becomes
r.incrementWidth();

and if you wanted to increment both at once, overloading the ++ operator is doable, looking like
r++; //increment both in this method?

and there is the simple public approach (public is frowned upon by design but ... I can take a frown personally)

class rect
{
public:
int l,w;
//etc
};

///
r.w++; //since they are public, you can just increment them.
r.l++;

There are indeed a lot of bad books out there. The web is your friend, as are good forums. You can get good help here if you ask good questions and try to do the work yourself and show that.

there is NOTHING stopping you from incrmenting after doing the area function. Its just 'strange' behavior for a 'normal' concept of a rectangle. Mechanically, you can do almost anything you want in c++, anywhere you want. Its up to you to decide if what you did really makes sense and can be understood by others -- I have pushed you away from this idea because it is abnormal; but if you renamed your class to selfincrementingrectangle or something that indicates its behavior, this idea would be more acceptable.
Last edited on
If you want to use classes, do something like what jonnin said.
I would also define a constructor for the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Example program
#include <iostream>

class rectangle
{
	int length;
	int width;
	
  public:
  
    // Constructor:
    rectangle(int len, int wid)
    {
        length = len; // assign len to the member variable called length
        width = wid; // assign wid to the member variable called width
    }
    int getArea()
    {
        return length * width;
    }
    int getAreaAndIncrement()
    {
        // I'm still not sure what you want here as far as a "postfix" but you could do something like this also:
        return (length++) * (width++);
    }
};


int main()
{
    using namespace std;
    
    rectangle rect(5, 3); // creates a rectangle with length == 5, width == 3
    
    int area;
    area = rect.getArea();
    cout << "The area of the rectangle is: " << area << "\n";
    
    area = rect.getAreaAndIncrement();
    cout << "The area of the rectangle is: " << area << "\n";
    
    area = rect.getAreaAndIncrement();
    cout << "The area of the rectangle is: " << area << "\n";
}
Last edited on
That is for sure. I always say at work that Google is my best friend. I will play around with this some more. Yes, we are in the public mode right now but starting to implement private.
Topic archived. No new replies allowed.