In this tutorial, I will explain how to use the Structures in c++ programming language. This tutorial will explain you about the Structures in C++ program. After going through of this tutorial, you will able to know:
The structure is a collection of variables of different data types in a single variable name. It is just like of similar to the class, it holds the collection of data types.
Below example will show you declare structures in C++ in your program.
1 2 3 4 5 6 | struct Employees { char name[50]; int age; float salary; }; |
Below program will take input as your full name, your age, and your salary, then this program will print the output as:
1 2 3 4 | Input Information. Name: Prem Tiwari Age: 29 Salary: 30000 |
Copy and paste below program and execute it to better under standing of structure in C++ programming language.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <iostream> using namespace std; struct Employees { char name[50]; int age; float salary; }; int main() { Employees e1; cout << "Enter your full name: "; cin.get(e1.name, 50); cout << "Enter your age: "; cin >> e1.age; cout << "Enter your salary: "; cin >> e1.salary; cout << "\nInput Information." << endl; cout << "Name: " << e1.name << endl; cout <<"Age: " << e1.age << endl; cout << "Salary: " << e1.salary; return 0; } |
Output:
Enter your full name: Prem Tiwari
Enter your age: 29
Enter your salary: 30000
**********************************
Input Information.
Name: Prem Tiwari
Age: 29
Salary: 30000
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: how to define structure in c++, structure, structure in c++, Structures in C++, what is the structure in c++