Constructors Functions in C++ - Object Oriented

In this tutorial, we will learn about a special type of function, the Constructor function, which plays a very important role in object orientation.

The C++ Constructor function

There is a very important function and used in object oriented, it is the construction function.
And it has some unique and special features.

The first is that the name is the same as the class name. That is, if your class is called "MyClass", the constructor function must be called "MyClass()", mandatorily, ok?

Another characteristic of it is that, as soon as we instantiate an object, the constructor function is always automatically invoked!

Finally, one last characteristic, it does not return any value! Do not write void, int, doublet etc., anything in your header and implementation.

Let's see a test of a working construction function? Let's create the "MyClass" class that has only one member, the "MyClass()" function, which will simply display a message, it looks like this:

#include <iostream>
using namespace std;

class MyClass
{
    public:
        MyClass();
};

MyClass::MyClass()
{
    cout << "Progressive C++ Course"<<endl;
}

int main()
{
    MyClass c;

    return 0;
}

Prontinho! Apenas criamos o objeto 'c' e veja o que apareceu na tela:

Constructor functions



Construction function: What is it for?

Ok, we've already seen how to do it, how it works and the result ... but what is it for? Just to show some silly messages on the screen?
Of course not, young man!

As the name says, it serves to 'construct' things, in this case, it builds things automatically. When we create an object, it is interesting that it already does some internal operations immediately, without having to always do everything manually.

This type of constructor, we call it default, because it doesn't take any arguments. However, the most common and useful way to use a constructor function is with a list of parameters, which are information that comes from the outside into the object, and will play an important role inside.


Constructor function: How to use?

For example, let's look at our Rect class, which created rectangles. Remember that we always had to use the setter functions to define the length and width values. How about if the builder did this automatically?

See how it looks:

#include <iostream>
using namespace std;

class Rect
{
    private:
        double length, width;

    public:
        Rect(double, double);
        double getArea();
        double getPerimeter();
};

Rect::Rect(double l, double w)
{
    length = l;
    width = w;
}
double Rect::getArea()
{
    return length * width;
}
double Rect::getPerimeter()
{
    return 2*(length+width);
}

int main()
{
    double len, wid;

    cout<<"Length: ";
    cin >> len;

    cout<<"Width: ";
    cin >> wid;

    Rect r(len, wid);

    cout<<"Area: " << r.getArea() << endl;
    cout<<"Perimeter: " << r.getPerimeter() << endl;

    return 0;
}

Note that in the header of the constructor, we have already specified that it will receive two variables of type double.

In the implementation, we call these variables 'l' and 'w', which will be provided later by the user. And what does our constructor function do? It will set the correct values for 'length' and 'width'.

In main, we ask for the length and width values, and then simply instantiate an object of type Rect, named 'r'. Notice now that in it we send two arguments, which are the two arguments that the constructor function will use!

Ready, internally, inside the object, the constructor has already done everything and the functions getArea() and getPerimeter() can now be used.

A little more about the Constructor Function

Another curiosity about constructors is that they ALWAYS exist! Even if you don't define any, C++ goes there and creates one, empty, that does nothing.

When you do:

  • MyClass c;

It will automatically invoke the constructor, but you don't see anything happening, as you haven't defined any constructors.

Another trick you may need and use, is through pointers, declaring a pointer to an object:

  • MyClass * ptrClass;

When doing this, the constructor method will not be executed, since an object was not created, only a pointer to an object.

Now if you do this next:

  • ptrClass = new MyClass;

Then, the object was instantiated and allocated in memory, and the constructor will be summarily executed.

No comments:

Post a Comment