Structs, Classes & Inheritance Flashcards

1
Q

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?

A

Let coordinate = (x: 0, y: 0)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
struct Point {
    let x: Int
    let y: Int
}	
What are x and y called?
A

Stored properties

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
When we create this → let coordinatePoint = Point(x: 2, y: 2) (Point is a struct)
What is that called?
A

An instance of the struct

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do we model data in object oriented programming?

A

By creating objects that serve as data structures, using Structs and Classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you add a comment so that it appears when you option click an object?

A

///

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a method?

A

A method is a function associated with a particular type not just any old function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is an instance method?

A
A method that can only be called on an instance
let coordinatePoint = Point(x: 2, y: 2)
coordinatePoint.points()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is an initializer?

A
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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why doesn’t a struct need an initializer?

A

It has a memberwise initializer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When there is a value where we can only get a value from it, what kind of property is it?

A

Read only or a gettable property

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

If we can change the value of this property, what kind of property is it?

A

A read write or a settable property

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the return statement referred to as?

A

A control transfer statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

TF: Classes generate an automatic member wise initializer to create an instance of the class

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

TF: Instance methods should be narrow in scope and carry out a single task

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do we refer to an instance of a class or a struct from within the definition of the object?

A

Self

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Methods that we do not call directly on an instance but help compute the output for another function are informally called

A

Helper methods

17
Q

When creating objects we can only use primitive types (like String, Int, etc) as the types for stored properties

A

False

18
Q

What is so important regarding inheritance between a struct and a class?

A

Class allows inheritance.

19
Q

How do you initialize a subclass?

A
Provide values for properties of the subclass
Once the subclass is initialized, provide values for properties of the base class
20
Q

When you inherit from one class to the other and you want to set up the initializer, what do you have to do?

A

Override the previous initializer

21
Q

TF: The properties of the subclass need to have initial values first before we do anything with the superclass?

A

True

22
Q

TF: An Array is a reference type.

A

False

23
Q

TF: An Int is a value type.

A

True

24
Q

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

A

True

25
Q

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.

A

True