Structures in C++
Structures in C++
S IN C++
C++ Language
What is a structure?
A structure is a user-
defined data type in
C/C++. A structure
creates a data type that
can be used to group
items of possibly
different types into a
single type.
How to create a structure?
The ‘struct’ keyword is used to create a structure.
The general syntax to create a structure is as
shown below:
struct structureName{
member1;
member2;
member3;
.
.
.
memberN;
};
Types Of Members in
Structures
Structures in C++ can contain two types of
members:
● Data Member: These members are normal C++
variables. We can create a structure with
variables of different data types in C++.
● Member Functions: These members are normal
C++ functions. Along with variables, we can
also include functions inside a structure
declaration.
Example
// Data Members
int roll;
int age;
int marks;
// Member Functions
void printDetails()
{
cout<<"Roll = "<<roll<<"\n";
cout<<"Age = "<<age<<"\n";
cout<<"Marks = "<<marks;
}
How to declare structure variables?
// A variable declaration with structure declaration.
struct Point
{
int x, y;
} p1; // The variable p1 is declared with 'Point'
int main()
{
struct Point p1; // The variable p1 is declared like a normal variable
}
Example:
#include <iostream>using namespace std;
struct Person
{
char name[50];
int age;
float salary;
};
int main()
{
Person p1;
cout << "Enter Full name: ";
cin.get(p1.name, 50);
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl;
cout << "Salary: " << p1.salary;
return 0; }
Passing structure to function in C++
A structure variable can be passed to a function in similar way as
normal argument. Consider this example:
#include <iostream>
using namespace std;
struct Person {
char name[50];
int age;
float salary;
};
void displayData(Person); // Function declaration
int main() {
Person p;
cout << "Enter Full name: ";
cin.get(p.name, 50);
cout << "Enter age: ";
cin >> p.age;
Passing structure to function in C++
Example continued:
cout << "Enter salary: ";
cin >> p.salary;
// Function call with structure variable as an argument
displayData(p);
return 0;
}
void displayData(Person p) {
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p.name << endl;
cout <<"Age: " << p.age << endl;
cout << "Salary: " << p.salary;
}
Returning Structure From Function
Example:
#include <iostream>
using namespace std;
struct Person {
char name[50];
int age;
float salary; };
Person getData(Person);
void displayData(Person);
int main() {
Person p, temp;
temp = getData(p);
p = temp;
displayData(p);
return 0;
}
Example Continued:
Person getData(Person p) {
cout << "Enter Full name: ";
cin.get(p.name, 50);
cout << "Enter age: ";
cin >> p.age;
cout << "Enter salary: ";
cin >> p.salary;
return p;
}
void displayData(Person p) {
cout << "\\nDisplaying Information." << endl;
cout << "Name: " << p.name << endl;
cout <<"Age: " << p.age << endl;
cout << "Salary: " << p.salary;
}
Points To Be Noted
Instead, we can simply
use the following code:
We don't really need
to use the temp
p = getData(p);
variable for most
compilers and C++
versions.
Thank You
Coming up next:
Class and its Class
Members and
Methods in C++