Splitting Classes into .h and .cpp Files
The two files
Header (account.h) — the class declaration (the interface):
#pragma once
#include <string>
class Account {
public:
Account(std::string owner, double balance);
void deposit(double amount);
double balance() const;
private:
std::string owner_;
double balance_;
};
Source (account.cpp) — the member function definitions:
#include "account.h"
Account::Account(std::string owner, double balance) : owner_(std::move(owner)), balance_(balance) {}
void Account::deposit(double amount) { balance_ += amount; }
double Account::balance() const { return balance_; }
Account:: says "this function belongs to Account". Note that const must be repeated in the definition.
Using the class (main.cpp)
#include "account.h"
Building
g++ -std=c++20 -c account.cpp → account.o
g++ -std=c++20 -c main.cpp → main.o
g++ account.o main.o -o app
or in one line: g++ -std=c++20 main.cpp account.cpp -o app
Include guards
Prevent a header from being included twice in one file:
#pragma once // simple, supported by all major compilers ✅
or the classic way:
#ifndef ACCOUNT_H
#define ACCOUNT_H
...
#endif
What goes where
| Header (.h) | Source (.cpp) |
|---|---|
| class definitions | member function bodies |
| function declarations | function definitions |
| inline / template / constexpr functions | non-inline functions |
extern declarations | global variable definitions |
<> vs ""
#include <string>: system/library headers#include "account.h": your project's headers (searched in the current folder first)
Rules
- ❌ Never
using namespace std;in a header - Include what you use; don't rely on other headers including things for you
- Keep headers small: forward-declare (
class Report;) when only pointers/references are needed — faster builds
Why split?
- Changing
account.cpponly recompiles that file - Others can use your class by reading the header only
- Libraries ship headers + compiled code
Example
// ===== file: account.h =====
#pragma once
#include <string>
class Account {
public:
Account(std::string owner, double balance);
void deposit(double amount);
bool withdraw(double amount);
double balance() const;
const std::string& owner() const;
static int count();
private:
std::string owner_;
double balance_;
static inline int count_ = 0;
};
// ===== file: account.cpp =====
#include "account.h"
#include <stdexcept>
Account::Account(std::string owner, double balance) : owner_(std::move(owner)), balance_(balance) {
if (balance_ < 0) throw std::invalid_argument("negative balance");
++count_;
}
void Account::deposit(double amount) {
if (amount > 0) balance_ += amount;
}
bool Account::withdraw(double amount) {
if (amount <= 0 || amount > balance_) return false;
balance_ -= amount;
return true;
}
double Account::balance() const { return balance_; } // const repeated here
const std::string& Account::owner() const { return owner_; }
int Account::count() { return count_; } // no 'static' in the definition
// ===== file: main.cpp =====
#include <iostream>
#include "account.h"
#include "account.h" // included twice on purpose: #pragma once makes this harmless
int main() {
Account a("Asha", 5000);
Account b("Ravi", 800);
a.deposit(1500);
std::cout << a.owner() << ": " << a.balance() << '\n';
std::cout << "Ravi withdraw 1000: " << (b.withdraw(1000) ? "ok" : "declined") << '\n';
std::cout << "Accounts created: " << Account::count() << '\n';
return 0;
}
/* Build:
g++ -std=c++20 -Wall -Wextra main.cpp account.cpp -o app
*/
/* Output:
Asha: 6500
Ravi withdraw 1000: declined
Accounts created: 2
*/