class Functions

Question: If you are designing a program that receives and returns (void/sets and return/gets), is it better to write individual void and return functions than to write functions that do both receive and return? What are the benefits?
closed account (zb0S216C)
I don't understand your question. Can you elaborate with an example?

Edit in response to Eyad: That doesn't help.

Wazzak
Last edited on
say whaaaat!!

Framework: what would help?! or who!!

i posted a problem at 2:23 am and i did not get a solution till now, all what i got is more problems "empty talk", well i am thankful for whoever tried to help, but they actually did not help, because all of what they give me is some useless comments and problems rather than giving me a solution!!

so chill out man.
Last edited on
He's asking is it better to overload a single function name to be used as both accessor and mutator, like so:

float getX() { return x; }
void setX(float in_x) { x = in_x; }

vs.

float X() { return x; }
void X( float in_x ) { x = in_x; }

Read this:

http://www.kirit.com/C%2B%2B%20killed%20the%20get%20%26%20set%20accessors


@OP, I've had trouble in the past when I use the latter form. I would get a compiler error about function not defined or a linker error and I wasn't really sure why I was getting the error, but I also haven't mastered the language and all its complexities, which is why I tend to minimize the use of classes and objects. I like the latter method more, though.
Last edited on
ie: Same Program written 2 different ways:

//This program demonstrates individual get and set functions.
#include <iostream>
using namespace std;

class Rectangle
{
private:
double length,width;

public:
void setlength(double);
void setwidth(double);
double getlength();
double getwidth();
double getarea();
};

void Rectangle :: setlength (double x)
{
length=x;
}

void Rectangle :: setwidth (double y)
{
width=y;
}

double Rectangle :: getlength ()
{
return length;
}

double Rectangle :: getwidth ()
{
return width;
}

double Rectangle :: getarea ()
{
return length*width;
}

int main()
{
Rectangle lou;
double a,b;

cout<<"Length: ";
cin>>a;
cout<<"Width: ";
cin>>b;

lou.setlength(a);
lou.setwidth(b);

cout<<endl;
cout<<"The values you entered are:"<<endl;
cout<<"Length: "<<lou.getlength()<<endl;
cout<<"Width: "<<lou.getwidth()<<endl;
cout<<"Total area is: "<<lou.getarea()<<" units."<<endl;

cin.get();
cin.get();

return 0;
}

//This program combines get/set functions.
#include <iostream>
using namespace std;

class Rectangle
{
private:
double length,width;

public:
double getset(double,double);
};

double Rectangle :: getset(double x, double y)
{
return x*y;
}

int main()
{
Rectangle lou;
double a,b

cout<<"Length: ";
cin>>a;
cout<<"Width: ";
cin>>b;

cout<<"The values you entered are: "<<endl;
cout<<"Length: "<<a<<endl;
cout<<"Width: "<<b<<endl;
cout<<"Total area is: "<<getset(a,b);

cin.get();
cin.get();

return 0;
}

Both programs calculate length and width but the first program uses individual get and set function. What is the reason for writing a program this way when you can get the same result with less coding like in the second program?Which way is beneficial and why?
Hi

The first one is a class of Rectangle with all necessary methods and members.
It is well implemented, a rectangle class should implemented in this way.

The second one is not a real rectangle class, in reality it is just a function. Instead of this class
you can just have the function getset independence of the class.

double getset(double x, double y){ return x * y;}

Forgot the second class, it is a very bad example


Last edited on
Normally getters/setters are done with function overloading:
1
2
3
4
5
6
7
8
int NameOfVar() const
{
    return nameOfVar;
}
void NameOfVar(int NewValue)
{
    nameOfVar = /*validate*/NewValue;
}
Having a getter and setter be the exact same member function is a bad idea because it ruins syntax to create a workaround for knowing whether you are setting or not.

Note: I am assuming that by "functions that do both receive and return" he means a single function that both returns the value and allows you to pass a parameter to set it to, without overloading.

Also, if you follow "tell, don't ask" you would never even run into this problem.
Last edited on
Topic archived. No new replies allowed.