How to create a project with Classes and Objects in C++: Headers and Implementations

In this tutorial from our C++ e-book, we will learn how to organize our classes and function implementations, creating a very professional project.


How to create a C++ project

Open your Code :: Blocks (or any other IDE you are using).
Click File, then New and choose Project.
Choose Console application and check the C ++ option.
Give your project a name, choose the folder you want to save and click Finish,

It will look like this:


How to create a C++ project
Note that the file 'main.cpp' already came, we will use it.
Now, click on that white file up there, saying 'New file', and click on Class. Name this class as 'Calc' as we are going to create a calculator.

Now see how cool:

He created two files: Calc.h and Calc.cpp

Implementing a Class in a C++ Project: .h

Okay, come on, first let's work on the Calc.h file, it's called a header.
Let's go to the outline of our class.

It will have two numbers: num1 and num2, obviously private.
It will have functions of sum, subtraction (sub), multiplication (prod) and division (div), all public and const (they will not be able to change the variables).

Only the setNum1 and setNum2 functions will be able to change the variables.
And ready, our Calc.h class header looks like this:
#ifndef CALC_H
#define CALC_H


class Calc
{
    private:
        double num1, num2;

    public
        double sum() const;
        double sub() const;
        double prod() const;
        double div() const;
        void setNum1(double);
        void setNum2(double);
};

#endif // CALC_H
See that there are some #ifndef, #define, #endif ... do not touch them, they are super important, leave them there and then we explain their meanings.

Implementing Functions of a Project Class: .cpp

It is in this file that we will implement the functions of our Calc.h class

Note that at the very beginning, there is a "#include Calc.h", so this .cpp file is directly related to the .h file, do you understand the magic of it?

Note that there is no 'cout', if it did, we would have to include 'include <iostream>:

In this file, we will implement all the getters and setters functions that were previously presented in the class header. The .cpp file looks like this:
#include "Calc.h"

double Calc::getSum() const
{
    return num1+num2;
}

double Calc::getSub() const
{
    return num1-num2;
}
double Calc::getProd() const
{
    return num1*num2;
}
double Calc::getDiv() const
{
    return num1/num2;
}
void Calc::setNum1(double n1)
{
    num1=n1;
}
void Calc::setNum2(double n2)
{
    num2=n2;
}

Running our C++ project: main.cpp

Now, in main.cpp, the only thing you have to do is to include :#include "Calc.h" up there in the file, and that's it, see how the main went:

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

using namespace std;

int main()
{
    Calc c;
    double num;

    cout << "First number: ";
    cin >> num;
    c.setNum1(num);

    cout << "Second number: ";
    cin >> num;
    c.setNum2(num);

    cout << "\nResults: "<<endl;
    cout << "Sum: " << c.getSum()<<endl;
    cout << "Substraction: " << c.getSub()<<endl;
    cout << "Product: " << c.getProd()<<endl;
    cout << "Division: " << c.getDiv()<<endl;

}
Now imagine for a moment if we had declared this class and the implementations of the functions all in main.cpp, the mess it would be and the gigantic code.

This way we did, separating by headers (.h) and function implementations (.cpp), everything was more cute, organized and professional, that's how they do it and use it in professional software.

Get used to doing your projects with this organization, okay?

No comments:

Post a Comment