Classes and structs are blueprints in C++ that are used to construct a class instance. Lightweight objects like rectangles, Colour, and Point are created using struct.
In contrast to classes, structs in C++ are of the value type rather than the reference type. It’s handy if you have data that shouldn’t be changed once the struct is created.
C++ Structure is a grouping of several data types. It’s comparable to a class that contains many sorts of data.
The Syntax Of Structure:
struct structure_name
{
// member declarations.
}
A structure is declared in the above declaration by preceding the struct keyword with the identifier (structure name). We can declare member variables of various kinds inside the curly braces. Think about the following scenario:
struct Student
{
char name[20];
int id;
int age;
}
The Student is a structure with three variables: name, id, and age in the example above. No memory is allocated when the structure is defined. The memory is allocated once the instance of a structure is created. Let’s have a look at this case.
How to create the instance of Structure?
The following is an example of a structure variable:
Student s;
s is a structural variable of type Student in this case. Memory will be allocated when the structure variable is created.
One char variable and two integer variables make up the student structure. As a result, one char variable requires 1 byte of memory, but two int require 2*4 = 8. The s variable takes up 9 bytes of memory in total but if the user enterd only 1 character only.
How to access the variable of Structure:
The structure’s variable may be accessed by first using the structure’s instance, then the dot (.) operator, and finally the structure’s field.
s.id = 4;
The dot(.) operator is used to access the id field of the structure Student in the above line, which assigns the value 4 to the id field.
Struct Example In C++:
Let’s look at a simple struct Rectangle example with two data members: width and height.
#include <iostream> using namespace std; struct Rectangle { int width, height; }; int main(void) { struct Rectangle rec; rec.width=5; rec.height=6; cout<<"Area of Rectangle is: "<<(rec.width * rec.height)<<endl; return 0; }
Output:
Area of Rectangle is: 30
Here, look at another example of structure Employee with properties and function:
//Struct In C++ #include <iostream> using namespace std; struct Employee { //properties int Emp_ID; float Emp_Salary; string Emp_Name; //member methods void Input() { cout << "Enter Employee ID:\n"; cin >> Emp_ID; cout << "Enter Employee Name:\n"; cin >> Emp_Name; cout << "Enter Employee Salary:\n"; cin >> Emp_Salary; } //member methods void Display() { cout << "Entered Employee ID:\n"; cout << Emp_ID << endl; cout << "Entered Employee Name:\n"; cout << Emp_Name << endl; cout << "Entered Employee Salary:\n"; cout << Emp_Salary << endl; } }; int main() { Employee E; E.Input(); E.Display(); return 0; }
Output:
Enter Employee ID:
12423
Enter Employee Name:
QWERTY
Enter Employee Salary:
12000
Entered Employee ID:
12423
Entered Employee Name:
QWERTY
Entered Employee Salary:
12000
In the above code, all the properties of class Employee will initialize by the user input, and then display using the Display() method.
C++ Struct Example Using Constructor and Method:
Let’s look at another struct example where the constructor is used to initialize data and the function is used to compute the area of a rectangle.
#include <iostream> using namespace std; struct Rectangle { int width, height; Rectangle(int w, int h) { width = w; height = h; } void areaOfRectangle() { cout<<"Area of Rectangle is: "<<(width*height); } }; int main(void) { struct Rectangle rec=Rectangle(3,6); rec.areaOfRectangle(); return 0; }
Output:
Area of Rectangle is: 18
Difference Between Structure V/s Class:
Structure | class |
If an access specifier is not explicitly stated, it is assumed to be public by default. | If an access specifier is not explicitly stated, it is assumed to be private by default. |
Syntax of Structure: struct structure_name { // body of the structure. } | Syntax of Class: class class_name { // body of the class. } |
“Structure variable” refers to the structure’s instance. | “Object of the class” refers to the class’s instance. |