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