LAST CHANCE: Save 50% on all my Swift books and bundles! >>

Checkpoint 7 - possible answer

Forums > 100 Days of SwiftUI

I think this works...


//Checkpoint 7

class Animal {
    var numberOfLegs = 4

    init(numberOfLegs: Int) {
        self.numberOfLegs = numberOfLegs
    }
    func speak() {

    }
}

class Dog : Animal {

    override func speak() {

    }

}

final class Corgi : Dog {

    override func speak() {
        print("arf")
    }
}

final class Poodle : Dog {

    override func speak() {
        print("woof")
    }
}

class Cat : Animal {

    var isTame: Bool

    init (numberOfLegs: Int, isTame: Bool) {
        self.isTame = isTame
        super.init(numberOfLegs: numberOfLegs)
    }
    override func speak() {

    }
}

final class Persian : Cat {

    override func speak() {
        print("meooow")
    }
}
final class Lion : Cat {

    override func speak() {
        print("rrooooaaarrr")
    }
}

2      

What sound would these animals make?

let puppy = Dog()
puppy.speak()

let kitten = Cat()
kitten.speak()

You should put some kind of default sound in the speak function for Dog and Cat so you don't end up with mute pets. This is what Paul meant when he said Dog.speak() should print "a generic dog barking string". (I assume he wants you to do similar with Cat.speak() but just didn't mention it.)

You have the same issue here:

let pangolin = Animal()
pangolin.speak()

You don't really need a speak method here, I don't think, unless you want to give every other type of animal besides dogs and cats something to say. In which case, you need to fill in the body of the method.

2      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.