We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Day 4: Class vs. Instance
Day 4: Class vs. Instance
Sort by
recency
|
1871 Discussions
|
Please Login in order to post a comment
C++14 Solution:
class Person: def init(self, initialAge): if initialAge < 0: self.age = 0 print("Age is not valid, setting age to 0.") else: self.age = initialAge
t = int(input()) for i in range(0, t): age = int(input())
p = Person(age)
p.amIOld() for j in range(0, 3): p.yearPasses()
p.amIOld() print("")
Whats wrong with this code,why is it saying wrong?
every thing is okk ............ last new line is thowing erroer
In C#
//javascript(node.js)
function Person(initialAge) { if (initialAge < 0) { this.age = 0; console.log("Age is not valid, setting age to 0."); } else { this.age = initialAge; } this.amIOld = function () { if (this.age < 13) { console.log("You are young."); } else if (13 <= this.age && this.age < 18) { console.log("You are a teenager."); } else { console.log("You are old."); } }; this.yearPasses = function () { this.age += 1; }; }