2017년 11월 20일 월요일

[C++] HackerRank Day4 Solution

https://www.hackerrank.com/challenges/30-class-vs-instance/problem

class Person{
    public:
        int age;
        Person(int initialAge);
        void amIOld();
        void yearPasses();
    };

    Person::Person(int initialAge){
        if(initialAge < 0){
            cout << "Age is not valid, setting age to 0." << endl;
            age = 0;
        } else{
            age = initialAge;     
        }

    }

    void Person::amIOld(){
        if(this->age < 13){
            cout << "You are young." << endl;
        } else if(this->age < 18){
            cout << "You are a teenager." << endl;
        } else {
            cout << "You are old." << endl;
        }

    }

    void Person::yearPasses(){
        ++this->age;

    }

댓글 없음:

댓글 쓰기