I wanted to learn about header files in C++ so I implemented simple arithmetic and wanted to know if this is the proper way to do this. Thanks!
my_math.h
#pragma once
namespace math {
/**
* returns the sum of numbers a and b
* @param a the first number
* @param b the second number
* @return sum a + b
*/
double sum(double a, double b);
/**
* returns the difference of numbers a and b
* @param a the first number
* @param b the second number
* @return difference a - b
*/
double difference(double a, double b);
/**
* returns the product of numbers a and b
* @param a the first number
* @param b the second number
* @return product a * b
*/
double product(double a, double b);
/**
* returns the dividen of numbers a and b
* @param a the first number
* @param b the second number
* @return dividen a / b
*/
double divide(double a, double b);
}
my_math.cpp
#include "my_math.h"
namespace math {
double sum(double a, double b) { return a + b; }
double difference(double a, double b) { return a - b; }
double product(double a, double b) { return a * b; }
double divide(double a, double b) { return a / b; }
}
main.cpp
#include <iostream>
#include "my_math.h"
using namespace std;
using namespace math;
int main() {
cout << sum(4, 6) << endl;
cout << difference(4, 6) << endl;
cout << product(4, 6) << endl;
cout << divide(24, 6) << endl;
}
Should I be putting
namespace math {
in both the source file and header file? Also is it good convention to have functions without classes implemented in header files?