Structures in C++ | struct in cpp

  Structures in C++ 


What are structures in CPP?

Structures are user-defined data types in CPP or you can say heterogeneous data types. As you know there are traditional (standard) data types in CPP for e.x. - int, char, float, etc. but structure is a container of one or more than one standard data type. For example, if we create a data type named "Student" then the "Student" data type may contain the "name" variable of char type, "roll_no" var of int type, "percentage" variable of float data type.

You can protect your data members with three access specifier
private - Data members & methods can access only in the class
public - Data members & methods can access the program

protected - Data members can access the child structure or clas

But Remember, no function can be a part of either private or public specifier. Also, it is to be kept in mind there was a need for class, because of the concept of inheritance and functions.


How to create structures in CPP?

Structures are defined outside the main() function. To create structures in CPP, you should use the struct keyword, and then the following syntax will follow

#include <iostream> //HEADER FILE
using namespace std;
struct Student 
    //Syntax - data_type name
    {
        public:
        char name[40];
        int roll_no;
        float percentage;

        
    };
    
int main() //MAIN FUNCTION WHICH RETURN TYPE IS INTEGER
{
    Student s1; //Creating Object Of The Structure
    cout<<"Enter The Student Name"<<endl;
    cin>>s1.name; //Syntax - object.variable_name

    cout<<"Enter The Roll No Of Studnet"<<endl;
    cin>>s1.roll_no;

    cout<<"Enter The Previous Class Percentage"<<endl;
    cin>>s1.percentage;
    cout<<endl;

    cout<<"Details Of The Student"<<endl;
    
    cout<<"Name: "<<s1.name<<endl;
    cout<<"Roll No: "<<s1.roll_no<<endl;
    cout<<"Previous Class Percentage: "<<s1.percentage<<endl;

    return 0;
}


Output - 

Enter The Student Name

Dhruv

Enter The Roll No Of Studnet

4566

Enter The Previous Class Percentage

96.32


Details Of The Student

Name: Dhruv

Roll No: 4566

Previous Class Percentage: 96.32


Methods in Structures - 

You can define a function in structures.

#include <iostream> //HEADER FILE
using namespace std;
struct Student 
    //Syntax - data_type name
    {
        public:
        char name[40];
        int roll_no;
        float percentage;

    //Function Inside Structure
    void display_detail() //Function that reutrn empty(nothing) data type(void)
    {
    cout<<"Details Of The Student"<<endl;
    
    cout<<"Name: "<<name<<endl;
    cout<<"Roll No: "<<roll_no<<endl;
    cout<<"Previous Class Percentage: "<<percentage<<endl;
    }

        
    };
    
int main() //MAIN FUNCTION WHICH RETURN TYPE IS INTEGER
{
    Student s1; //Creating Object Of The Structure
    cout<<"Enter The Student Name"<<endl;
    cin>>s1.name; //Syntax - object.variable_name

    cout<<"Enter The Roll No Of Studnet"<<endl;
    cin>>s1.roll_no;

    cout<<"Enter The Previous Class Percentage"<<endl;
    cin>>s1.percentage;
    cout<<endl;

    s1.display_detail();//Calling A Method From The Structure.
    

    return 0;
}




Output - 

Enter The Student Name

Dhananjay

Enter The Roll No Of Studnet

2005

Enter The Previous Class Percentage

98.78


Details Of The Student

Name: Dhananjay

Roll No: 2005

Previous Class Percentage: 98.78

****************************************************************************




Comments

Popular posts from this blog

Array Of Objects In C++ | Array Of Objects Explained With Real Life Example

Pointers In C++ | Pointer Explained With Real Life Examples | Pointers In OOPs C++

Destructor In C++ | Destructor Explained With Real Life Examples