Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add function in class but not in header file in C++?

I am currently practicing writing classes and header files in C++. I have a question: let's say that in my header file I have a public function that a client can use, and I know how to implement it in the respective class. However, let's say that this function is divided into several steps that can be written as independent functions that I don't want the user to see (protect intellectual property). Normally, for each defined function in the header file, I would write in myClassName::myFunctionName(parameter 1 ..) in the .cpp file. Is there a way to define and use functions only in .cpp file? For example, I wrote a program to see if two words are anagrams (have the same letters).

My header file is:

#ifndef _Anagrams_h
#define _Anagrams_h
#include <string>
using namespace std;

class Anagrams{
    public:
        Anagrams(string &s);
        static bool areTwoWordsAnagrams(string s1, string s2) ; 
        string getWord()const; 
        void setWord(string &s);

    private:
        string word;

};
#endif

My class is:

#include "Anagrams.h"
#include <string>
using namespace std;

Anagrams::Anagrams(string &s){
    word = s;
}

bool Anagrams::areTwoWordsAnagrams(string word1, string word2){
    int sizeOfWord1 = word1.size();
    int sizeOfWord2 = word2.size();

    int array1[26];
    int array2[26];

    for (int i = 0; i < 26; i++){ //Initialize both arrays
        array1[i] = 0;
        array2[i] = 0;
    }


    decomposeWordIntoLetters(word1,array1,sizeOfWord1);
    decomposeWordIntoLetters(word2,array2,sizeOfWord2);

    return true;
}

string Anagrams::getWord() const{
    return word;
}

void Anagrams::setWord(string &s){
    word = s;
}

void decomposeWordIntoLetters(string word, int array[], int size){
    for (int i = 0; i < size; i++){
        char letter = word[i];
        array['z' - letter]++;
    }
}

Notice that the decomposeWordIntoLetters function is not defined in the header file. If I copy and paste the code twice in Anagrams::areTwoAnagrams(string word1, string word2), the program works. Otherwise, I get the following error:

Anagrams.cpp: In static member function ‘static bool Anagrams::areTwoWordsAnagrams(std::string, std::string)’:
Anagrams.cpp:22: error: ‘decomposeWordIntoLetters’ was not declared in this scope

Any help would be greatly appreciated. Thank you.

like image 519
Johnathan Avatar asked Jun 14 '13 01:06

Johnathan


People also ask

Can classes have member functions?

In addition to holding data, classes (and structs) can also contain functions! Functions defined inside of a class are called member functions (or sometimes methods). Member functions can be defined inside or outside of the class definition.

Can you put function definitions in header files?

Function and type declarations, global variables, structure declarations and in some cases, inline functions; definitions which need to be centralized in one file. In a header file, do not use redundant or other header files; only minimal set of statements. Don't put function definitions in a header.

Do classes go in header files?

Classes are no different. Class definitions can be put in header files in order to facilitate reuse in multiple files or multiple projects. Traditionally, the class definition is put in a header file of the same name as the class, and the member functions defined outside of the class are put in a .

Can class and function have same name?

Yes, It is allowed to define a method with the same name as that of a class. There is no compile-time or runtime error will occur.


1 Answers

You can definitely have non-member functions in your cpp file. However, these functions may not be used before they declared or defined.

To declare a function, provide its prototype, like this:

void decomposeWordIntoLetters(string word, int array[], int size);

Put this line above the member function that calls decomposeWordIntoLetters. This should fix the compile problem that you are seeing.

When you define functions like that, you may want to hide them not only from the header, but also from other modules that link to your library. In order to do that, declare the function static:

static void decomposeWordIntoLetters(string word, int array[], int size);

Note that when you do that to a free-standing function, the meaning of static is completely different: the function does not become a class function as class-scoped static functions; instead, it becomes a function with the visibility limited to the translation unit (i.e. a single cpp file where it is defined).

like image 100
Sergey Kalinichenko Avatar answered Oct 25 '22 23:10

Sergey Kalinichenko