Structures in C++
The Structure in C++ is a compound data type that contains different variables of different types. For example, you want to store Student details like student name, student roll num, student age. You have two ways to do it, one way is to create different variables for each data, but the downfall of this approach is that if you want to store the details of multiple students, in that case, it is not feasible to create a separate set of variables for each student.
The second and best way of doing it by creating a structure like this:
struct Student { char stuName[30]; int stuRollNo; int stuAge; };
Now these three members combined will act as a separate variable and you can create a structure variable like this:
structure_name variable_name
So if you want to hold the information of two students using this structure then you can do it like this:
Student s1, s2;
Then I can access the members of the Student structure like this:
//Assigning name to first student s1.stuName = "Ajeet"; //Assigning age to the second student s2.stuAddr = 22;
Similarly, I can set and get the values of other data members of the structure for every student. Let’s see a complete example to put this up all together:
Structure Example in C++
#include <iostream> using namespace std; struct Student{ char stuName[30]; int stuRollNo; int stuAge; }; int main(){ Student s; cout<<"Enter Student Name: "; cin.getline(s.stuName, 30); cout<<"ENter Student Roll No: "; cin>>s.stuRollNo; cout<<"Enter Student Age: "; cin>>s.stuAge; cout<<"Student Record:"<<endl; cout<<"Name: "<<s.stuName<<endl; cout<<"Roll No: "<<s.stuRollNo<<endl; cout<<"Age: "<<s.stuAge; return 0; }
Output:
Enter Student Name: Negan ENter Student Roll No: 4101003 Enter Student Age: 22 Student Record: Name: Negan Roll No: 4101003 Age: 22
Related Posts:
- Data Types in C++
- Operators in C++
- Recursion in C++ with example
- Functions in C++ with example
- goto statement in C++ with example
- Break statement in C++ with example
- Continue Statement in C++ with example
- do-while loop in C++ with example
- While loop in C++ with example
- For loop in C++ with example
- Switch Case statement in C++ with example
- If else Statement in C++
- Constructors in C++
- Enumeration in C++
- OOPs Concepts in C++
- this pointer in C++
- Pointers in C++
- Strings in C++
- Passing Array to Function in C++
- Multidimensional Arrays in C++
- Arrays in C++
- Difference between Function Overloading and Function overriding in C++
- Function Overriding in C++ with examples
- Function overloading in C++ with examples
- Polymorphism in C++ and its types
- Inheritance in C++ with examples
- Destructors in C++ with examples
- Encapsulation in C++ with example
- Friend Class and Friend Functions in C++
- Interfaces in C++ with Examples
- Abstraction in C++ with example