Dear team,
Requesting your constructive criticism of my solution to Checkpoint 7.
While I think I've got the classes right, what I'm unable to do, without seriously breaking the code, is to assign a default value to legs in the Animal class, and then have the option to give an instance of the final classes a different number of legs — should that be the case.
I learned in structs that you could assign a default value, and then choose to modify it when initialising. For example — "Pam Poovey giving herself 35 days of holiday when the default was about 14 days." How do I do that here with legs?
In the code below, though I've given the default value '4' to 'legs', if I remove the 'legs' parameter while initialising, I get wild errors and I can't fix it without two things breaking elsewhere. I tried; but I'm missing something.
Looking forward to your comments. Please also tell me if I've done something else wrong/inefficiently here:
import Cocoa
// Level #1
class Animal {
var name: String
var legs = 4
init(name: String, legs: Int) {
self.name = name
}
}
// Level #2
class Dog: Animal {
func speak() {
print("Bark Bark.")
}
}
class Cat: Animal {
let isTame: Bool
init(name: String, legs: Int, isTame: Bool) {
self.isTame = isTame
super.init(name: name, legs: legs)
}
func speak() {
print("Meow Meow.")
}
}
// Level #3
final class Corgi: Dog {
override func speak() {
print("Arf, Arf, Arf,... AAARrgf!!\", says \(name) the Corgi.")
}
}
final class Poodle: Dog {
override func speak() {
print("Woooo! Woorf, Wooohrf!\", says \(name) the Poodle.")
}
}
final class Persian: Cat {
override func speak() {
print("nnngrrreaou! nnnnreow! Mau.\", says \(name) the Persian Cat.")
}
}
final class Lion: Cat {
override func speak() {
print("ROAAAAR! ROOOOOOOOOOARR!\", says \(name) the Lion.")
}
}
// Creating & Testing Instances
let sandy = Corgi(name: "Sandy", legs: 3)
sandy.speak()
let hector = Poodle(name: "Hector", legs: 2)
hector.speak()
let scrapper = Persian(name: "Scrapper", legs: 4, isTame: false)
scrapper.speak()
let pumba = Lion(name: "Pumba", legs: 8, isTame: true)
pumba.speak()