NEW: Learn to build the incredible iOS 15 Weather app today! >>

Checkpoint 7: Cats and Dogs

Forums > 100 Days of SwiftUI

For Checkpoint 7, Paul asks us to demonstrate an understanding of class inheritence. Here's my crack at it. Make sure you try it on your own before critiquing my code! Please provide improvements.

class Animal {
    let legs: Int  // typically attached to feet
    init (legs : Int) { self.legs = max(legs, 0)  } // cannot be negative
}

class Dog: Animal {
    init() { super.init( legs: 4 ) }       // dogs are quadrupeds.
    public func speak() { print("bark") }  // standard implementation
}

class Cat: Animal {
    let isTame: Bool
    init ( isTame isTameFromInit: Bool) {
        isTame = isTameFromInit
        super.init(legs: 4) // cats are quadrupeds.
    }
    public func speak() { print("meow?") }
}

class Corgi:  Dog { override func speak() { print ("YAP YAP YAP!")     }}
class Poodle: Dog { override func speak() { print ("WOOF!")            }}

class Persian: Cat {
    init() { super.init(isTame: true)}
    override func speak() { print("Yowwwww Yowwwwww") }}
class Lion:    Cat {
    init() { super.init(isTame: false)}
    override func speak() { print("Roooooooar")       }}

// ====================================
// Test cases to try in Playground   
let larry = Corgi()    // create an instance of the Corgi class
let moe   = Poodle()
let curly = Persian()
let shemp = Lion()

print ("Larry is a dog with \(larry.legs) legs. Larry says ")
larry.speak() ; print("\n")
print ("Moe is a dog with \(moe.legs) legs. Moe says ")
moe.speak() ; print("\n")
print ("Curly is a " + (curly.isTame ? "tame " : "wild ") + "cat with \(curly.legs) legs. Curly says ")
curly.speak() ; print ("\n")
print ("Shemp is a " + (shemp.isTame ? "tame " : "wild ") + "cat with \(shemp.legs) legs. Shemp says")
shemp.speak() ; print("\n")

1      

Really great work and I have no complains! I just want to share my solution as well. It´s simpler than yours and I hope it´s not false.

class Animal {
    let legs: Int

    init(legs: Int) {
        self.legs = legs
    }
}

    class Dog: Animal {
        func speak() {
            print("Bark, Bark with \(legs) legs")
        }
//        init() {
//            super.init(legs: 4)
//        }
    }

        class Corgi: Dog {
            override func speak() {
                print("Wau Wau I am a Corgi and I have \(legs) legs")
            }
        }
        class Poodle: Dog {
            override func speak() {
                print("Wuff Wuff I am a Poodle and I have \(legs) legs")
            }
        }

    class Cat: Animal {
        var isTame: Bool
        func speak() {
            print("Mew Mew")
        }

        init(legs: Int, isTame: Bool) {
            self.isTame = isTame
            super.init(legs: legs) //or (legs: 4)
        }

    }

        class Persian: Cat {
            override func speak() {
                print("Miau Miau Cat \(legs) legs and isTame = \(isTame)")
            }
        }

        class Lion: Cat {
            override func speak() {
                print("Miz Miz I am a Cat with \(legs) legs and isTame? \(isTame)")
            }
        }

let random = Persian(legs: 8, isTame: true)
random.speak()
let random2 = Lion(legs: 4, isTame: false)
random2.speak()

   

I thought it would be fun to try putting a variable called voice and a single Speak() method inside the Cat and Dog subclasses and then use super.init to change the voice:

class Animal { let legs = 4 }

class Dog: Animal {
    var voice: String

    init(voice: String = "Woof") { self.voice = voice }

    func speak() { print("\(voice)!") }
}

class Cat: Animal {
    var voice: String
    var isTame: Bool

    init(voice: String = "Meow", isTame: Bool) {
        self.voice = voice
        self.isTame = isTame
    }

    func speak() { print("\(voice)!") }
}

class Persian: Cat {
    init() { super.init(voice: "Yowl", isTame: true) }
}
class Lion: Cat {
    init() { super.init(voice: "Roar", isTame: false) }
}
class Corgi: Dog {
    init() { super.init(voice: "Ruff") }
}
class Poodle: Dog {
    init() { super.init(voice: "Yip yip") }
}

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Learn the most up-to-date techniques and strategies for testing new and legacy Swift code in this free practical course for iOS devs who want to become complete Senior iOS Developers.

Learn more

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

Reply to this topic…

You need to create an account or log in to reply.

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.