Updated March 28, 2023
Introduction to C++ Struct Constructor
A structure called Struct allows us to create a group of variables consisting of mixed data types into a single unit. In the same way, a constructor is a special method, which is automatically called when an object is declared for the class, in an object-oriented programming language.
So, combining these two different methodologies, we can say that when these constructors are defined inside a Struct, then these would be referred to as Struct constructors. Let us learn about this functionality in the C++ programming language.
Syntax:
The general syntax for Struct constructor can be defined below:
Struct Struct_Name
{
datatype_1 variable_1;
datatype_2 variable_2;
datatype_3 variable_3;
datatype_4 variable_4;
……..
……..
Struct_Name()
{
//inside default constructor
}
Struct_Name(data_type1 variable_name1, data_type2 variable_name2)
{
//inside parameterized constructor
}
…
………
…………
};
As per the above syntax, a struct can be defined using the struct keyword, followed by the user-defined structure name. As we can notice, the declaration is struct is similar to the class declaration.
After defining the struct, it is about the declaration of all the variables with different data types. Then we had defined the constructor. It is defined as a function with the name the same as the Struct name. In the syntax, we had shown declaration of both default and parameterized constructor.
How does Struct Constructor work in C++?
Here, let us check out the sample code and understand its working in the C++ programming language.
Code:
#include <iostream>
using namespace std;
struct rect
{
float a; //for height
int b; //for width
rect()
{
a=10.58;
b=4;
cout<<"Area at default constructor is: "<<a*b<<endl;
}
};
int main()
{
rect r1;
}
Output:
Above we have used the constructor functionality with the Struct functionality. We can observe that we have defined a Struct with two variables for calculating the area of a rectangle. And then we have defined a default constructor and initialized values for the variables defined earlier.
We have even calculated the area in the defined constructor and then ended up the Struct functionality. In the main method, we had just created the Struct variable. In the output, we can observe that without explicit calling, the area that we are calculating under the default struct constructor has been successfully printed. In this way, the constructor concept works in Struct.
Examples of C++ Struct Constructor
Here, let us check out different examples for the struct constructors:
Example #1
We will define our first example as an extension to that which is given above. Below, we will define a parameterized constructor and check how it works,
Code:
#include <iostream>
using namespace std;
struct rect
{
float a; //for height
int b; //for width
rect()
{
a=10.58;
b=4;
cout<<"Area at default constructor is: "<<a*b<<endl;
}
rect( float x, int y)
{
cout<<"Area is: "<<x*y;
}
};
int main()
{
rect r1;
float x;
int y;
cout<<"Enter height value: "<<endl;
cin>>x;
cout<<"Enter width value: "<<endl;
cin>>y;
rect(x,y);
}
Output:
Here, we had defined both the default and parameterized constructors under Struct. We need to observe the calling functionality for a parameterized constructor by giving it as user input. We had taken two user input values, stored them in variables and call the constructor. Respectively the area calculation is being done.
Example #2
Let us see an example for the parameterized struct constructor without having user input values:
Code:
#include <iostream>
using namespace std;
struct rect
{
float a; //for height
int b; //for width
rect()
{
a=10.58;
b=4;
cout<<"Area at default constructor is: "<<a*b<<endl;
}
rect( float x, int y)
{
cout<<"Area is: "<<x*y;
}
};
int main()
{
rect r1;
float x=7.8;
int y=3;
rect(x,y);
}
Output:
Here we are not providing any user input values but just gave the hardcoded values in the main method itself and called the parameterized constructor.
The two written codes above are examples of the Struct Constructor overloading concept.
Example #3
Let have a look at how we can have a constructor as well as a method defined under struct.
Code:
#include <iostream>
using namespace std;
struct rect
{
float a; //for height
int b; //for width
rect()
{
a=10.58;
b=4;
area(a,b);
}
rect( float x, int y)
{
area(x,y);
}
int area(float x, int y)
{
cout<<"Area is: "<<x*y<<endl;
}
};
int main()
{
rect r1;
float x;
int y;
cout<<"Enter height value: "<<endl;
cin>>x;
cout<<"Enter width value: "<<endl;
cin>>y;
rect(x,y);
}
Output:
In this example, instead of calculating the area inside the constructors, we have declared a new method inside the Struct itself for calculating the area. Inside that method, we are displaying the result. Through the Struct constructors, we have called the method and got the required output.
Example #4
Here, we are looking into an example of having two parameterized constructors with same number of parameters but different data types.
Code:
#include <iostream>
using namespace std;
struct rect
{
float a; //for height
int b; //for width
rect()
{
a=10.58;
b=4;
area(a,b);
}
rect( float x, int y)
{
area(x,y);
}
rect( float x, float z)
{
area(x,z);
}
int area(float x, int y)
{
cout<<"Area is: "<<x*y<<endl;
}
int area(float x, float z)
{
cout<<"Area is: "<<x*z<<endl;
}
};
int main()
{
rect r1;
float x;
int y;
float z;
cout<<"Enter height value: "<<endl;
cin>>x;
cout<<"Enter width value in integer data type: "<<endl;
cin>>y;
cout<<"Enter width value in float data type: "<<endl;
cin>>z;
rect(x,y);
rect(x,z);
}
Output:
For this example, we had to write code for the area method too. As the method which we had before have the parameters similar to the constructor. So for finding area different parameter constructor, we need to have the method for the same too. If we do not declare and use those different data type variables properly, then our output for the area results to be zero.
Conclusion
Here, we have got on how to declare and initialize struct constructors in C++ programming language. Please notice that we declare struct variables under main, those are not called objects. We have learned to use different ways of using struct constructors, so keep practicing ad enjoy learning.
Recommended Articles
This is a guide to C++ Struct Constructor. Here we discuss the introduction and how struct constructor works in C++ along with different examples and its code implementation. You may also have a look at the following articles to learn more –