I'm just starting to take up programming and am trying to understand the way a subclass could update a variable in a superclass. I have a family superclass that keeps track of family members and every time a member of the family is created the family members variable adds 1 to it. here is what i have.
I have tried a couple ways of doing this, including creating a update family members method in the superclass and calling it from the child classes in their init statements but it still didn't work. If anyone could help me or at least explain why its not updating I would be grateful. Thanks.
Oh, the playground states it runs the familymembers++ 4 times, but the variable is not keeping the updates. I have also tried creating two variable and having one update the other but still nothing.
EDIT:
Thank you. I will try and understand the concept better. I appreciate your help. Thanks :D
class Family {
var familyMembers = 0
init() {
familyMembers++
}
func talk() {
println("The family is talking")
}
func argue() {
println("The family is arguing")
}
}
class Son : Family {
var hisName = ""
init(name: String) {
super.init()
self.hisName = name
}
override func argue() {
println("\(hisName) is arguing")
}
}
class Mother : Family {
var herName = ""
init(name: String) {
super.init()
self.herName = name
}
}
let barthelemy = Family()
let ayden = Son(name: "ayden")
let tasha = Mother(name: "tasha")
let jim = Son(name: "jim")
barthelemy.familyMembers