How to define a private function

1

Someone could tell me how I should define a private function in c ++. I have this class definition.

#ifndef AUTHOR_H_
#define AUTHOR_H_


#include <string>
#include <iostream>
using namespace std;
class Author {

private:
    string name;
    string email;
    char gender;


public:
    Author();
    Author(string name, string email, char gender);
    Author(const Author &rAuthor);
    Author & operator=(const Author &rAuthor);
   ~Author();

    string getName() const;
    string getEmail() const;
    char getGender() const;
    void setEmail(string email);
    void setEmail_2(string email);
    void print() const;
    bool checkGender(char gender) const;
};

#endif /* AUTHOR_H_ */

And I would like the checkGender method to be private, but if I put it in the private part the Author.h file, when defining the functions it does not appear to me as belonging to the class Author . I want to define it as private because it will be used by the constructor of my class, but I do not want to make it visible.

Author.cpp File:

#include "Author.h"

Author::Author(){}

Author::Author(string name,string email, char gender){
    this->name=name;
    Author::setEmail_2(email);
    if(Author::checkGender(gender))
        this->gender=gender;
    else
        this->gender='';
}

bool Author::checkGender(char gender) const{
    if(gender == 'm' || gender == 'f' || gender=='u'){
       return true;
   }else{
       return false;
   }
}

I think this is all the info, anything you tell me. Thanks.

If I change it to the private zone of the class you understand, var does not appear in the list of available methods, should it not appear in the methods of the class?

 class Author {

 private:
    string name;
    string email;
    char gender;
    bool checkGender(char gender) const;


public:
     Author();
     Author(string name, string email, char gender);
     Author(const Author &rAuthor);
     Author & operator=(const Author &rAuthor);
     ~Author();

     string getName() const;
     string getEmail() const;
     char getGender() const;
     void setEmail(string email);
     void setEmail_2(string email);
     void print() const;
     //bool checkGender(char gender) const;

};

    
asked by Jcpardo 23.04.2018 в 22:37
source

2 answers

1
  

but if I put it in the private part of the Author.h file, when defining the functions it does not appear to me as belonging to the class Author

That's something you should discuss with your IDE. The way to declare private functions is precisely like this, declaring them in the%% share%. In your case the program should work correctly and, if it does not compile, look to see what you are setting wrong in your IDE:

class Author {

 private:
    bool checkGender(char gender) const;
};

bool Author::checkGender(char gender) const
{
}
    
answered by 24.04.2018 в 07:41
0

I'm on my cell phone so I can not give you a precise answer. Follow this example to adapt it to your needs, assume the following, You have your class :

class Class1 {
    private:
    void metodo1();

    public:
    void metodo2();
};

Implement the function in your class :

void Class1::metodo1() { // <-- private
   // tu codigo
}

void Class1::metodo2() { // <-- public
   // tu codigo
   metodo1();              // <-- Llamando la función privada aquí.
}
    
answered by 23.04.2018 в 22:51