Learning classes, can this code be simplified?

Hello everyone, I'm a fairly experienced programmer but learning c++ for the first time. I've done a fair amount of things in python and other high-level languages and have decided to move down to increase my capabilities and write more advanced programs.

I find c++ to be a bit challenging though, mostly because I have been spoiled with how easy it is to write things in Python and stuff just gets accepted.

My question is: Can this basic class be written in more effective ways? I figured it out on my own, now I want to learn how to improve. Thanks for help!

Do I really need to call void in my request_x?
Does it really need to return 0 (or null?)?

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
class aRect{
	int x, y;

public:
	void set_value(int X, int Y){
		x = X;
		y = Y;
	}
	int get_area(){
		int area = x*y;
		return area;
	}
	int request_x(void){
		int temp;
		cout << "Please write your x value: ";
		cin >> temp;
		x = temp;
		return 0;
	}
	int request_y(void){
		int temp;
		cout << "Please write your y value: ";
		cin >> temp;
		y = temp;
		return 0;
	}
};
Do I really need to call void in my request_x?

Not sure what you're asking here, but you don't need to specify void in the parameter listing if you don't want. It's implied if left empty.

Does it really need to return 0 (or null?)?

I think I see your issue now.

int request_x(void)
The int here is the return value. If it is anything but void, then you have to specify a return statement in the function. The void in between the parentheses is your parameter listing. Void is saying it takes no parameters, so you can just completely leave out void if you want. It's just preference.

Since your functions all just return 0 no matter what, you might as well change the type to be void and just get rid of the return statements all together.
area function:
you can write return (x*y) eventhough the area variable makes it easier to read imo

request functions:
i don't see a reason why they are int and return 0. just make those void
You can omit the void if you want, it makes no difference. In this context, it's nothing more than an indication that there are no parameters passed into that function.

There are a couple things to note, though:

1) get_area has a pretty unnecessary variable declaration and assignment. You could just have easily have done return x*y;.

2) request_x and request_y sound like they should be retrieving the x and y values respectively. You're actually setting them though.

This would return the value, if that's what's required:
1
2
3
4
int request_x()
{
   return x;
}
Simplify it like so:
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
class aRect{
	int x, y;

public:
	void set_value(int X, int Y){
		x = X;
		y = Y;
	}
	int get_area(){
		int area = x*y;
		return  x*y area;
	}
	void int request_x(void){
		int temp;
		cout << "Please write your x value: ";
		cin >> x temp;
		x = temp;
		return 0;
	}
	void int request_y(void){
		int temp;
		cout << "Please write your y value: ";
		cin >> y temp;
		y = temp;
		return 0;
	}
};
woo! Fast replies, thanks guys!
ResidentBiscuit: Thanks, excellent explanation!
Darkmaster: Worked perfectly, THANKS!
iHutch: that "request"-name was missleading indeed, changed it now to set_x and set_y instead.

OK so I moved on to create what I call a "date tracker" - which is essentially something I recreate when I want to learn a new programming language. It is basically a date class with an int in the format YYYYMMDD (year, month, day) or just a simple YYYY and then lets you choose to do all kinds of things with that date like "How many days ago was this date?" "How many days are left until this date?" "Was this year a leap year?"

So I'm making my class right now and I'll just post another question if I have any. Should be a fun introduction :)
coder777: Thanks! That little piece of code was valuable!

especially the cin >> y
Came across this as a way of doing the same thing but why would I ever put my function outside of my class?

1
2
3
4
5
6
7
8
9
10
11
class Crect {
    int x, y;
  public:
    void set_values (int,int);
    int area(){return (x*y);}
};

void Crect::set_values (int a, int b) {
  x = a;
  y = b;
}
devoto wrote:
changed it now to set_x and set_y instead.

In that case, I have one further suggestion.

By including the cin and cout commands, your class now becomes dependent on the iostream library. Essentially, this will make it less portable and (not particularly relevant here) can increase build times.

For setter functions, I'd suggest using something like your set value:
1
2
3
4
void SetX( int x )
{
   this->x = x;
}


Then, in main (or wherever), the call would be, for example:
1
2
3
aRect myRect;

myRect.SetX( 5 );


The class is no longer dependent on the iostream library and can be used in non command line projects, making it more portable.
Last edited on
devoto wrote:
Came across this as a way of doing the same thing but why would I ever put my function outside of my class?

It's not outside of the class, exactly. The scope resolution operation, in this case Crect::, denotes that it's part of the Crect class.

In C++ project, it's not uncommon to write the class declaration in a header file (.h extension) and the class definition (i.e. all of the function bodies) in a source file (.cpp extension).
Last edited on
would I ever put my function outside of my class?
Because you do not want to pollute your interface/class. A clean interface makes it much easier to use/document your class.
Came across this as a way of doing the same thing but why would I ever put my function outside of my class?


Often in C++ (which is unlike Java and C#) people like to separate their declarations and implementations. You can put declarations in header files, and have them include the necessary cpp files. This lets you only have to worry about including whatever header files you want to use. It also makes it easier to find a certain method. If the header file is essentially a listing of the methods and members of a class, you can easily scan through that to find what you're looking for.
iHutch: excellent advice, I'm reading up on the documentation on classes right now so I'm still getting the hang of writing in this new language. Pun.

I still find myself forgetting semi-colons (;) and using way to much white space. My code is pretty and readable but I wanted to learn C++ to write more effective code. Your advice is happily accepted!
aaah, right! Thanks everyone!
Topic archived. No new replies allowed.