11

I don't know how to divide some class into .hpp and .cpp files. How can I divide the following example class?

class Foo {
    public:
    int x;
    int getX() {
        return x;
    }
    Foo(int x) : x(x) {}
};

And how can I include this class in the main function?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Qorn
  • 189
  • 1
  • 1
  • 6
  • _"devide"_ means what again please? Did you mean _separating_ declaration and definition of a class? – πάντα ῥεῖ Dec 11 '16 at 15:01
  • 1
    @Qorn it would help you to understand the compilation process. I did have the same problem before I fully understood how C++ compiles source files. Basicly what you need to know is what files get compiled (answer only .cpp/.c files), how the preprocessor imports other files into these compilation units, and also how the linker resolves dependencies. So maybe more googling on the compilation units will help a lot. take a look at glm math library, there you will see .hpp in use – androidu Dec 11 '16 at 15:04

1 Answers1

23

foo.hpp:

#pragma once

class Foo {
public:
    int x;
    Foo(int x);
    int getX();
};

foo.cpp:

#include "foo.hpp"

Foo::Foo(int x) : x(x) {

}

int Foo::getX() {
    return x;
}

main.cpp:

#include "foo.hpp"

int main() {

    Foo f(10);

    return 0;
}

Keep your variables private and make functions like getX() const.


Possible large class declaration / implementation using more techniques (static, move semantics):

foo.hpp:

#pragma once
#include <iostream> // std::ostream

class Foo {

public:
    // Default constructor
    Foo();

    // Constructor
    Foo(int n);

    // Destructor
    ~Foo();

    // Copy constructor
    Foo(const Foo& other);

    // Move constructor
    Foo(Foo&& other);

    // Copy assignement operator
    Foo& operator=(const Foo& other);

    // Move assignment operator
    Foo& operator=(Foo&& other);

    // Some other operator
    int operator()() const;

    // Setter
    void set_n(int n);

    // Getter
    int get_n() const;

    // '<<' overload for std::cout
    friend std::ostream& operator<<(std::ostream& os, const Foo& f);

    // Static variable
    static int x;

    // Static function
    static void print_x();

private:
    int n;
};

foo.cpp:

#include "foo.hpp"
#include <utility> // std::move

//-----------------------------------------------------------------
// Default constructor
Foo::Foo() {

}

//-----------------------------------------------------------------
// Constructor
Foo::Foo(int n) : n(n) {

}

//-----------------------------------------------------------------
// Destructor
Foo::~Foo() {

}

//-----------------------------------------------------------------
// Copy constructor
Foo::Foo(const Foo& other) {
    *this = other;
}

//-----------------------------------------------------------------
// Move constructor
Foo::Foo(Foo&& other) {
    *this = std::move(other);
}

//-----------------------------------------------------------------
// Copy assignement operator
Foo& Foo::operator=(const Foo& other) {
    this->x = other.x;
    return *this;
}

//-----------------------------------------------------------------
// Move assignement operator
Foo& Foo::operator=(Foo&& other) {
    this->x = std::move(other.x);
    return *this;
}

//-----------------------------------------------------------------
// Some other operator
int Foo::operator()() const {
    return this->x;
}

//-----------------------------------------------------------------
// Setter
void Foo::set_n(int n) {
    this->n = n;
}

//-----------------------------------------------------------------
// Getter
int Foo::get_n() const {
    return this->n;
}

//-----------------------------------------------------------------
// '<<' overload for std::cout
std::ostream& operator<<(std::ostream& os, const Foo& f) {
    return os << f.n;
}

//-----------------------------------------------------------------
// Static variable
int Foo::x = 5;

//-----------------------------------------------------------------
// Static function
void Foo::print_x() {
    std::cout << "Foo::x = " << Foo::x << '\n';
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stack Danny
  • 7,754
  • 2
  • 26
  • 55