Showing posts with label Object Orientation. Show all posts
Showing posts with label Object Orientation. Show all posts

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?

How to create a Class and Objects in C++

Now that we have a good idea of what the Object-Orientada programming paradigm is, as well as the Class and Object concepts (in a more abstract way), let's get our hands dirty and make code! Create such classes and real objects.

How to create a Class in C ++

Initially, the concept of class encoding is very reminiscent of structs. In this case, we will use the keyword class to create a Class, as follows:

#include <iostream>
using namespace std;

class MyClass
{
    //Informations
    //about your
    //Class
};

int main()
{
    return 0;
}

That is, we write class, choose a name for our class, open keys and put all the information about it inside. Don't forget to put a semicolon at the end of the class scope!

Ready, the MyClass class was created! Also note that it is not within the main() function, but at the same level as it.

Defining members of a Class: Variables and Functions

Shall we create a real class? We will create a class that will create squares. Our class is called Square.

What is an important data of a square? Hey, its side. Soon, it will have a variable that stores its side.

What other important 'thing' in a square? Its area. Let's create a function that calculates the area of that square. Our class looks like this:

#include <iostream>
using namespace std;

class Square
{
    double side;
    double area();
};

double Square::area()
{
    return side*side;
}

int main()
{
    return 0;
}

There, there is the declaration of the variable 'side' as well as the header of the area() function. And this function, where are we going to declare? It was declared after the scope of the class.

So far, in our course, if we wanted to declare an area() function in our programs, we would do:

double area()
{
     return side * side;
}

And this function is visible and accessible to our entire program, correct?

What if we want to create a function that is a Class and should be visible and accessible only by that class?
We do:

double Square::area()
{
    return side * side;
}

Understood? Put "ClassName::" before the function name, that says that function is of the ClassName class, and only it has access and can use that function!

How to create and instantiate objects in C++

Ok, our class is cute and done! Now let's create objects from it!
If you want to create information of the entire type, you do:

int num;
int age;
int ID;

If you want to create string information, you do:
string name;
string address;

And so on. If you want to create information like 'MyClass', you do:

MyClass example;

For example, to create an object of class Square, you would do:

Square q1;
Square square;

Let's call our object 'q', we create it like this:

#include <iostream>
using namespace std;

class Square
{
    double side;
    double area();
};

double Square::area()
{
    return side*side;
}

int main()
{
    Square q;

    return 0;
}

Ready. Now there is a real square, called Square, which has 'side' and 'area()' members. Nice, right?

When we create an object, we say that we are instantiating an object q of class Square. It is a new 'instance' of the class.

You can even create another object, or millions, just give them different names: q1, q2, ..., q2112 ... each one will have its characteristics: its 'side' values and its areas. Although they are of the same Square 'type' (that is, they all have side and area() in common), each has its specific values.

It's just like us, people ... everyone has a name, heart, lung capacity, age ... but each has its own specific values (specific name, specific age, specific physical condition). Now you understand better the concept of Class and Object, in C++?

In the next tutorial we will learn how to access, change and use these members of the Square class.