Structure and Union
Structure and Union
Structures in C++ allow grouping related data of different types under one name. They are
essential for:
For example, to manage data for a Student with attributes like name, age, and grade, you
can use a structure.
Structure Declaration
Syntax:
struct StructureName {
DataType Member1;
DataType Member2;
// More members...
};
Example:
struct Student {
string name;
int age;
float grade; };
Declaring Structure Variables
Example:
struct Student {
string name;
int age;
float grade;
};
Example:
struct Student {
string name;
int age;
float grade;
};
// Direct initialization
Student s1 = {"John Doe", 20, 3.8};
// Separate initialization
Student s2;
s2.name = "Jane Smith";
s2.age = 22;
s2.grade = 3.9;
Example:
You can use pointers to structures to reference structure members. The arrow (->)
operator is used when accessing members through pointers.
Example:
Code Implementation
#include <iostream>
#include <string>
using namespace std;
// Structure declaration
struct Student {
string name;
int age;
float grade;
};
int main() {
// Declaring structure variables
Student s1 = {"John Doe", 20, 3.8}; // Direct initialization
Student s2; // Declared but not initialized
return 0;
}
Output
Student 1:
Name: John Doe
Age: 20
Grade: 3.8
Student 2:
Name: Jane Smith
Age: 22
Grade: 3.9
Nested Structures
Example:
struct Address {
string city;
string state;
int zipCode;
};
struct Student {
string name;
int age;
Address address; // Nested structure
};
Uses of Structures
Concept of Unions
A union is a special data type where all members share the same memory
location. At any point, only one member can hold a value, saving
memory.
Syntax:
union UnionName {
DataType Member1;
DataType Member2;
// More members...
};
Example:
union Data {
int intValue;
float floatValue;
char charValue;
};
Data d1;
d1.intValue = 10; // Setting intValue
cout << "Int Value: " << d1.intValue << endl;
Unions of Structures
Example:
struct Student {
string name;
int age;
};
union Data {
Student student;
int id;
};
Data d;
d.id = 101; // Using id
cout << "ID: " << d.id << endl;
#include <iostream>
#include <string>
using namespace std;
struct Address {
string city;
string state;
int zipCode;
};
struct Student {
string name;
int age;
Address address; // Nested structure
};
union Data {
int id;
float salary;
char grade;
};
int main() {
// Nested Structure Example
Student s1 = {"Alice", 21, {"Los Angeles", "CA", 90001}};
cout << "Student Details:" << endl;
cout << "Name: " << s1.name << endl;
cout << "City: " << s1.address.city << endl;
cout << "State: " << s1.address.state << endl;
cout << "Zip Code: " << s1.address.zipCode << endl;
// Union Example
Data d;
d.id = 12345;
cout << "\nID: " << d.id << endl;
return 0;
}
Output
Student Details:
Name: Alice
City: Los Angeles
State: CA
Zip Code: 90001
ID: 12345
Salary: 55000.75
Grade: A
#include <iostream>
#include <string>
using namespace std;
struct Student {
private:
// Private members (not accessible outside the structure)
string studentID;
public:
// Public members (accessible outside the structure)
string name;
int age;
float grade;
int main() {
// Creating a Student object
Student s1("S123", "John Doe", 20, 3.8);
return 0;
}
1. Variables:
a. name, age, and grade are public and directly accessible.
b. studentID is private and accessed through getter and setter
methods.
2. Methods:
a. displayDetails(): Prints the structure's data.
b. updateGrade(): Updates the grade member.
c. getStudentID() and setStudentID(): Access and modify the
private studentID.
Output
Initial Details:
Student ID: S123
Name: John Doe
Age: 20
Grade: 3.8
Updated Grade:
Student ID: S123
Name: John Doe
Age: 20
Grade: 4