User-defined data types
도입됨
15:07
사용자 정의 데이터 타입
평범한 문자열 필드는 허구적 데이터를 기꺼이 저장합니다. 차량 유형을 묻고 someone이 Bananas를 입력하면 프로그램은 아무런 불만 없이 이를 수락합니다. 하지만 만약 당신이…
영어 내레이션 · 영어와 중국어 자막 전환 가능
이 수업의 내용
| English | 의미 |
|---|---|
| field/fiːld/ | A named component of a record, with its own value and type. |
| enumerated type/ɪˈnjuːməreɪtɪd taɪp/ | 열거형 |
| enum literal | A named value in the fixed list of an enumerated type. |
| compiler/kəmˈpaɪlə/ | Software that translates a program from a source language into another form, commonly machine or intermediate code. |
| run time/rʌn taɪm/ | The period during which a program is running on a computer. |
| assigned | Given a value to store in a variable or field. |
| user-defined types/ˈjuːzə dɪˈfaɪnd taɪps/ | 사용자 정의 타입 |
| variable/ˈveərɪəbl/ | A quantity or named value that can change or take different values. |
| non-composite/nɒn ˈkɒmpəzɪt/ | 비합성(non-composite) |
| composite/ˈkɒmpəzɪt/ | 합성(composite) |
| named constants | Values given names that do not change while a program runs. |
| integers/ˈɪntɪdʒəz/ | A whole number that may be positive, negative or zero. |
| pointer/ˈpɔɪntə/ | 포인터 |
| memory address/ˈmeməri əˈdres/ | A number that identifies a location in memory. |
| dereference/ˌdiːˈrefrəns/ | 해제引用 |
| dynamic structures | Data structures that can grow or shrink while a program runs. |
| linked list/lɪŋkt lɪst/ | A sequence of nodes connected by references or pointers; the nodes need not occupy adjacent memory locations. |
| nodes/nəʊdz/ | Individual parts of a data structure that hold data and links to other parts. |
| tree/triː/ | A structure of connected nodes that can split into branches. |
| record/ˈrekɔːd/ | 레코드 |
| procedure/prəˈsiːdʒə/ | A named sequence of instructions that performs a task when called. |
| set/set/ | 집합 |
| membership test/ˈmembəʃɪp test/ | A check of whether a particular value is present in a set. |
| union/ˈjuːnɪən/ | The set of elements that belong to at least one of the sets being combined. |
| intersection/ˌɪntəˈsekʃn/ | The set of elements that belong to all of the sets being compared. |
| class/klæs/ | 클래스 |
| instances/ˈɪnstənsɪz/ | A particular occurrence or example; in object-oriented programming, an object created from a class. |
| objects/ˈɒbdʒekts/ | An entity in a program with associated state and behaviour, often an instance of a class. |
| data fields | Named places in a record or object that hold its data values. |
| attributes/ˈætrɪbjuːts/ | 속성 |
| methods/ˈmeθədz/ | 메서드 |
| identifier/aɪˈdentɪfaɪə/ | A name used to identify a program element such as a variable, function or class. |
| indirection | Access to data through an address held by a pointer. |
| array/əˈreɪ/ | An indexed collection of elements arranged in one or more dimensions. |
실수를 불가능하게 만드는 타입
- 한 병원에서 환자의 혈형을
STRING로 저장했습니다. someone이 "O+" 대신 "0+"를 입력하여: 글자 O 대신 숫자 0. 필드는 이를 수락했는데, 왜냐하면 문자열은 아무것도 수락하기 때문입니다. - 프로그램 내에 "0+"가 혈형이 아님을 알 수 있는 것은 없었으며, 이로 인해 오류는 문제가 되는 지점까지 수정되지 않고 전달되었습니다.
- 필드에 정확히 8가지 혈형을 가지는 타입을 부여하면, 프로그램이 실행되기 전 컴파일러에 의해 할당이 즉시 거부됩니다.
- 이것이 사용자 정의 타입이 주는 장점입니다. 이 수업은 커리큘럼에서 명명한 것들이며, 그것들 사이에서 선택하는 방법입니다.

사용자 정의 타입이 필요한 이유
- 내장된
STRING는 몇 가지 합법적인 값 중 하나를 가져야 할 필드에 무의미한 값을 저장할 수 있게 하지만, 사용자 정의 타입은 이를 제한하여 컴파일러가 더 많은 실수를 잡게 합니다. - 실제 엔티티는 일반적으로 서로 다른 타입의 값들의 모음집이지만, 내장형 타입은 값을 하나만 저장할 수 있습니다.
DECLARE MyTaxi : Vehicle는 자기 문서화(self-documenting) 됩니다: 변수가 무엇이라고 말하지만,DECLARE MyTaxi : STRING는 저장 방식만 말합니다.
Why are user-defined types needed? Select 모든 that apply.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Restriction, grouping and self-documentation. Speed is not the reason; correctness and clarity are.
비복합 타입: 나열형
- non-composite 유형은 단일 값을 유지하며, composite 유형은 여러 값을 그룹화합니다.
- 열거형은 고정된 목록의 이름 붙인 상수로 구성된 값을 가집니다. 이 이름들은 새로운 타입의 값으로, 내부적으로 작은 정수로 저장됩니다.
TYPE Vehicle = (M100, M230, T101, T102, T120, T150)
DECLARE MyTaxi : Vehicle
MyTaxi ← T102 // anything outside the list is rejected
- 사용 예: 요일, 색상, 상태 코드, 혈액형 등 합법적 값이 짧고 알려진 목록에 속하는 모든 분야.

여섯 가지 이름 붙인 값만 할당 가능하며, 그 외에는 할당할 수 없습니다

An enumerated type holds:
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
An enumerated type restricts a variable to one of a fixed set of named values (e.g. days of the week).
비합성 타입: 포인터
- 포인터는 다른 변수의 메모리 주소를 유지하거나,
NULL로 "타겟 없음"을 의미합니다. - 포인터를 해제(dereference) 한다는 것은, 표식 기호
p^로 표시되며, 포인터가 가리키는 변수에 접근하는 것입니다.
TYPE PNode = ^TNode // a pointer to a TNode
DECLARE p : PNode
p ← NEW TNode
p^.Value ← 42 // dereference, then reach the field
- 포인터가 동적 구조를 가능하게 합니다: 연결 리스트는 포인터로 이어진 노드이며, 트리는 두 개의 포인터를 가진 노드입니다. 또한, 복사 없이 procedures(절차)에 큰 레코드를 전달하는 데에도 사용됩니다.

변수는 데이터가 아닌 데이터가 있는 위치를 유지합니다

A pointer stores the memory address of another variable, and dereferencing it (e.g. p^) reaches the variable it points to.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Pointers (with NULL marking "points to nothing") are how dynamic structures like linked lists and trees are built.
Reaching the variable a pointer points at, written p^, is called ____.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
The pointer holds an address; dereferencing follows it to the data. That is how linked lists and trees are built.
합성 타입
- 레코드(record) 는 서로 다른 타입의 필드들을 하나의 이름 아래에 묶어,
TYPE … ENDTYPE블록 내에서 정의합니다. 주제 10에서 접한 바 있습니다. - 집합(set) 은 무순서(unordered) 로 중복 없는(unique) 값들의 모음이며, 추가(add), 제거(remove), 멤버십 테스트, 합집합(union), 교집합(intersection) 연산을 지원합니다.
DECLARE Available : SET OF Colour
Available ← {Red, Blue}
IF Green IN Available THEN
…
ENDIF
- 클래스(class) 과 그 인스턴스인 오브젝트(object) 는 데이터 필드인 속성(attributes) 과 그에 대한 연산인 메소드(methods) 를 결합합니다.

서로 다른 타입의 필드들, 하나의 식별자

Match each user-defined type to what it holds.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Enumerated = fixed named values; pointer = an address; class = data + behaviour; set = unique unordered values.
A set is an ordered collection in which a value may appear more than once.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
A set is unordered and its values are unique. An array is the ordered structure that allows repeats.
클래스
CLASS Taxi
PRIVATE Capacity : INTEGER
PUBLIC FUNCTION GetCapacity() RETURNS INTEGER
RETURN Capacity
ENDFUNCTION
ENDCLASS
- 클래스는 정의이며, 오브젝트는 그 인스턴스입니다. 즉, 자체적인 승객 수를 가진 실제 택시 한 대와 같습니다.
- 레코드와의 차이는 메서드에 있으며, 레코드는 데이터만 포함하고 클래스는 데이터와 그에 수반되는 동작을 모두 포함합니다.
A class combines:
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
A class bundles attributes and methods; an object is an instance of a class.
What distinguishes a class from a record?
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Both group fields; only the class carries behaviour. An object is one instance of the class.
worked example: 타입 선택 및 근거 제시
- 예약 시스템은 각 예약에 대해 '요청됨(Requested)', '확정됨(Confirmed)', '취소됨(Cancelled)' 중 하나여야 하는 상태를 저장합니다. 어떤 사용자 정의 타입을 사용할 것이며, 왜?
- 열거형: 합법적 값이 짧고 고정된 목록이므로, 해당 타입은 다른 값의 할당을 방지하며 코드가 도메인(Domain)의 의미를 그대로 반영합니다.
- 그리고 예약 자체에 대해서는? 참조, 날짜, 승객 수, 가격? 레코드, 값들이 서로 다른 타입이며 항상 하나의 예약을 함께 설명하기 때문입니다.
- 아직 빈 좌석의 집합을 어떻게 표현하나요? 셋: 순서가 없고 중복이 없으며, 멤버십 확인이 필요한 연산입니다.
선택 기준
| 필요 | 타입 |
|---|---|
| 짧고 고정된 목록에서의 값 | 열거형 |
| 간접 참조 또는 성장하는 구조 | 포인터 |
| 하나의 것을 설명하는 여러 종류의 필드 | 레코드 |
| 중복 없는 무순서 집합 | 집합 |
| 상태 그리고 그에 작용하는 행동 | 클래스 |
Programming concept lab
Connect examples to the programming idea they show.
For a variable that must be one of {Red, Green, Blue}, the best type is:
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
A fixed list of allowed values is exactly what an enumerated type is for.
놓칠 수 있는 점
- 열거형의 값은 이름 붙인 상수이지 문자열이 아닙니다.
MyTaxi ← "T102"는 다른 것이며 틀린 경우입니다. - 포인터는 주소를 유지하지 값(value)을 유지하지 않습니다. 해제는 값을 도달하는 단계입니다.
- 집합은 무순서이고 중복이 없으며, 배열(array) 은 순서가 있고 반복될 수 있습니다. 문제의 서술이 어떤 것을 알려줍니다.
- 클래스를 레코드보다 더 많은 것으로 만드는 것은 메소드입니다. 차이점을 묻길 때 이 점을 언급하십시오.
이해했습니다
- 사용자 정의 타입은 저장 가능한 내용을 제한하고, 함께 묶여야 할 값들을 그룹화하며, 코드가 자문서처럼 이해되게 합니다.
- 비합성: 열거형(이름 붙인 상수의 고정된 목록)과 포인터(주소, 해제를 통해 도달)
- 합성: 레코드(서로 다른 타입의 필드), 집합(무순서, 고유함), 클래스/오브젝트( 속성 plus 메소드)
- 필요에 따라 선택하고, 고정된 목록, 간접 참조, 혼합 필드, 고유함, 혹은 상태와 행동 중 맞는 성질로 근거를 제시하십시오.
어떤 문제가 있었나요? 사이트 팀만 이 내용을 볼 수 있습니다.