Introduction
Step 1: Defining a Struct
#include <iostream>
#include <string>
struct Person {
std::string name;
int age;
};
Step 2: Creating Struct Instances
Person person1;
person1.name = "Alice";
person1.age = 30;
Person person2;
person2.name = "Bob";
person2.age = 25;
Step 3: Accessing and Modifying Struct Members
std::cout << person1.name << " is " << person1.age << " years old." << std::endl;
std::cout << person2.name << " is " << person2.age << " years old." << std::endl;
person1.age += 1;
std::cout << person1.name << " is now " << person1.age << " years old." << std::endl;
Output:
Alice is 30 years old.
Bob is 25 years old.
Alice is now 31 years old.
Conclusion
You’ve learned how to use structs in C++! Structs are a powerful way to organize and group data of different types together. Keep practicing and exploring other intermediate programming concepts! 🚀