Home   |   About   |   Terms   |   Contact    
Read & Learn
 

What is Structure Data Type in C++?

C++

☯ All C++ Articles     ☯ All Articles


A new product.






Popular Google Pages:




This article is regarding What is Structure Data Type in C++? Use of Structure Data Type in C++.
Last updated on: .

◕ What is Structure Data Type in C++?

■ Let have an example of Structure Data first:
(This program is successfully tested on C++ compiler.)

#include <iostream>
using namespace std;

struct officeStaff{
char name[20];
int employID;
float weight;
};

int main(){
officeStaff hara = {"Dipak", 123, 56.25};
officeStaff savi = {"Sabitri", 124, 49.5};

cout << "1st name is: " << hara.name << endl;
cout << hara.name << "'s employID is: " << hara.employID << endl;
cout << hara.name << "'s weight is: " << hara.weight << " Kg." << endl;

cout << "2nd name is: " << savi.name << endl;
cout << savi.name << "'s employID is: " << savi.employID << endl;
cout << savi.name << "'s weight is: " << savi.weight << " Kg." << endl;
return 0;
}


■ If we run this program the output will be:

1st name is: Dipak
Dipak's employID is: 123
Dipak's weight is: 56.25 Kg.
2nd name is: Sabitri
Sabitri's employID is: 124
Sabitri's weight is: 49.5 Kg.


■ In the above example we use Structure data.

In C++, a single Structure Data Type can hold different data type at a time.
In the above example, the Structure Data Type holds char, int and float data type simultaneously.
This is its specialty.


■ How to create a Structure Data Type?
Creating a Structure Data Type is a two-part process.

1st part:
We define a Structure description.
This description describes the different types of data that can be stored in that Structure Data Type..
Here it is officeStaff.
The word struct is a keyword to describe the Structure Data Type..

2nd part:
In this part we have to create Structure Variables as per the previous description.
Here we create two Structure variables:
officeStaff hara &
officeStaff savi

We also assign variables to these Structure Variables.
Here these are as follows:

officeStaff hara = {"Dipak", 123, 56.25};
officeStaff savi = {"Sabitri", 124, 49.5};

Which means
officeStaff hara = {char[20], int, float}


Now how to call this variables?
We have to call these values for output.
To call the word 'Dipak' we should use hara.name
cout << "1st name is: " << hara.name << endl;

To call the number '123' we should use hara.employID
cout << hara.name << "'s employID is: " << hara.employID << endl;

To call the number '56.25' we should use hara.weight
cout << hara.name << "'s weight is: " << hara.weight << " Kg." << endl;



◕ Use of Structure Data Type in C++.

Let, in your office you have 50 staffs. You want to update all staffs name, employ ID, salary, joining date etc in one program. In this situation Structure Data Type is very useful. Please remember Structure Data Type is regularly used the C++. Hence it is very important for a C++ programmer.



Related articles:
How to display a value in Hexadecimal form in C++?
How to display a value in Octal form in C++?
How C++ identify the base of a number?
What is the Compound Type in C++?
Array in C++?
Can we read include files in C++?
What is bool Type in C++?
What is char16_t and char32_t in C++?
What is wchar_t in C++?
What is Universal Character Names in C++? How to use Universal Character Names?
What is Escape Sequence in C++? Complete list of Escape Sequence.
Different way to create a new line in C++?



Popular Google Pages:



Top of the page

Amazon & Flipkart Special Products

   


Top of the page