Re: [Dev-C++] How would you write a method returning a string ?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2007-07-23 13:40:41
|
If you have functions that returns their answers without making use of any
state of a specific class, then I would recommend that you put these
functions into a namespace instead of into a class.
Methods to a class normally perform a service based on the contents of an
object, or changes the state of an object. With no involvement of an
object, they are very strange methods.
/pwm
On Mon, 23 Jul 2007, Jamiil Abduqadir wrote:
> Maize
>
>
> Good day Matheus and Adam, I agree with what you tow have to say about a stand alone function, but I also recognize the validity of my statement.
>
> A class provides not only a way to hide data or polymorphism, this alone is useless unless the class or one of its derivatives provides a service. To bring my point home, let us say that class Abecedarium, provides the sound and the location of the letter in the Abecedarium, and you want to create a class that handles queries about the letters of the alphabet in the English language, well, it would be a good Idea to use Abacedarium as you base class, thus making your point valid, for in this case the hiding data and polymorphism ins an important aspect of OOP. However, this alone does not provide any service to the user, unless the creator of the either class provides a "bunch" of methods to take information in or out of the class to be processed or that has been processed respectively, thus the API of the class. And this brings me to the point of adding the method I suggested to a class. For instance
>
> const std::string& MyClass::Char2Str(const char* x){ return str = x;}
> As Adam said, this is a waist of time 'since std::string::c_str' handles this very nicely, but what if the case is not to convert the letters from char to string alone, but to check the validity of the character-string, for instance that it does not have numbers or punctuation marks in it, inform the client of such a mistake, and so on and so forth, if the character-string is valid, then to remove any white spaces at the beginning or end of the character-string, then and only then the character-string passed as a char can be converted to a 'std::string'. If Abecedarium did not have a method(s) that would handle this operations then you will have to add it to your derived class. Which is my position on this subject.
>
> I would strongly recommend to add what ever function you find necessary to a particular class, MOSTLY for beginners, later on, as the beginner grows more familiar with the paradigm of OOP he or she will find that there are many classes that have the same method, which will make the new programmer create a class that will be the parent class of all those classes that have the same method, thus making his or her skills escalate from beginners to advanced. Later on, as an advance programmer, s/he will learn design and development making him or her a sort of an expert. C++ gurus are class of its own, so we don't talk about them
>
> I know that the are many loopholes in my brief explanation, however, time permited I have done all I can to help the C++ community. I want you to know that it has never been my intention to over shadow your opinion, which is just a valid as mine, but to only help to the best of my abilities.
>
>
> Have a good one.
|