Nested-Structure-in-C++ .

In this session i have discussed nested structure in C++ .


Here we will learn structure within structure or nested structure .


Example : Write a program that demonstrate the nested structure in C++ .

    #include<iostream>
    using namespace std;  
    struct Employee
    {
        char name[20];
        int age;
        struct Sal   // nested struct
        {
            int salary;
        }S;
    }E;
    int main()
    {
        cout<<"\nEnter the Name :";
        cin>>E.name;
        cout<<"\nEnter the Age :";
        cin>>E.age;
        cout<<"\nEnter the Salary :";
        cin>>E.S.salary;
        cout<<"---------------------------------\n";
        cout<<"\nName : "<<E.name;
        cout<<"\nAge : "<<E.age;
        cout<<"\nSalary : "<<E.S.salary;
        return (0);
    }
    OUTPUT : Enter the Name : Rakesh
             Enter the Age  : 20
             Enter the Salary : 20000
             -------------------------------------
             Name : Rakesh
             Age : 20
             Salary : 20000
    


Example : Write a C program that demonstrate the structure with in structure .

    #include<iostream>
    using namespace std;  
    struct Sal
    {
        int salary;
    };

    struct Employee
    {
        struct Sal S;
        char name[20];
        int age;
    }E;

    int main()
    {
        cout<<"\nEnter the Name :";
        cin>>E.name;
        cout<<"\nEnter the Age :";
        cin>>E.age;
        cout<<"\nEnter the Salary :";
        cin>>E.S.salary;
        cout<<"---------------------------------\n";
        cout<<"\nName : "<<E.name;
        cout<<"\nAge : "<<E.age;
        cout<<"\nSalary : "<<E.S.salary;
        return (0);
    }

    
    OUTPUT : Enter the Name : Rakesh
             Enter the Age  : 20
             Enter the Salary : 20000
             -------------------------------------
             Name : Rakesh
             Age : 20
             Salary : 20000
    

Welcome to AndroidATPoint.

back image                     next image