13.1 User-defined data types Flashcards
Why are user-defined data types necessary?
Necessary as it allows the user to create data types that are custom to their required needs.
Difference between a composite data type vs non-composite?
non-composite data type = defined without reference to another data type. Not iterable, no functions, immutable.
composite data type = built from other data types. e.g. List.
Define an enumerated data type
nc
TYPE Rating = (1 .. 10)
DECLARE Happiness : Rating
Happiness ← 9
from enum import Enum, auto class Lunch(Enum): noodles = auto() rice = auto ()
today = Lunch(1)
Define a pointer
nc TYPE MyPointer = ^INTEGER DECLARE variable1 : MyPointer variable^ ← variable2 variable1 ← @variable3 variable1 ← variable2^ (@ memory location var2) variable ← variable3
Define a set
c
DECLARE MyThings : SET OF STRING
MyThings ← {“phone”, “laptop”, “camera”}
my_things = {“phone”, “laptop”, “camera”}
my_things.intersection(your_things) is A AND B
my_things.union(your_things) is A OR B
Define a record
c TYPE ThingOfWonder DECLARE Thought : STRING DECLARE Prize : INTEGER ENDTYPE
class ThingOfWonder:
def __init__(self):
self. Thought = "" self. Prize = 0
Define a class for a person with a property name
class Person: def \_\_init\_\_(self, name): self.\_\_name = name
def set_name(self, new_name): self.\_\_name = new_name
def get_name(self); return self.\_\_name
Define a child class for person called ReallyImportantPerson
class ReallyImportantPerson(Person): def \_\_init\_\_(self, name, title): super().\_\_init\_\_(name) self.\_\_title = title
def get_name(self): return super().get_name() + "IS MY NAME!"