0

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?

  • 2
    Yes and Yes. Only problem here is if you run across one of the rare compilers that doesn't support `#pragma once` or have a sufficiently complicated build environment that makes `#pragma once` fail. Neither of which are likely issues. Reading on the problem: [Is #pragma once a safe include guard?](https://stackoverflow.com/questions/787533/is-pragma-once-a-safe-include-guard) and [#pragma once vs include guards?](https://stackoverflow.com/questions/1143936/pragma-once-vs-include-guards) – user4581301 Jul 13 '19 at 05:58
  • Look good to me, may be you can look at that : Why is “using namespace std;” considered bad practice? https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice/1452759 – Martin Morterol Jul 13 '19 at 05:59
  • @young.jos for some questions like if you should put `namespace math` in the source file, it is good to see what happens if you don't. This might help you to recognize this error message in the future if it happens by accident. – tos-1 Jul 13 '19 at 08:30

1 Answers1

3

Should I be putting

namespace math { 

in both the source file and header file?

When you implement one of the functions that are declared in my_math.h, you'll have to indicate to the compiler that the function is in math namespace. There are two ways of doing that:

Method 1

As you have done.

Method 2

Use math:: scope with each function implementation.

#include "my_math.h"

double math::sum(double a, double b) { return a + b; }
double math::difference(double a, double b) { return a - b; }
double math::product(double a, double b) { return a * b; }
double math::divide(double a, double b) { return a / b; }

The two methods are exactly the same as far as the compiler is concerned. You can use either of the methods. You can even mix the two. There is nothing wrong with that.

Also is it good convention to have functions without classes implemented in header files?

The answser to that question is rather broad. This is not the best place to address it.

R Sahu
  • 196,807
  • 13
  • 136
  • 247
  • One note about the class-less function: you'll probably need to `inline` the suckers or face the wrath of the [One Definition Rule](https://en.cppreference.com/w/cpp/language/definition). – user4581301 Jul 13 '19 at 23:32