I have a question about classes and header files. As I see the trend of reusing classes, I would like to start building the classes in their own file, and be able to call it, and use(#include "class.h") to include them. I am a little bit confused because I understood the part about defining a header file which basically shows how to put the declarations or prototypes into a header file. I also read that you must still compile the classes you wish to use. Do I need a header file and a .cpp file both to use a custom defined class? or must I compile the program and "source" or class file separate and combine them into an executable? I use GNU compiler (g++ command line and notepad++),

Thanks,

Dani

Do I need a header file and a .cpp file both to use a custom defined class?

Depends on the how complex the class is. In the simplest case the class and implementation can both be in the header file. I like to use inline for very short methods. For example you can put all this in a header file.

class Foo
{
public:
   Foo() { _x = 0; } // inline method
   void setX(int x) {_x = x;} // another inline method
   int getX() {return _x;} // another inline function
private:
   int _x;
};

Template classes and their implementation code almost always has to be in a header file, implementation can't be compiled separately in *.cpp file(s) because the compiler doesn't know the data type at that time.

Do I need a header file and a .cpp file both to use a custom defined class?

In addition to AD's remarks, I just want to clarify what you mean by "use". To create a custom class, it is conventional to place the declarations in a header file and the definitions in a cpp file ("definition" means "implementation"). But as AD remarked, you can, in some cases, depart from that convention, notably for short functions (inline) and templates, in which cases, you put the definitions in the header file directly. To use a custom class in some other code, you need to include the header file in order to compile that code, and then, you need to link with the compiled cpp file for the custom class. The compiled cpp file can be either compiled along with your other cpp file(s), compiled separately into an object file (.o or .obj) and then linked with your other cpp file(s), or it can be bundled into a static or dynamic library and then linked with your other cpp file(s).

I highly suggest that you read my comprehensive tutorial on compiling C++ code.

Both AD & Mike2k's answers are good. I just have a small nit to pick with AD regarding proper use of member initializers in a constructor, rather than inside the body of the constructor. IE, instead of his example, I would do this:

    class Foo
    {
    public:
        Foo() : _x(0) {} // inline method
        void setX(int x) {_x = x;} // another inline method
        int getX() {return _x;} // another inline function
    private:
        int _x;
    };

IE, it is always prefereable to have your member variables properly initialized before the constructor is executed. It helps avoid a lot of buggy/invalid code in my experience.

The only time I've seen it make any real difference is when the class object is a reference, in that case you have to initialize it the way you showed.

@AD
True enough, but I do like consistency! I find that I make fewer mistakes that way, and IMO consistency is the root to reliable code. I've spent 30+ years (20+ with C++) writing code for ultra-high reliability systems, both embedded real-time and enterprise, where failure (bugs) can either cause a plane to crash, a factory to trash a gazillion $ worth of product, or introduce a fatal defect in a medical device or drug.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.