Ugrás a tartalomhoz

Szerkesztő:LinguisticMystic/cpp/Structures

A Wikiszótárból, a nyitott szótárból

🔹 Introduction to Structures in C++

[szerkesztés]

You already know how to store:

  • Individual values using variables (like int age),
  • and homogeneous collections using arrays (like int scores[5]).

But what if you want to store multiple values of different types together, like a name (string), age (int), and height (float)? This is where structures come in!



🔸 Why Use Structures?

[szerkesztés]

[szerkesztés]

Imagine a real-world concept like a student. A student has:

  • a name (string),
  • an age (int),
  • and a grade (char or float).

A structure allows you to group all of these related attributes together in a single unit:

struct Student {
    std::string name;
    int age;
    char grade;
};

✅ 2. Custom (User-Defined) Data Types

[szerkesztés]

With structures, you’re essentially creating your own data types. Just like int or float, you now have Person, Car, Book, or any other meaningful type.

✅ 3. Easier Function Argument Passing

[szerkesztés]

Rather than passing many separate arguments to a function, you can pass a single structure that contains all the necessary data.



🔸 Declaring a Structure

[szerkesztés]
struct Person {
    std::string name;
    int age;
    float height;
};
  • struct is the keyword that tells the compiler you’re creating a structure.
  • Person is the name of the structure type.
  • Inside the {}, you declare members (also called fields) of different types.
  • 🔔 Don’t forget the semicolon ; after the closing brace }!



🔸 Using a Structure

[szerkesztés]

▶ Declaring and Assigning Members

[szerkesztés]
Person mike;
mike.name = "Mike Wazowski";
mike.age = 45;
mike.height = 1.2f;
  • You create a variable mike of type Person.
  • Then you use the dot operator (.) to access and assign values to each field.



🔸 Structure Initialization

[szerkesztés]

▶ Standard Initialization

[szerkesztés]
Person james = { "James P. Sullivan", 47, 2.2 };

▶ C++11 Uniform Initialization

[szerkesztés]
Person james { "James P. Sullivan", 47, 2.2 };

▶ Partial Initialization

[szerkesztés]
Person james = { "James P. Sullivan", 47 };
// height is default-initialized to 0.0

▶ Assignment Using Braces (C++11+)

[szerkesztés]
Person mike;
mike = { "Mike Wazowski", 45, 1.2 };

🔸 Nested Structures

[szerkesztés]

You can define one structure inside another:

struct Address {
    std::string street;
    std::string city;
    std::string country;
};

struct Employee {
    std::string name;
    int age;
    Address address;  // Nested structure
};

Now you can do:

Employee emp;
emp.name = "John Doe";
emp.address.city = "Budapest";

Even further nesting is possible:

struct Company {
    std::string name;
    Employee CEO;
    Address address;
};

Company comp;
comp.CEO.address.street = "Monster Lane";
comp.address.city = "Monstropolis";

🔸 Structures and Functions

[szerkesztés]

Structures can be:

  • Passed to functions by value or by reference
  • Returned from functions

🧪 Example:

[szerkesztés]
#include <iostream>
using namespace std;

struct Person {
    std::string name;
    int age;
    float height;
};

void printPersonInformation(Person person) {
    cout << "Name: " << person.name << endl;
    cout << "Age: " << person.age << endl;
    cout << "Height: " << person.height << endl << endl;
}

int main() {
    Person mike = { "Mike Wazowski", 45, 1.2 };
    Person james = { "James P. Sullivan", 47, 2.2 };

    printPersonInformation(mike);
    printPersonInformation(james);

    return 0;
}

📝 Explanation:

  • We create a function printPersonInformation that takes a Person as a parameter.
  • Inside the function, we access and print its fields using the . operator.
  • In main(), we call the function twice with different people.



💡 Pro Tips

[szerkesztés]
  • You can use const Person& in functions for efficiency, especially when working with large structures:
void printPersonInformation(const Person& person);
  • Structures can also be used with arrays or vectors:
Person group[10];  // Array of 10 Persons
std::vector<Person> groupList;  // Dynamic collection (C++ STL)

🔚 Conclusion

[szerkesztés]

Key Takeaways

[szerkesztés]
Concept Summary
Structure User-defined type that groups different data types
Declaration Use struct keyword followed by fields
Access Dot operator (.) for field access
Nesting Structures can contain other structures
Function Use Structures can be passed/returned to/from functions
Initialization Use {} to set values all at once



🧭 What’s Next?

[szerkesztés]

Structures are fundamental in C++ and serve as a stepping stone toward understanding classes and object-oriented programming.

Soon you’ll explore:

  • The difference between structs and classes
  • Concepts like constructors, methods, and encapsulation
  • Building full objects with behavior, not just data