This guide will describe about:
- What is a “struct” Constructor in C++?
- Key Points for Defining Constructor in C++
- Syntax of “struct” Constructor in C++
- Working of “struct” Constructor in C++
- Default “struct” Constructor in C++
- Parameterized “struct” Constructor in C++
- How to Create Pointers for Structure Constructor in C++?
- Bottom Line
What is a “struct” Constructor in C++?
In C++, the “struct” is known as a structure that is a special function member within a struct. The “struct” constructor is used to initialize its member variables and enable users to make a new group of variables consisting of mixed data types in a single place. In simple words, the “struct” constructor is a particular method that is automatically invoked whenever users declare an object for the class.
Key Points for Defining Constructor in C++
Here are some key points for defining a constructor in C++:
- Constructor always has the same name as that class of which it is part.
- If the constructor is not provided by users, then it will generate a default constructor.
- Constructors are used for initialization rather than for input/output operations.
- Utilized for locating memory at run time with the help of the operator.
- It can not be declared virtual and static.
- Users can declare more than one constructor (it can be overloaded).
Syntax of “struct” Constructor in C++
Let’s check out the general syntax of the struct constructors:
d_type variable1;
d_type variable2;
........
........
//without parameters constructor
Structure-Name()
{
//code
}
//with parameters constructor
Structure-Name (d_type variable1, d_type variable2,..)
{
//code
}
};
According to the above-described syntax:
- “Structure-Name” will be replaced with the user’s preferred structure name and will define it with the “struct” keyword.
- “d_type variable1” represents the variable name with different data types.
- Then, the “Structure-Name()” constructor is defined without a parameter.
- Next, the syntax of the parameterized constructor has also been defined, such as “Structure-Name(d_type variable1,…)” represents the parameterized constructor.
Note: The “struct” declaration is the same as the declaration of class in C++.
Working of “struct” Constructor in C++
In C++, users can use the struct constructor without or with parameters according to their requirements. Let’s check out the following code demonstration for better understanding.
Create Default “struct” Constructor in C++
To create a default struct constructor in C++, check out the following code block:
using namespace std;
struct folk{
string name;
float height;
int age;
folk(){
name= "David";
height = 6.7;
age = 26;
cout<<"Name: "<<name<<endl<<"Height: "<<height<<endl<<"Age: "<<age<<endl;
}
};
int main()
{
folk();
}
In the above-given code block:
- At first, we added the “# include <iostream>” input/output stream library as the header file.
- Next, used the “using namespace std” to enable the usage of standard library identifiers.
- Afterward, we created the “folk()” structure having three members with different data types, such as “name” as string, “height” as float, and “age” as an integer type respectively.
- Then, added the constructor for the struct and initialized the members with respective values. Inside the constructor body, we defined the data of the folk structure and used the “cout” statement to show the result on the console.
Output
Create Parameterized “struct” Constructor in C++
In order to create the parameterized constructor in C++, try out the below-given code:
using namespace std;
struct folk{
string name;
float height;
int age;
folk(string x, float y, int z){
name = x;
height = y;
age = z;
}
};
int main()
{
folk f1("Ayzel", 5.1, 23);
cout<<"Name: " <<f1.name<<endl<<"Height: "<<f1.height<<endl<<"Age: "<<f1.age<<endl;
}
Here:
- We extended the previously discussed example and passed the variables along with their respective data types as parameters to the constructor, such as “x” as a string, “y” as a float, and “z” as an integer.
- Lastly, inside the “main()” function we have created the object named “f1” and invoked the constructor with values. Then, utilized the “cout” statement to print the resultant values of the variables on the console.
Output
How to Create Pointers for Structure Constructor in C++?
A pointer is a variable that is utilized for storing an object’s memory address. In C++, pointers are also used for iterating over an array’s elements or other data structures and many more purposes. Pointer variables are also created for user-defined types like structures. Let’s have a look at the provided example for creating a pointer for structures:
#include <cstring>
using namespace std;
struct folk
{
string name;
int age;
};
int main(){
struct folk f = {"Elice ", 28};
struct folk *ptr;
ptr = &f;
cout << f.name << f.age << endl;
cout <name <age << endl;
return 0;
}
In above-provided code:
- We have added the “<iostream>” and “<cstring>” header files.
- Then, specified the “using namespace std” to enable the usage of standard library identifiers.
- Next, we created the “folk()” structure having two members with different data types, such as “name” as a string, and “age” as an integer type respectively.
- Afterward, we defined a pointer “ptr” to the “folk” structure.
- Now, we created a pointer “ptr” for pointing to the “f” structure variable. Consequently, the “ptr” now saves the address of the structure variable “f”.
- Lastly, we used the “->” operator to access the data members of structure members of a structure utilizing a pointer to that structure.
Here is the output of the above-described code:
Bottom Line
In C++, the “struct” is known as a structure that is a special member function within a struct that is used to initialize its member variables and enable users to make a new group of variables consisting of mixed data types in a single place. Users can define with or without parameter constructors. In C++, pointer variables are also created for user-defined types like structures. We have demonstrated detailed information about struct constructors in C++.