"Mastering C++: Structs Explained with Practical Examples"

Mastering C++ Structs: A Comprehensive Example

In the realm of C++, structs serve as a powerful tool for grouping related data and functions. They are a fundamental part of object-oriented programming, enabling encapsulation and data abstraction. Let's dive into a comprehensive example to understand and appreciate the versatility of C++ structs.

Understanding C++ Struct Basics

Before we embark on our journey, let's quickly recap the basics of C++ structs. A struct is a user-defined data type that allows you to combine data members of different kinds. It is defined using the 'struct' keyword, followed by the struct name, and enclosed in curly braces {}.

Defining a Simple Struct

Here's a simple example of a struct definition:

B2B Marketing in 2026
B2B Marketing in 2026

struct Student {
    std::string name;
    int age;
    double gpa;
};

In this example, we have defined a struct called 'Student' with three data members: 'name', 'age', and 'gpa'.

Initializing and Accessing Struct Members

Now that we have defined our struct, let's see how we can initialize and access its members.

Initializing a Struct

We can initialize a struct in several ways. Here's how you can initialize the 'Student' struct:

DTC Business Model
DTC Business Model

Student student1 = {"John Doe", 20, 3.5};
Student student2;
student2.name = "Jane Smith";
student2.age = 22;
student2.gpa = 3.8;

Accessing Struct Members

To access the members of a struct, we use the dot (.) operator. Here's how you can access the members of 'student1' and 'student2':

std::cout << "Student 1: " << student1.name << ", " << student1.age << ", " << student1.gpa << std::endl;
std::cout << "Student 2: " << student2.name << ", " << student2.age << ", " << student2.gpa << std::endl;

Structs and Functions

Structs can also contain functions, allowing for encapsulation and data abstraction. Let's see an example:

Defining a Function within a Struct

Here's how you can define a function within the 'Student' struct:

Business to Business (B2B) Example
Business to Business (B2B) Example

struct Student {
    std::string name;
    int age;
    double gpa;

    void display() {
        std::cout << "Name: " << name << ", Age: " << age << ", GPA: " << gpa << std::endl;
    }
};

Using the Function

Now, you can call the 'display' function on a 'Student' object like this:

student1.display();
student2.display();

Structs and Inheritance

C++ structs also support inheritance, allowing you to create a hierarchy of types. Let's see an example:

Defining a Base and Derived Struct

Here's how you can define a base struct 'Person' and a derived struct 'Student':

struct Person {
    std::string name;
    int age;

    void display() {
        std::cout << "Name: " << name << ", Age: " << age << std::endl;
    }
};

struct Student : Person {
    double gpa;

    void display() {
        Person::display();
        std::cout << "GPA: " << gpa << std::endl;
    }
};

Using Inheritance

Now, you can create a 'Student' object and call its 'display' function:

Student student3;
student3.name = "Mike Johnson";
student3.age = 21;
student3.gpa = 3.7;
student3.display();

Structs and Arrays

You can also create an array of structs. Here's how you can do it:

Student students[3] = {
    {"John Doe", 20, 3.5},
    {"Jane Smith", 22, 3.8},
    {"Mike Johnson", 21, 3.7}
};

And you can access the elements of the array like this:

for (int i = 0; i < 3; i++) {
    students[i].display();
}

Conclusion

In this article, we've explored the power and versatility of C++ structs. We've seen how to define, initialize, and access struct members. We've also seen how structs can contain functions, support inheritance, and be used in arrays. With this knowledge, you're well-equipped to leverage structs in your C++ projects.

Understand Data Structures in C and C++
Understand Data Structures in C and C++
B2B2C
B2B2C
C# Programming Examples
C# Programming Examples
the b2b vs b2c marketing strategy is shown in this graphic diagram
the b2b vs b2c marketing strategy is shown in this graphic diagram
the b2b marketing framework is shown in this diagram, which shows how to use it
the b2b marketing framework is shown in this diagram, which shows how to use it
an image of a person's face with the eye looking up at something in front of them
an image of a person's face with the eye looking up at something in front of them
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
Basic Structure of C Programming Language
Basic Structure of C Programming Language
the differences between b2b and b2c in business infographical design
the differences between b2b and b2c in business infographical design
the anatomy of a perfect b2b service page
the anatomy of a perfect b2b service page
three different colored lines with the words b2c and c2c on them
three different colored lines with the words b2c and c2c on them
a poster with the words errors in c
a poster with the words errors in c
Difference between B2B & B2C segmentation
Difference between B2B & B2C segmentation
12 Great B2B Website Design Examples | Rahulogy
12 Great B2B Website Design Examples | Rahulogy
What is B2B Marketing? Strategy Guide for 2026
What is B2B Marketing? Strategy Guide for 2026
30-Day CSS Glow-Up: From Messy Styles to Modern Designs
30-Day CSS Glow-Up: From Messy Styles to Modern Designs
How to Create B2B Content 3x Faster on LinkedIn (Executive Content System)
How to Create B2B Content 3x Faster on LinkedIn (Executive Content System)
B2B Model
B2B Model
the diagram shows how we can take care of this project
the diagram shows how we can take care of this project
the diagram shows how to build b2b demand gen model, and how it works
the diagram shows how to build b2b demand gen model, and how it works
CSS Forms Styling Guide for Beginners | Style Input Fields, Buttons & Responsive Forms
CSS Forms Styling Guide for Beginners | Style Input Fields, Buttons & Responsive Forms
an image of a computer screen with the text, day 8 additional statements and instructions
an image of a computer screen with the text, day 8 additional statements and instructions
the two different types of b2 content are shown in this diagram, and there is also
the two different types of b2 content are shown in this diagram, and there is also