Lecture on Structures in C++
Introduction to Structures
A structure in C++ is a user-defined data type that allows you to group together variables of
different data types under a single name. It is useful for representing real-world entities with
multiple attributes, such as:
• Student → Name, Roll Number, Marks
• Car → Model, Year, Price
Structures make your code more organized and easier to manage by grouping related data
together.
Syntax of a Structure
struct StructureName {
dataType1 member1;
dataType2 member2;
...
dataTypeN memberN;
};
• struct → Keyword to define a structure.
• StructureName → Name of the structure (e.g., Student, Car).
• member1, member2, ..., memberN → Members of the structure (variables of different
data types).
Declaring Structure Variables
You can declare variables of a structure type after defining the structure:
StructureName variableName;
Accessing Structure Members
Access the members of a structure using the dot (.) operator:
variableName.member1 = value;
Technical Details of Structures in C++
Feature Description
Definition A structure is defined using the struct keyword.
Members Can be of any data type (e.g., int, float, string, etc.).
Memory Allocation Members are stored in contiguous memory locations.
Size of Structure Sum of sizes of all members (may include padding for alignment).
Accessing Members Use the dot (.) operator for normal variables and arrow (->) for
pointers.
Nested Structures A structure can contain another structure as a member.
Passing to Functions Structures can be passed to functions by value or by reference.
Pointers to You can create pointers to structures for dynamic memory
Structures allocation.
Examples with Explanation
Example 1: Basic Structure
Problem: Define a structure to store student details (name, roll number, and marks). Create a
variable and display its values.
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int rollNumber;
float marks;
};
int main() {
Student s1;
s1.name = "John Doe";
s1.rollNumber = 101;
s1.marks = 95.5;
cout << "Student Details:" << endl;
cout << "Name: " << s1.name << endl;
cout << "Roll Number: " << s1.rollNumber << endl;
cout << "Marks: " << s1.marks << endl;
return 0;
}
Explanation:
• The structure Student has three members: name, rollNumber, and marks.
• A variable s1 of type Student is created, and its members are assigned values.
• The values are displayed using the dot operator (.).
Example 2: Structure with Multiple Variables
Problem: Define a structure to store details of a rectangle (length and width). Calculate and
display its area.
#include <iostream>
using namespace std;
struct Rectangle {
float length;
float width;
};
int main() {
Rectangle rect;
rect.length = 10.5;
rect.width = 5.5;
float area = rect.length * rect.width;
cout << "Area of Rectangle: " << area << endl;
return 0;
}
Explanation:
• The structure Rectangle has two members: length and width.
• A variable rect is created, its members are assigned values, and the area is calculated
and displayed.
Example 3: Structure with Array Member
Problem: Define a structure to store details of a book (title, author, and price). Display the
book details.
#include <iostream>
#include <string>
using namespace std;
struct Book {
string title;
string author;
float price;
};
int main() {
Book b1;
b1.title = "C++ Programming";
b1.author = "John Smith";
b1.price = 499.99;
cout << "Book Details:" << endl;
cout << "Title: " << b1.title << endl;
cout << "Author: " << b1.author << endl;
cout << "Price: " << b1.price << endl;
return 0;
}
Explanation:
• The structure Book has three members: title, author, and price.
• A variable b1 is created, its members are assigned values, and the book details are
displayed.
Example 4: Passing Structure to Function
Problem: Pass a structure to a function to display student details.
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int rollNumber;
float marks;
};
void displayStudent(Student s) {
cout << "Name: " << s.name << endl;
cout << "Roll Number: " << s.rollNumber << endl;
cout << "Marks: " << s.marks << endl;
}
int main() {
Student s1 = {"Jane Doe", 102, 88.5};
displayStudent(s1);
return 0;
}
Explanation:
• A function displayStudent is defined to accept a Student type parameter.
• The structure variable s1 is passed to the function, and its members are displayed.