Structs, Classes & Inheritance Flashcards
Let’s say you have a coordinate that is a constant, you want to save that coordinate as a tuple with values 0, 0 for x and y. How would you do this?
Let coordinate = (x: 0, y: 0)
struct Point { let x: Int let y: Int } What are x and y called?
Stored properties
When we create this → let coordinatePoint = Point(x: 2, y: 2) (Point is a struct) What is that called?
An instance of the struct
How do we model data in object oriented programming?
By creating objects that serve as data structures, using Structs and Classes
How do you add a comment so that it appears when you option click an object?
///
What is a method?
A method is a function associated with a particular type not just any old function.
What is an instance method?
A method that can only be called on an instance let coordinatePoint = Point(x: 2, y: 2) coordinatePoint.points()
What is an initializer?
It’s just a special instance method that sets up our class ready for use. It has one purpose, to set up an object ready for use. Basically all the stored properties need to have initial values before it is ready to use. (When we create an instance, the initializer is what allows the stored properties to appear) example let coordinatePoint = Point(x: 2, y: 2)
Why doesn’t a struct need an initializer?
It has a memberwise initializer
When there is a value where we can only get a value from it, what kind of property is it?
Read only or a gettable property
If we can change the value of this property, what kind of property is it?
A read write or a settable property
What is the return statement referred to as?
A control transfer statement
TF: Classes generate an automatic member wise initializer to create an instance of the class
False
TF: Instance methods should be narrow in scope and carry out a single task
True
How do we refer to an instance of a class or a struct from within the definition of the object?
Self
Methods that we do not call directly on an instance but help compute the output for another function are informally called
Helper methods
When creating objects we can only use primitive types (like String, Int, etc) as the types for stored properties
False
What is so important regarding inheritance between a struct and a class?
Class allows inheritance.
How do you initialize a subclass?
Provide values for properties of the subclass Once the subclass is initialized, provide values for properties of the base class
When you inherit from one class to the other and you want to set up the initializer, what do you have to do?
Override the previous initializer
TF: The properties of the subclass need to have initial values first before we do anything with the superclass?
True
TF: An Array is a reference type.
False
TF: An Int is a value type.
True
TF: A value type is a type whose value is copied when it is assigned to a variable or constant, or when it is passed to a function
True
TF: Reference types are not copied when they are assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used instead.
True