2

I am fairly new to programming and am confused over when I should make a function a member function of a class or just use member getter functions to access the private members of the class. I can do it either way I think.

Consider the following:

#include <iostream>
#include <string>
using namespace std;

class Person
{  
  string name;
  int age;
public:
  Person()
    : name("James"), age(30)
  {};
  void Print();
  string GetName(Person& person) { return name;};
  int GetAge(Person&) { return age;};
};

void Person::Print() // member function
{
 cout << "Using member function: " << name << ", " << age << endl;
}

void Print(Person& person) // non-member function 
{
 cout << "Using non-member function: " << person.GetName(person) << ", " << person.GetAge(person) << endl;
}

int main() 
{
Person test_person; //default constructor
test_person.Print(); // member function
Print(test_person); // non-member function

return 0;
}

The output is the same for both the member function Person::Print() or the non-member function Print() i.e. the program produces:

Using member function: James, 30
Using non-member function: James, 30

You can clearly then write a non-member function that uses member getter functions to access private members of the class so you can do it either way.

It seems to me like making Print() a member function of class Person is the way to go since the function is clearly specific to the class and its private data and will probably want to be used by someone else if they use the class.

Is that right? What else should I be considering?

Reno
  • 1,039
  • 3
  • 14
  • 22
  • Does this answer your question? [Free function versus member function](https://stackoverflow.com/questions/21028773/free-function-versus-member-function) – Scheff's Cat Jun 06 '20 at 09:19
  • 1
    @George I retracted my close-vote but I'm still somehow convinced that it would be of help. As far as I understood it, a class should provide as less as possible member functions which are essentially needed to use it i.e. to retrieve internals which should be exposed as well as to modify internals in a more or less complex way. Everything else should become a free function using these member functions. – Scheff's Cat Jun 06 '20 at 09:24
  • Thanks I didn't know the non-member was called a Free function which is why nothing came up when I searched. – Reno Jun 06 '20 at 09:33
  • @Scheff Fair point. I think the answers do actually cover the op's question. If in a slightly dense/concise way from the p.o.v of their question (but maybe that's just me being tired). – George Jun 06 '20 at 09:35
  • Consider using the standard [`` header](https://en.cppreference.com/w/cpp/header/functional) and C++ [lambda expressions](https://en.cppreference.com/w/cpp/language/lambda) – Basile Starynkevitch Jun 06 '20 at 10:41

3 Answers3

0

Your code is similar to the codes below:

private:
 string name;
 int age;

these attributes is only access inside the class object. You can not call it from outside the object, And you only use geter(), seter() to get, set value for the object.

If you use the public attributes, there is similar to struct structure on C/C++

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
cnp
  • 339
  • 2
  • 11
0

I think you may want to consider taking another look on access modifiers. You can not modify private or protected data members of a class. Thus you need member functions for them.

Umair
  • 123
  • 2
  • 10
  • If you might want to modify the private or protected data members of a class from your main / another class. Maybe you should consider if they really should be private and / or part of that class. – darclander Jun 06 '20 at 10:48
  • Access and modify mean two very different things. – George Jun 06 '20 at 11:56
0

Prefer member functions over non-member functions. Suppose you change the name from Print to Log and you forget the name. For most IDE, a member function could be easily looked up by typing "." then find the right one, while non-member function will require several keystrokes of guessing before finding the right one.

I will also say member functions are more intuitive to use for many cases.

Implementing both are not considered bad practice as well. For example, for std::vector v, there is member function v.size() as well as the general purpose non-member function std::size(v)

charliepu123
  • 59
  • 1
  • 7