Create and Include Files

Create and Include Header Files

Create a header file math_utils.h and a source file math_utils.cpp. Define a function int add(int a, int b); in the header and implement it in the source file. Include the header in a main.cpp file and use the add function in a simple user input calculator

Hint

Make sure to include the header file in math_utils.cpp and main.cpp.

Answer

math_utils.h

#pragma once

int add(int a, int b);

math_utils.cpp

#include "math_utils.h"

int add(int a, int b) {
    return a + b;
}

main.cpp

#include <iostream>
#include "math_utils.h"

int main() {
    int num1, num2;
    std::cout << "Enter the first number: ";
    std::cin >> num1;
    std::cout << "Enter the second number: ";
    std::cin >> num2;

    int result = add(num1, num2);
    std::cout << "The result is: " << result << std::endl;

    return 0;
}
Questions