C++ Structures - ProgrammerTech
Language:

Search

C++ Structures

  • Share this:
C++ Structures

C++ Structures (struct)

structure are a set of data of different types (i.e. some of them are int, some are char, some are float .... etc.) with each other so that they can be dealt with as a single unit.

The word struct is used to define a new type, and this new type can contain a set of values ​​in an orderly fashion and is easy to work with.

For example, to write a program to record employee data in a company, we need to store:

  1. The employee name is of type char name[40].
  2. Its address is of type char address[40].
  3. Its age is an integer variable of type int age.
  4. His salary is a real number variable.

As we note, all this data must be dealt with as one unit because it is for one employee, so we need a special record for this employee.

 

Declaring a structure in C++

To declare a structure, we use the reserved word struct, which is an abbreviation of the word structure, which means structure.

We put all the components of this composition within parentheses and seal the statement with a semicolon after the second parenthesis.

Then a special variable for this record is declared in the main menu of the program.

The record is declared in one of the following ways:

#include <iostream.h>
struct employee   /struct name
{
--------;
--------;
--------;
--------;
};   //Semicolons must be placed in simi collins
void main()
{
struct employee emp;  //We declare a variable for the registry

As we can see, we wrote the registry with all the variables inside it, then we started writing the program with the regular expression void main() and then we declared a variable for this registry.

Of course, we choose the name of the variable qualitatively and then continue writing the program.

Create an object from a struct in C++

struct Book {
    string title;
    string author;
    double price;
    int numberOfPages;
};
struct Book book;

 

How to write the variables inside the registry

#include <iostream.h>
struct employee
{
char name[40];
char address[40];
int age;
float salary;
};
//We declare a variable for this struct as follows
void main()
{
struct employee emp;

We created a char array with 40 employees, after that we created a second array to write the employee's address, and a third int array to write the age of this employee, and finally we created a float array to write the salary of this employee.

We then declare a variable for this registry by struct employee emp;.

 

How to enter and output information in the registry

For example, when we want to enter the age of the employee, we type cin >> emp . age; And when we want to enter the name of the employee. We write cin>>emp . name; And we don't write parentheses when we want to enter the full name as we learned in arrays.

But when we want to output any value, we replace cin with cout as it is known.

We can also know about the declaration in another way as follows

void main()
{
struct employee
{
-------;
-------;
-------;
-------;
}; /And we continue the program

example 1

Write a program that prints a student's information, mark, and id number for that student.

#include <iostream.h>
using namespace std;
struct student
{
    int id;
    string name;
    int grade;
};
int main()
{
    student stu;
    stu.id=1;
    stu.name="Ahmed";
    stu.grade=60;
    cout << stu.id << endl;
    cout << stu.name << endl;
    cout << stu.grade << endl;
    return 0;
}

Code result:

1
Ahmed
60

As we noted in the previous example, we recorded the student’s data, Ahmed, his mark, and the hands, and he printed his data for us. What if we had more than one student, what would the data be?

Example 2

Write a program that asks the user to enter the data of 4 students asking for hands, name and mark.

ain()
{
    student stu[4];
    for(int i=0;i<4;i++)
    {
    cout << "enter info student" << i+1 << endl;
    cin >> stu[i].id;
    cin >> stu[i].name;
    cin >> stu[i].grade;
    }
    for(int i=0;i<4;i++)
    {
    cout << stu[i].id << endl;
    cout << stu[i].name << endl;
    cout << stu[i].grade << endl;
    }
    return 0;
}

As we noticed in the example, when running the program, we were asked to enter the data of the first student, then we pressed Enter and requested the data of the second, third and fourth, then printed them for us.


Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com