STRUCTURE IN C++
📚 Topics Covered
- What is a Structure?
- Member functions in structure
WHAT IS A STRUCTURE?
DEFINITION:
A structure is a user-defined datatype which is used to store multiple variables under a single name.
To create a structure we use struct keyword for creating it.

In the above image, the representation of structure is displayed in form of visuals.
SYNTAX:
struct structureName
{
datatype variable1;
datatype variable2;
datatype variable3;
.
.
};
EXAMPLE:
struct Person
{
char fullName[50];
int age;
int salary;
};
In the above example, Person structure is created which consists of multiple members (variables) of different datatype.
To access the members of the structure first we need to create the structure variable to access.
HOW TO ACCESS THE MEMBERS OF THE STRUCTURE?
To access the members of the function we use member operator ( . ) along with structure variables to access the members of structure.
structure_variable.membername = value;
EXAMPLE:
#include<iostream>
using namespace std;
struct Person
{
char fullName[50];
int age;
int salary;
};
int main()
{
Person p1;
cout<<"Enter full name: ";
cin>>p1.fullName;
cout<<"Enter age: ";
cin>>p1.age;
cout<<"Enter salary: ";
cin>>p1.salary;
cout<<endl<<"PERSON DETAILS:"<<endl<<endl;
cout<<"Name: "<<p1.fullName<<endl;
cout<<"Age: "<<p1.age<<endl;
cout<<"Salary: "<<p1.salary<<endl;
return 0;
}
In the above program, we have created p1 structure variable which stores fullname, age, salary for p1 using member operator.
Enter full name: Intelle
Enter age: 21
Enter salary: 50000
PERSON DETAILS:
Name: Intelle
Age: 21
Salary: 50000
We can create as many structure variables as we want for the structure & for each structure variable different memory is allocated for each.
MEMBER FUNCTIONS IN STRUCTURE
DEFINITION:
In C++, structures can also have member functions. Function defined inside the structure are known as member function. We can create a function inside the structure.

In the above image, the representation of structure is displayed in form of visuals.
EXAMPLE:
#include<iostream>
using namespace std;
struct Person
{
char fullName[50];
int age;
int salary;
void display()
{
cout<<"Full Name: "<<fullName<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Salary: "<<salary<<endl;
}
};
int main()
{
Person p1;
cout<<"Enter full name: ";
cin>>p1.fullName;
cout<<"Enter age: ";
cin>>p1.age;
cout<<"Enter salary: ";
cin>>p1.salary;
p1.display();
return 0;
}
In the above program, we have created display() which displays fullname, age, salary for p1 using member operator.
OUTPUT:
Enter full name: Intelle
Enter age: 21
Enter salary: 50000
Full Name: Intelle
Age: 21
Salary: 50000
We can create as many member functions as we want for the structure.
Video Explanation:
If video not loading,watch on YouTube
Practice Task
Write a program to take full the details of a student (Roll no, Student name, Class, Division) and display the details on the screen.
🚀 Challenge
Try to create your own mini project so that you will understand what you have truly learnt about C++.
Solve in Compiler