Structures in C++

Structures in C++

Structures in C++ is defined as a group of variables of different data types under a single name. Class and structure holds a collecion of data of different data types. It is a user-defined data type. 

How to create a structure?

For create a structure ‘struct’ keyword is used.

Syntax of structure in C++

struct structureName{
    member1;
    member2;
    member3;
    .
    .
    .
    memberN;
};

Example of Structure in C++

struct Student
{
    char stuName[30];
    int stuRollNo;
    int stuAge;
};

Program of Structure in C++

#include <iostream>
using namespace std;

struct Employee
{
    char name[50];
    int age;
    float salary;
};

int main()
{
    Employee e1;
    
    cout << "Enter the name: ";
    cin.get(e1.name, 50);
    cout << "Enter age: ";
    cin >> e1.age;
    cout << "Enter salary: ";
    cin >> e1.salary;

    cout << "\n Information." << endl;
    cout << "Name: " << e1.name << endl;
    cout <<"Age: " << e1.age << endl;
    cout << "Salary: " << e1.salary;

    return 0;
}

Output

Enter name: Parth Bairagi
Enter age: 25
Enter salary: 25000

Information.
Name: Parth Bairagi
Age: 25
Salary: 25000

 

Read more, Strings in C++

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *