TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

How to work with default values in Classes (with class inheritance)? — w.r.t. Checkpoint 7

Forums > 100 Days of SwiftUI

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()

2      

I'm not entirely clear on what you are trying to do and what the problem you're running into is, but you can do something like this to have a default number of legs but also have the option to supply a different number if desired.

class Cat: Animal {
    let isTame: Bool

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

    func speak() {
        print("Meow Meow.")
    }
}

let scrapper = Persian(name: "Scrapper", isTame: false)
print(scrapper.legs) //4
let pumba = Lion(name: "Pumba", legs: 8, isTame: true)
print(pumba.legs) //8

2      

Yeah, when I post the code that you have provided in a Playground it seems to work just fine. So, I'm not sure where the error is, but it may be in some other part of your code.

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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.