Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to divide class in c++ into hpp and cpp files

Tags:

c++

class

I don't know how to divide some class into .hpp and .cpp files. Could you tell me how to divide the following example class?

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

and how to include this class in main function?

like image 312
Qorn Avatar asked Dec 11 '16 14:12

Qorn


People also ask

How do I include a class in a header file?

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 . cpp file of the same name as the class. Now any other header or code file that wants to use the Date class can simply #include "Date. h" .

What is the difference between HPP and cpp?

hpp files contain declarations, while . cpp files contain implementation of what is declared in the . hpp file. You don't want to put implementations in a .


1 Answers

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 assignement 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';
}
like image 152
Stack Danny Avatar answered Oct 03 '22 16:10

Stack Danny