×
BLUE
C++: STRUCTER


C++: STRUCTER
  20 April 2021
Read more Like this Post.
structure use for storing different data type’s. in structure user can made a new user-defined data type in one data type.
For example we want to make a new data-type for storing student records like name, department, roll number, attendance percentage in this case we need:
  • char data-type for storing student name.
  • int type for storing roll number.
  • char for storing department.
  • float data-type for storing attendance percentage.
This is different data-type’s which we include in one data-type it’s name is _student_ for making new data-type we use struct keyword.

Example

In above example we made a new data-type by using struct keyword in this data-type we include different data-type’s like char for name and department storing, int for roll number and float for storing attendance percentage these all stored in one data-type name _student_. Now let’s call our new data-type in main function.
#include 
    #include 
    using namespace std;

    // section 1
    struct _student_ {
        char name[8];
        char department[5];
        int roll;
        float attendance_percant;
    };
    main() {
        // section 2
        _student_ _std_;
        // section 3
        strcpy(_std_.name, "zohaib");
        strcpy(_std_.department, "BCS-2");
        _std_.roll = 103;
        _std_.attendance_percant = 99.99;
        // section 4
        cout << "name: " << _std_.name << endl;
        cout << "dept: " << _std_.department << endl;
        cout << "roll: " << _std_.roll << endl;
        cout << "atnd: " << _std_.attendance_percant << "%" << endl;
    }
In section 2 first line we made a new variable with _student_ data-type. _student_ is data-type and _std_ is variable name. After making new variable we assign value’s on it in section 3. Calling a member of the variable with ( . ) dot like
_std_.name std_ is variable and name is the member of the variable.
Second line we copy the value in the _std_.name variable. By using strcpy() function. Second we call another variable member which is _std_.department we set the value on it by using strcpy() function after this we call a new variable member which is _std_.roll this is integer type we assign a value on it. After this the last member of the struct is floating point the same way to assigning value on it. In section 4 we print all the members of the structer data-type by using dot ( . ) operator.

Nested structures

We can create structure in structure mean a structure can call a structure like in example:
#include 
    #include 

    using namespace std;

    // section 1
    struct uni_info{
        char uni_name[25];
        char dept_name[10];
    };
    struct _student_ {
        char name[8];
        int roll;
        float attendance_percant;
        struct uni_info dept;
    };
    main() {
        // section 2
        _student_ _std_;
        // section 3
        strcpy(_std_.name, "zohaib");
        strcpy(_std_.dept.dept_name, "BCS-2");
        strcpy(_std_.dept.uni_name, "HU-SUB-CAMPUS-BATTAGRAM");
        _std_.roll = 103;
        _std_.attendance_percant = 99.99;
        // section 4
        cout << "name: " << _std_.name << endl;
        cout << "dept: " << _std_.dept.dept_name << endl;
        cout << "university: " << _std_.dept.uni_name << endl;
        cout << "roll: " << _std_.roll << endl;
        cout << "atnd: " << _std_.attendance_percant << "%" << endl;
    }
In first section we create 2 structures uni_info and _student_ in _student_ structure we call first struct uni_info by name of dept in section 3 we assign value’s of nested structer dept and parent structer _std_. Calling of nested structer first call parent structer then call nested structer by using dot ( . ) then call nested variable member.

Example:

strcpy(_std_.dept.dept_name, "BCS-2");
strcpy(_std_.dept.uni_name, "HU-SUB-CAMPUS-BATTAGRAM");
in the section 4 we call all the variable to print his values.

Array of user-defined variable or structure

The structure array mean if user want to store multiple data in user-defined variable it’s simple just create an array. Let if user want to store data of student’s roll number and student name’s in a class.

Example:

#include 
    #include 

    using namespace std;

    // section 1
    struct _student_ {
        char name[10];
        int roll;
    };
    main() {
        // section 2
        int total_student = 5;
        _student_ _std_[total_student];
        // section 3
        for(int i = 0; i < total_student; i  ){
            cout << i << ". Enter Student Name: ";
            cin >> _std_[i].name;
            cout << "  Enter Student Roll number: ";
            cin >> _std_[i].roll;
        }
        // section 4
       for(int k = 0; k < total_student; k  ){
           cout << k << " . name: " << _std_[k].name;
           cout << "\t roll: " << _std_[k].roll << endl;
       }
    }
In the above programme section 1 we created a struct with name of _student_ and this struct have two member roll and name. In section 2 first we made a variable for setting total students in total_student variable in the above program its 30. Second line we created a variable array which size is equal to total_student variable. In section 3 a for loop looping total for 30 times and the for loop body the program get the name and the roll number from user. And store in the _std_ array. After storing the student record in section 4 we have another for loop for printing the all students record’s one by one.
Related Post's

C++   classes   structers  
Not Loaded Zohaib Jozvi
0 20 April 2021
Read Latest Post.
Read other post.