9.17 Introduction to structs, members, and member selection

주홍영·2022년 3월 14일
0

Learncpp.com

목록 보기
108/199

https://www.learncpp.com/cpp-tutorial/introduction-to-structs-members-and-member-selection/

Defining structs

struct 또한 program-defined type이다
아래와 같이 define한다

struct Employee
{
    int id {};
    int age {};
    double wage {};
};

Defining struct objects

struct type을 정의한다고 object가 만들어지는 것은 아니다
object는 다음과 같이 방법으로 만든다

Employee joe; // Employee is the type, joe is the variable name

Accessing members

joe의 member variable에 접근하기 위해서 member selection operator ( . )을 사용한다

#include <iostream>

struct Employee
{
    int id {};
    int age {};
    double wage {};
};

int main()
{
    Employee joe;

    joe.age = 32;  // use member selection operator (.) to select the age member of variable joe

    std::cout << joe.age; // print joe's age

    return 0;
}
profile
청룡동거주민

0개의 댓글