C++ Structures


Go to main Page

Structures in C++

In this article, you'll learn about structures in C++ programming, including what they are, how to describe them, and how to include them in your code. Struc is a collection of elements of different information types grouped together under a single general term. It is similar to a class in that the two possess a set of different types of material.

How to declare a structure in C++ programming?

The struct word specifies a structure category, which is then supported by an attribute .Then we declare one or more members in the curley braces. For example

struct student
{
    char name[30];
    int semester;
    int rollno;
};

In above example struct keyword is used with structure name and in curley braces there are members of structure.

There is no space assigned when a structure is formed.The int determines that variable foo can only contain number elements. Structure specification, on the other hand, only defines what property a structure variable has when it is defined.

How to define a structure variable?

When you announce a structure student, as seen above. A structure variable can be described as follows:

student shameer;

A structure variable name of form structure student is described here.

How to access members of a structure?

We can access members of structure wit a dot(.).In the following example we are accessing semester of student which is member of structure student. Lets Understand with the help of example:

shameer.semester = 4;

C++ Structure Example

Write a C++ Program for assigning data to structure variable members and displaying it?

#include <iostream>
using namespace std;

struct student
{
    char name[40];
    int semester;
    int rollno;
};

int main()
{
    student s1;
    
    cout << "Please Enter your  name: ";
    cin.get(s1.name, 40);
    cout << "Enter semester: ";
    cin >> s1.semester;
    cout << "Enter rollno: ";
    cin >> s1.rollno;

    cout << "\nDisplaying the Info of student s1" << endl;
    cout << "Name: " << s1.name << endl;
    cout <<"Semester: " << s1.semester << endl;
    cout << "Roll number: " << s1.rollno;

    return 0;
}

Output


    Name: ITdesignerspk
    Semester:4
    Roll number:123

Example 2: C++ Structures

Write a C++ program to create a structure of an employee.First enter information and tahn display information.

#include <iostream>
using namespace std;

struct employee
{
    char name[40];
    int age;
    int salary;
};

int main()
{
    employee e1;
    
    cout << "Please Enter employee  name: ";
    cin.get(e1.name, 40);
    cout << "Enter age: ";
    cin >> e1.age;
    cout << "Enter salary: ";
    cin >> e1.salary;

    cout << "\nDisplaying the Info of employee e1" << endl;
    cout << "Employee Name: " << e1.name << endl;
    cout <<"Employee age: " << e1.age << endl;
    cout << "Employee Salary: " << e1.salary;

    return 0;
}

Output


    Employee Name: ITdesignerspk
    Employee age:4
    Employee Salary :12300