如何将c ++中的类划分为hpp和cpp文件

时间:2016-12-11 14:55:31

标签: c++ class

我不知道如何将某些类划分为.hpp.cpp文件。你能告诉我如何划分以下示例类吗?

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

以及如何在main函数中包含此类?

1 个答案:

答案 0 :(得分:5)

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;
}

这是绝对的基础之一。你应该看一下guide。 同时将变量设为私有,并制作getX() const

等函数

作为参考,以下是实施和声明如何查找具有更多相关功能的类(例如static stuffmove 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);

    //geter
    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;
}

//-----------------------------------------------------------------
//geter
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';
}