UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.