Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save engin7/175f5ae244956b58259313fbb883d60f to your computer and use it in GitHub Desktop.
Save engin7/175f5ae244956b58259313fbb883d60f to your computer and use it in GitHub Desktop.
Reference Value Types Questions
import Foundation
// Question 1
struct Cat {
var age: Int
}
var cat = Cat(age: 5)
var fluffyCat = cat
fluffyCat.age = 3
print("Cat's age is \(cat.age)")
print("Fluffy Cat's age is \(fluffyCat.age)")
////----------------------------------------
//// Question 2
class Dog {
var age: Int
init(age: Int) {
self.age = age
}
}
var dog = Dog(age: 5)
var playfulDog = dog
playfulDog.age = 10
print("Dog's age is \(dog.age)")
print("Playful Dog's age is \(playfulDog.age)")
//----------------------------------------
// Question 3.1
struct Computer {
var screen: Screen
}
class Screen {
var type: String
init(type: String) {
self.type = type
}
}
let macbook1 = Computer(screen: Screen(type: "QLED"))
let pc1 = macbook1
macbook1.screen.type = "LCD"
print("Macbook's screen type: \(macbook1.screen.type)")
print("PC's screen type: \(pc1.screen.type)")
//----------------------------------------
// Question 3.2
let screen2 = Screen(type: "QLED")
let macbook2 = Computer(screen: screen2)
let pc2 = macbook2
screen2.type = "LCD"
print("Macbook's screen type: \(macbook2.screen.type)")
print("PC's screen type: \(pc2.screen.type)")
//----------------------------------------
// Question 3.3
let screen3 = Screen(type: "QLED")
let macbook3 = Computer(screen: screen3)
let pc3 = Computer(screen: screen3)
macbook3.screen.type = "LCD"
print("Macbook's screen type: \(macbook3.screen.type)")
print("PC's screen type: \(pc3.screen.type)")
//----------------------------------------
// Question 3.4
let macbook4 = Computer(screen: Screen(type: "QLED"))
let pc4 = Computer(screen: Screen(type: "QLED"))
macbook4.screen.type = "LCD"
print("Macbook's screen type: \(macbook4.screen.type)")
print("PC's screen type: \(pc4.screen.type)")
//----------------------------------------
// Question 4.1
class Plane {
var cockpit: Cockpit
init(cockpit: Cockpit) {
self.cockpit = cockpit
}
}
struct Cockpit {
var hasPilot: Bool
}
var airbus1 = Plane(cockpit: Cockpit(hasPilot: true))
var boeing1 = airbus1
airbus1.cockpit.hasPilot = false
print("Airbus's cockpit has a pilot: \(airbus1.cockpit.hasPilot)")
print("Boeing's cockpit has a pilot: \(boeing1.cockpit.hasPilot)")
//----------------------------------------
// Question 4.2
var cockpit2 = Cockpit(hasPilot: true)
var airbus2 = Plane(cockpit: cockpit2)
var boeing2 = airbus2
cockpit2.hasPilot = false
print("Airbus's cockpit has a pilot: \(airbus2.cockpit.hasPilot)")
print("Boeing's cockpit has a pilot: \(boeing2.cockpit.hasPilot)")
//----------------------------------------
// Question 4.3
var cockpit3 = Cockpit(hasPilot: true)
var airbus3 = Plane(cockpit: cockpit3)
var boeing3 = Plane(cockpit: cockpit3)
airbus3.cockpit.hasPilot = false
print("Airbus's cockpit has a pilot: \(airbus3.cockpit.hasPilot)")
print("Boeing's cockpit has a pilot: \(boeing3.cockpit.hasPilot)")
//----------------------------------------
// Question 4.4
var airbus4 = Plane(cockpit: Cockpit(hasPilot: true))
var boeing4 = Plane(cockpit: Cockpit(hasPilot: true))
airbus4.cockpit.hasPilot = false
print("Airbus's cockpit has a pilot: \(airbus4.cockpit.hasPilot)")
print("Boeing's cockpit has a pilot: \(boeing4.cockpit.hasPilot)")
//----------------------------------------
// Question 5
func incrementer(amount: Int) -> () -> Int {
var total = 0
func sum() -> Int {
total += amount
return total
}
return sum
}
let inc = incrementer(amount: 5)
print(inc()) // ?
print(inc()) // ?
print(inc()) // ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment