Day 4: Object-Oriented Programming - 30 Days Code - IndianTechnoEra
Latest update Android YouTube

Day 4: Object-Oriented Programming - 30 Days Code

Introduction:

Welcome to another coding challenge where we dive into the world of Object-Oriented Programming (OOP). In this challenge, we'll explore the concepts of classes and instances. Understanding OOP is crucial in modern programming, and this task will provide you with hands-on experience.


The Challenge:

The challenge is to implement a Person class with specific behaviors. The Person class should have an instance variable age and a constructor that sets the age while handling certain conditions. Additionally, you need to implement two instance methods: amIOld() and yearPasses().


Write a Person class with an instance variable, , and a constructor that takes an integer, , as a parameter. The constructor must assign  to  after confirming the argument passed as  is not negative; if a negative argument is passed as , the constructor should set  to  and print Age is not valid, setting age to 0.. In addition, you must write the following instance methods:


yearPasses() should increase the  instance variable by .

amIOld() should perform the following conditional actions:

If  age<3, print You are young..

If ag>=13 and age<18, print You are a teenager..

Otherwise, print You are old..


Format & Sample:

Let's explore the expected format and review a sample to better understand the challenge.


Input Format:

The first line contains an integer, t (the number of test cases).

The subsequent lines each contain an integer denoting the age of a Person instance.


Output Format:

Print the appropriate message based on the conditions specified in the amIOld() method. If the constructor sets the age to 0 due to a negative input, print the corresponding message.


Sample Input:

4

-1

10

16

18


Sample Output:

Age is not valid, setting age to 0.

You are young.

You are young.


You are young.

You are a teenager.


You are a teenager.

You are old.


You are old.

You are old.


Explanation:

Test Case 0: Because the input is negative, our code sets the age to 0 and prints the "Age is not valid..." message followed by the young message. Three years pass, and the young message is printed again.

Test Case 1: The code prints that the person is young. Three years pass, and the person is now a teenager.

Test Case 2: The code prints that the person is a teenager. Three years pass, and the person is now old.

Test Case 3: The code prints that the person is old. Three more years pass, and the person is still old, so the old message is printed again.


The extra line at the end of the output is supposed to be there and is trimmed before being compared against the test case's expected output.


Code Breakdown:

Now, let's break down the provided code and understand each section.


Reading Input:

int t;

int age;

cin >> t;

for(int i=0; i < t; i++) {

    cin >> age;

    Person p(age);

    p.amIOld();

    for(int j=0; j < 3; j++) {

        p.yearPasses(); 

    }

    p.amIOld();

    cout << '\n';

}

In this section, we read the input values for the number of test cases (t) and the age of each Person instance.


Printing Output:

Person::Person(int initialAge){

    if(initialAge < 0){

        age = 0;

        cout << "Age is not valid, setting age to 0." << endl;

    } else {

        age = initialAge;

    }

}


void Person::amIOld(){

    if(age < 13) {

        cout << "You are young." << endl;

    } else if(age >= 13 && age < 18) {

        cout << "You are a teenager." << endl;

    } else {

        cout << "You are old." << endl;

    }

}


void Person::yearPasses(){

    age++;

}

In the Person class, the constructor sets the age based on the specified conditions. The amIOld() method prints the appropriate message based on the age, and the yearPasses() method increments the age by one.


Final Code:

#include <iostream>

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

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

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

void Person::yearPasses() {
    age++;
}

int main() {
    int t;
    int age;
    std::cin >> t;
    for (int i = 0; i < t; i++) {
        std::cin >> age;
        Person p(age);
        p.amIOld();
        for (int j = 0; j < 3; j++) {
            p.yearPasses();
        }
        p.amIOld();
        std::cout << '\n';
    }

    return 0;
}


This code demonstrates the implementation of a Person class with specific behaviors. Understanding how to work with classes, instances, and instance methods is crucial in OOP. Feel free to experiment with different inputs to further solidify your understanding. Happy coding!





Post a Comment

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.