[C++] Introduction to Structs | Thorough Explanation from Definition to Initialization and Assignment

目次

Introduction

In programming, when you want to manage “Student” information, multiple different pieces of data are related, such as “Student ID (int type),” “Name (string type),” and “Average Score (double type).” Managing these variables separately is inefficient. By using C++ “structures (structs),” you can group these related data items into a single original type called “Student.” In this article, I will explain the basic usage of structures in easy-to-understand steps for beginners: 1. Definition -> 2. Variable Declaration -> 3. Accessing Members -> 4. Initialization and Assignment.

1. Definition of Struct (Type)

First, use the struct keyword to define the “blueprint” of the type, specifying what data it consists of. Each variable that makes up the structure is called a “member.”

// Definition of "Student" structure
struct Student {
    int id;           // Student ID (Member)
    std::string name; // Name (Member)
    double average;   // Average score (Member)
};

Explanation: We defined a new type called Student. This type consists of three members: int, std::string, and double. Do not forget the semicolon ; at the end of the definition.

2. Declaration of Struct Variables and Access to Members

Using the defined structure (type), we declare a variable (instance) to actually store data. To access members, use the dot operator (.).

Sample Code

#include <iostream>
#include <string>

using namespace std;

// Definition of "Student" structure
struct Student {
    int id;
    string name;
    double average;
};

int main() {
    // Declare a struct "variable" from the struct "type"
    Student student1;

    // Assign values to each member using the dot (.)
    cout << "Enter Student ID: ";
    cin >> student1.id;
    
    cout << "Enter Name: ";
    cin >> student1.name;
    
    cout << "Enter Average Score: ";
    cin >> student1.average;

    // Reference and display member values
    cout << "\n--- Entered Student Information ---" << endl;
    cout << "Student ID: " << student1.id << endl;
    cout << "Name: " << student1.name << endl;
    cout << "Average Score: " << student1.average << endl;

    return 0;
}

Explanation: You can assign values to or read values from a specific member using the syntax structVariableName.memberName, such as student1.id.

3. Initialization and Assignment of Structs

Structure variables can be initialized at the same time they are declared, or the contents of another structure variable can be copied (assigned) entirely.

Sample Code

#include <iostream>
#include <string>

using namespace std;

struct Student {
    int id;
    string name;
    double average;
};

int main() {
    // 1. Initialize simultaneously with declaration using an initializer list
    Student studentA = {101, "Sato", 85.5};
    
    // 2. Declare another variable
    Student studentB;
    
    // 3. Batch assign (copy) the contents of studentA to studentB
    studentB = studentA;
    
    // Check copied contents
    cout << "Information of copied Student B" << endl;
    cout << "Student ID: " << studentB.id << endl;   // -> 101
    cout << "Name: " << studentB.name << endl;     // -> Sato
    cout << "Average Score: " << studentB.average << endl; // -> 85.5
    
    return 0;
}

Explanation

  • Student studentA = {101, "Sato", 85.5};: You can set initial values for each member at once using an initializer list with curly braces {}. The values correspond to the order of members defined in the structure.
  • studentB = studentA;: Using the assignment operator =, all member values of studentA are copied to the corresponding members of studentB at once (member-wise copy).

Summary

In this article, I comprehensively explained the basic usage of the C++ structure struct.

  • You can define your own data type that groups variables of multiple different types using struct.
  • Use the dot operator (.) to access members.
  • You can set values at the time of declaration using an initializer list ({...}).
  • Structure variables of the same type can be batch-assigned using =.

Structures are one of the most fundamental tools in C++ programming for organizing and managing related data.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次