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.
static int count=0; Person::Person(int initialAge){ if(initialAge>0) age=initialAge; else { age=0; count++; if(count==1) cout<<"Age is not valid, setting age to 0."; else cout<<"\nAge is not valid, setting age to 0."; } } void Person::amIOld(){ count++; if(age<13&&count==1) cout<<"You are young."; else if(age<13&&count!=1) cout<<"\nYou are young."; else if(age>=13&&age<18&&count==0) cout<<"You are a teenager."; else if(age>=13&&age<18&&count!=0) cout<<"\nYou are a teenager."; else if(count==1) cout<<"You are old."; else cout<<"\nYou are old."; } void Person::yearPasses(){ age++; }
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 4: Class vs. Instance
You are viewing a single comment's thread. Return to all comments →
static int count=0; Person::Person(int initialAge){ if(initialAge>0) age=initialAge; else { age=0; count++; if(count==1) cout<<"Age is not valid, setting age to 0."; else cout<<"\nAge is not valid, setting age to 0."; } } void Person::amIOld(){ count++; if(age<13&&count==1) cout<<"You are young."; else if(age<13&&count!=1) cout<<"\nYou are young."; else if(age>=13&&age<18&&count==0) cout<<"You are a teenager."; else if(age>=13&&age<18&&count!=0) cout<<"\nYou are a teenager."; else if(count==1) cout<<"You are old."; else cout<<"\nYou are old."; } void Person::yearPasses(){ age++; }