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
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.
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
Post a Comment