Constructor in Structure in C

Constructor in Structure in C programming to combine different data types to store a particular type of record. It helps to construct a complex data type in a more meaningful way.

Structure Program using Constructor and Passing String Argument

Program

#include<stdio.h>
#include<conio.h>
#include<string> //including Library for Working with String.
struct str //'str' is name of Structure.
{
str() //this is default constructor.
{
printf("This is Default Constructor\n");
}
str(string p) //this is parameterized constructor and passing
{            //here argument.
printf("This is %s Constructor\n", p);
}
str(int roll, char sec) //this is parameterized constructor and
{                       //passing here arguments.
printf("Roll No is: %d\nSection is: %c", roll, sec);
}
};
void main()
{   //'obj', 'obj2' and 'obj3' is objects of Structure 'str'.
str obj; //calling default constructor.
str obj2("Parameterized"); //calling and passing argument to parameter.
str obj3(120122067, 'B'); //calling and passing arguments to parameters.
getch();
}
Output
This is Default Constructor
This is Parameterized Constructor
Roll No is: 120122067
Section is: B

Program Calling Two Structure Functions

Program

#include<stdio.h>
#include<conio.h>
struct str //'str' is name of Structure.
{
str()
{
printf("This is Default Constructor of Structure str\n");
}
void fun()
{
printf("This is Simple Function of Structure str\n");
}
};
struct str2 //'str2' is name of Structure.
{
str2()
{
printf("This is Default Constructor of Structure str2\n");
}
void fun()
{
printf("This is Simple Function of Structure str2");
}
};
void main()
{
str obj; //'obj' is object of Structure 'str'.
str2 obj2; //'obj2' is object of Structure 'str2'.
obj.fun();
obj2.fun();
getch();
}
Output
This is Default Constructor of Structure str
This is Default Constructor of Structure str2
This is Simple Function of Structure str
This is Simple Function of Structure str2