Interfaces vs. Header Files

I am trying understand what I am supposed to put in a header file. It seems like the same thing as an interface.

Interface -> define functions required for a class and their signatures
Header File -> I was taught not to put the functions here, but just their input and output so that if someone wanted to learn about my code they wouldn't have to look through my cpp code

BUT THAT'S THE SAME THING AS AN INTERFACE!

Am I missing something here...
BUT THAT'S THE SAME THING AS AN INTERFACE!


Yep. Typically the interface goes in the header, and the implementation goes in the cpp file(s).
Can I ask you a favour. I'm started learning c++ less than a week ago. The concept including, creating, and implimenting interfaces is still foggy for me.

Could you write some code to show me.
Last edited on
helloWorld.h:
1
2
3
4
5
6
#ifndef HELLOWORLD
#define HELLOWORLD

void helloWorld();

#endif 


helloWorld.cpp
1
2
3
4
5
6
7
#include "helloWorld.h"
#include <iostream>

void helloWorld()
{
   std::cout << "Hello World!" << std::endl;
}


main.cpp:
1
2
3
4
5
6
7
#include "helloWorld.h"

int main()
{
   helloWorld();
   return 0;
}


In this example, the interface to the helloWorld function is found in the header file helloWorld.h. The helloWorld.cpp file implements the function. The main program only needs to see the interface to be able to compile.

Both .cpp files are compiled into object files (most likely .o or .obj depending on your system) independently, and neither depends on the other--each .cpp file depends only on helloWorld.h (helloWorld.cpp also depends on iostream). After both object files are compiled, the linker links them together to create a fully functioning program.

Notice that the helloWorld.cpp file only needs to see the iostream header (interface) file, not the entire implementation of iostreams. The linker will pull in the required libraries to provide the iostream capabilities.

Is this the type of explanation you are looking for?
I was in France asking what to put on my feet, and they said "chaussures", but when I looked at one of these "chaussures" it was the same thing as a shoe!
It's just a matter of organization.
Also, having different interfaces can speed up compiling the program: files that are not changed are not compiled again.

@doug4

That was helpful, but I am more confused on the implementation with classes.

Do you mind creating an examle (with 3 files like above) but with an interface class, the class itself, and then how it would all come together in the main.

Thanks so much! :D
How about this?

HelloWorld.h:
1
2
3
4
5
6
7
8
9
10
#ifndef HELLOWORLD
#define HELLOWORLD

class HelloWorld
{
public:
    void printGreeting();
};

#endif  



HelloWorld.cpp
1
2
3
4
5
6
7
#include "HelloWorld.h"
#include <iostream>

void HelloWorld::printGreeting()
{
   std::cout << "Hello World!" << std::endl;
}



main.cpp:
1
2
3
4
5
6
7
8
#include "HelloWorld.h"

int main()
{
   HelloWorld hw;
   hw.printGreeting();
   return 0;
}
That was perfect! Seriously thank you so much!

Okay so in HelloWorld.cpp, could I have done this as well?

1
2
3
4
5
6
7
8
9
10
11
#include "HelloWorld.h"
#include <iostream>

class HelloWorld 
{
   public:
   void printGreeting()
   {
      std::cout << "Hello World!" << std::endl;
   }
}


Also, I've seen some people (specifically this site: http://www.functionx.com/cppcli/classes2/Lesson21e.htm) use the keyword "Interface" when defining an interface. Do we use that keyword? If so, when?

Thank you!
Okay so in HelloWorld.cpp, could I have done this as well?

No, that woiuld be double-defining. To do so, HelloWorld.h should contain just class HelloWorld;
Do we use that keyword?

It's not a keyword here, and no.
Thank you!

Your wellcome ;)
Thanks!

So no interface or abstract class keywords.
Just classes being defined and the way you use them imply if it's an interface or abstract class.

Sounds good!
There are no "interface" or "abstract class" keywords in C++.

A class is abstract if one of more of its methods is pure virtual (undefined in the base class). Like so:

1
2
3
4
class Abstract
{
    virtual void func(int a) = 0;
};


The function "func" must be defined in a derived class.

You can create an "interface" base class similar to Java (I believe - my Java was never great and is a bit rusty now) by merely by creating a class with only pure virtual functions in it. The above class Abstract is essentially an interface class.

C++ allows for multiple inheritance, so a class can inherit from one or more "non-interface" classes as well as one or more "interface" classes. Because of multiple inheritance, C++ does not distinguish between "interface" classes and "non-interface" classes.
Thanks @doug4, I feel I have a much better understanding :)
I'm glad you better understand this.

One thing that @viliml said is technically correct for the file he was discussing, but the answer would be incorrect overall. Let me to clarify.

You asked:
Okay so in HelloWorld.cpp, could I have done this as well?

1
2
3
4
5
6
7
8
9
10
11
#include "HelloWorld.h"
#include <iostream>

class HelloWorld 
{
   public:
   void printGreeting()
   {
      std::cout << "Hello World!" << std::endl;
   }
}



@viliml responded:
No, that woiuld be double-defining. To do so, HelloWorld.h should contain just class HelloWorld;


That is an instance of forward declaration. This tells the compiler that there is a class called HelloWorld that will be defined later. The compiler then knows about it and can create pointers and references to object of that type because it doesn't need to know how much memory the class takes up, or what it can do.

This will work just fine for compiling your version of HelloWorld.cpp (although trivially so). However, you would not be able to compile main.cpp. The main function needs to create an object of type HelloWorld (line 5 in my code example). Because the compiler only has a forward declaration available in the header file, it will not know how to allocate memory for hw, nor whether hw.printGreeting() is legal in line 6.

If this is confusing, go ahead and ignore it. If you understand the concept behind what to put in .h and .cpp files, you should be fine.
Last edited on
I understood most of what you said other than:

The main function needs to create an object of type HelloWorld


Doesn't line 5 create an object of HelloWorld?
HelloWorld hw;

Exactly. That's what I meant.

So if you made the change to HelloWorld.h that @viliml suggested:

1
2
3
4
5
6
#ifndef HELLOWORLD
#define HELLOWORLD

class HelloWorld;

#endif   


and moved the declaration and implementation of the class into HelloWorld.cpp as you suggested, main.cpp would not compile. When trying to create hw in line 5, the compiler would not know what to create, because the included file (HelloWorld.h) would only contain a forward reference.

This is a subtle point and hinges on the recognition of the forward declaration that @viliml mentioned. If it's the only thing you don't understand, you can move on. You'll eventually understand it as you get more experience.
Gotcha!

Thanks a lot doug :)
Topic archived. No new replies allowed.