C++ struct Keyword
The struct
keyword in C++ is used to define a structure, which is a user-defined data type that groups together variables under one name. Structures are commonly used to represent data objects with multiple attributes.
A structure in C++ is similar to a class, but with one key difference: by default, all members of a structure are public, whereas members of a class are private. Structures can contain variables, functions, and even constructors and destructors.
Syntax
struct StructName {
// Members of the structure
data_type member1;
data_type member2;
...
};
- StructName
- The name of the structure.
- member1, member2
- Variables or functions that belong to the structure.
Examples
Example 1: Defining and Using a Simple Structure
In this example, we will see how to define and use a structure to group related variables.
#include <iostream>
using namespace std;
struct Point {
int x;
int y;
};
int main() {
Point p1;
p1.x = 10;
p1.y = 20;
cout << "Point: (" << p1.x << ", " << p1.y << ")" << endl;
return 0;
}
Output:
Point: (10, 20)
Explanation:
- The
Point
structure is defined with two integer members:x
andy
. - In the
main
function, aPoint
objectp1
is created, and its members are assigned values. - The structure’s members are accessed using the dot operator (
.
).
Example 2: Structures with Member Functions
Structures in C++ can contain member functions just like classes.
#include <iostream>
using namespace std;
struct Rectangle {
int length;
int width;
int area() {
return length * width;
}
};
int main() {
Rectangle rect;
rect.length = 5;
rect.width = 10;
cout << "Area: " << rect.area() << endl;
return 0;
}
Output:
Area: 50
Explanation:
- The
Rectangle
structure has two members (length
andwidth
) and one member functionarea()
. - The
area
function calculates and returns the area of the rectangle. - In the
main
function, the member functionarea()
is called using the dot operator.
Example 3: Structures with Constructors
Structures can have constructors to initialize their members.
In this example, we will write a structure that has a constructor to initialise property of the structure.
#include <iostream>
using namespace std;
struct Circle {
double radius;
Circle(double r) { // Constructor to initialize radius
radius = r;
}
double area() {
return 3.14159 * radius * radius;
}
};
int main() {
Circle c(5.0); // Initialize using constructor
cout << "Area of Circle: " << c.area() << endl;
return 0;
}
Output:
Area of Circle: 78.5397
Explanation:
- The
Circle
structure has a constructor that initializes theradius
member. - When a
Circle
object is created, the constructor is called to set the initial value ofradius
. - The
area
function calculates and returns the area of the circle.
Key Points to Remember about struct
Keyword
- Structures are user-defined data types that group related variables.
- By default, members of a structure are public.
- Structures can contain variables, functions, and constructors.
- The dot operator (
.
) is used to access structure members.