0% found this document useful (0 votes)
12 views

Define and Declare Structure

The document discusses how to define and declare structures in C++. A structure allows grouping related data under a single name, like collecting person details such as name, age, and salary. To declare a structure, use the struct keyword followed by the structure name and members within curly braces. An example demonstrates defining a Person structure and assigning values to its members.

Uploaded by

syu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Define and Declare Structure

The document discusses how to define and declare structures in C++. A structure allows grouping related data under a single name, like collecting person details such as name, age, and salary. To declare a structure, use the struct keyword followed by the structure name and members within curly braces. An example demonstrates defining a Person structure and assigning values to its members.

Uploaded by

syu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Define and Declare Structure

What is Structure?

Structure is a collection of variables of different data types under a single name. It is similar to
a class in that, both holds a collecion of data of different data types.

For example: You want to store some information about a person: his/her name, citizenship number
and salary. You can easily create different variables name, citNo, salary to store these information

separately.

However, in the future, you would want to store information about multiple persons. Now, you'd
need to create different variables for each information per person: name1, citNo1, salary1, name2,

citNo2, salary2

You can easily visualize how big and messy the code would look. Also, since no relation between the

variables (information) would exist, it's going to be a daunting task.

A better approach will be to have a collection of all related information under a single name Person,

and use it for every person. Now, the code looks much cleaner, readable and efficient as well.
This collection of all related information under a single name Person is a structure.

How to declare a structure in C++ programming?

- The struct keyword defines a structure type followed by an identifier (name of the
structure).
- Then inside the curly braces, you can declare one or more members (declare variables inside
curly braces) of that structure.

For EXAMPLE

struct Person Structure Name


{
char name[50];
int age; Structure Members
float salary;
};

Note: Remember to end the declaration with a semicolon (;)


Example: C++ Structure

C++ Program to assign data to members of a structure variable and display it.

#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;
}
Output
Enter Full name: Magdalena Dankova

Enter age: 27

Enter salary: 1024.4

Displaying Information.

Name: Magdalena Dankova

Age: 27

Salary: 1024.4

You might also like