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?
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!!
@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.
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;
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?
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.
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.