C++ class constructors

Constructors are like functions. These are used to initialize the values and the objects of the class. These constructors are initiated when the object of a class is created. Constructor directly does not return any value. To get the value of the constructor, we need to describe a separate function as the constructor doesn’t have any return type. Constructor differs from the simple function in different ways. A constructor is created when the object is generated. It is defined in the public segment of the class.

In this article, we will deliberate on all these types of constructors with examples.

Example 1

This is an illustration of a default constructor. As we know that the constructors are default created automatically when we create an object of a class. This is called implicit creation. Constructors are of the same name that is the name of the class. Consider a file having the c++ code of the constructor as we know that the class has two options, private and public. The private part contains the data variables, whereas the public part is for the functions retrieved by any object. So the constructor is also defined in the public part.

Integer()

{

x=50;

y=20;

};

In this constructor, values are assigned to the variables. If we want to fetch the values as an output, we have to print them in the main program.

After defining the constructor, the class is closed. While entering the main program, we will take print of the values by using an object. The object always accesses constructors as these are the parts of the class. Object creation is so simple. It is done by introducing it with the name of the class. That is an integer in this example. The value will be fetched through the “dot” method. i.e., “a.x.”

We can see the output of the source code from the terminal in Ubuntu. The approach used for getting the output is quite easy. Firstly the code is compiled, and then it is executed. We use the G++ compiler for the compilation process. Just like in the case of C, we use GCC.

$ G++ -o filec filec.c

. /filec

-O is used to save the output in the file.

Example 2

In this illustration, we are going to explain the parameterized constructors. Unlike the previous example, we can also pass the arguments to the constructors from the main program. When the object is generated, these values are automatically passed to the variables present in the constructor to receive the value. Some of the uses of parameterized constructors are.

  • It is used to initialize different variables with different values inside the constructors when it is initialized.
  • It is used in constructor overloading. It is defined later in the article.

Now let us consider the illustration we have described to elaborate this concept. The class has the name integer, so definitely, the constructor’s name will also be the same. In the parameters of the constructor, there are two integer type values. These are initialized to accept the values that are sent from the main program as a function call.

Integer( int x, int y)

{

A=x;

B=y;

};

In the previous example, the variables inside the constructor were given the values. Whereas in this constructor, variables are assigned with the variables having the value.

If we want to take display, we need to define a function that will return the value as it is not possible to access that initialized variable directly from the constructor.

int getX()

  {

    return a;

  };

Now we will see the main part of the program. Here when the object is created, you can see the values in the parameter section.

Integer v(70,55);         {implicit}

integer v=integer(10,15);      {explicit}

And to display the result, we will call the functions created inside the class by using the object. i.e. v.getx().

The method of record fetching is the same as introduced before.

Example 3

This example deals with the copying of a constructor of a class. A copied constructor is used to initialize the object with another object of a similar class it belongs to. This constructor copies the data present in one object to the other. The parameters of this constructor contain the address of an object of the class. Consider the given examples, in which we have introduced two variables of the same data types so that these can access by any function inside the class. The constructor will receive the values through the variable. At the same time, the copied constructor will receive only the object. And with the help of this object, the values will be fetched.

Wall(wall &obj)

{

Length = obj.length;

Height= obj.height;

}

We have to calculate area, so the function for this calculation is defined here. This will return the value to the main function when it is called. Now we will observe the main program of the code

The function call of the copied constructor will be like this.

Wall wall2 = wall1;

The object calls the copied constructor, and the data through the first object is copied through it. Furthermore, we will call the function to calculate area through both the objects.

From the output, you can view that the result from both the constructors is the same. It means the whole data was copied by the object successfully.

Example 4

This is an illustration of constructor overloading. It happens when we have to use more than a single function inside the class. Constructor overloading follows the instructions of parameterized constructors. All the constructors in the class have a similar name as the class. But each of the constructors is assigned different parameters. Each constructor is called according to the argument when we create the object.

Consider the given an example, in which we have used three constructors. One is without any argument. The second one is with a single argument, whereas the third one is with two arguments. This illustration is similar to the preceding one. As we calculate area in the separate function described inside the class.

// Constructor with two arguments

  shape(int x, int y)

  {

    a= x;

    b= y;

  };

Now, moving towards the main program, we can see that when we initiate the class object, the constructor with no argument is called by default. Now we need to call other constructors with different objects having different arguments.

Shape s;

Shape s2(8);

Shape s3(4,2);

The function through which we can display the value is called through the same object created.

To view the output, we will utilize the same command terminal method by compiling and executing the code present in the file.

From the output, we can view that the answer is the same for each constructor.

Conclusion

In this tutorial, we have seen the basics of constructors and their functionalities, including how to overload them. Constructors are utilized to initializing the variables with the values.



from Linux Hint https://ift.tt/3y3M64D

Post a Comment

0 Comments