<\/span><\/h2>\n\n\n\nIn this challenge, we’re going to learn about the difference between a class<\/em> and an instance<\/em>; because this is an Object Oriented<\/em> concept, it’s only enabled in certain languages. <\/p>\n\n\n\n<\/span>Task<\/strong><\/span><\/h2>\n\n\n\nWrite a Person<\/em> class with an instance variable, age<\/strong><\/em>, and a constructor that takes an integer, initialAge<\/strong><\/em>, as a parameter. The constructor must assign initialAge<\/strong><\/em> to age<\/strong><\/em> after confirming the argument passed as initialAge<\/strong><\/em> is not negative; if a negative argument is passed as initialAge<\/strong><\/em>, the constructor should set age<\/strong><\/em> to 0<\/strong>and print Age is not valid, setting age to 0.<\/code>. In addition, you must write the following instance methods:<\/p>\n\n\n\n- yearPasses()<\/em> should increase the age<\/em><\/strong> instance variable by 1<\/strong>.<\/li>
- amIOld()<\/em> should perform the following conditional actions:
- If age<\/em> < 13<\/strong>, print
You are young.<\/code>.<\/li>- If age<\/em> => 13<\/strong> and age<\/em> < 18<\/strong>, print
You are a teenager.<\/code>.<\/li>- Otherwise, print
You are old.<\/code>.<\/li><\/ul><\/li><\/ol>\n\n\n\nTo help you learn by example and complete this challenge, much of the code is provided for you, but you’ll be writing everything in the future. The code that creates each instance of your Person<\/em> class is in the main<\/em> method. Don’t worry if you don’t understand it all quite yet!<\/p>\n\n\n\nNote:<\/strong> Do not remove or alter the stub code in the editor.<\/p>\n\n\n\n<\/span>Input Format <\/strong><\/span><\/h2>\n\n\n\nInput is handled for you by the stub code in the editor.<\/p>\n\n\n\n
The first line contains an integer, T<\/em><\/strong> (the number of test cases), and the T<\/em><\/strong> subsequent lines each contain an integer denoting the age<\/em><\/strong> of a Person instance.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- 1 <= T<\/em> <= 4<\/strong><\/li>
- -5 <= age<\/em> <= 30<\/strong><\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nComplete the method definitions provided in the editor so they meet the specifications outlined above; the code to test your work is already in the editor. If your methods are implemented correctly, each test case will print 2<\/strong> or 3<\/strong> lines (depending on whether or not a valid initialAge<\/em><\/strong> was passed to the constructor).<\/p>\n\n\n\nSample Input<\/strong><\/p>\n\n\n\n4\n-1\n10\n16\n18<\/code><\/pre>\n\n\n\nSample Output<\/strong><\/p>\n\n\n\nAge is not valid, setting age to 0.\nYou are young.\nYou are young.\n\nYou are young.\nYou are a teenager.\n\nYou are a teenager.\nYou are old.\n\nYou are old.\nYou are old.<\/code><\/pre>\n\n\n\nExplanation<\/strong><\/p>\n\n\n\nTest Case 0<\/em>: initialAge<\/em> = -1<\/strong>
Because initialAge<\/em> < 0<\/strong>, our code must set age<\/em><\/strong> to 0<\/strong> and print the “Age is not valid…” message followed by the young message. Three years pass and age<\/em> = 3<\/strong>, so we print the young message again.<\/p>\n\n\n\nTest Case 1:<\/em> initialAge<\/em> = 10<\/strong>
Because initialAge<\/em> < 13<\/strong>, our code should print that the person is young. Three years pass and age<\/em> = 13<\/strong>, so we print that the person is now a teenager.<\/p>\n\n\n\nTest Case 2:<\/em> initialAge<\/em> = 16<\/strong>
Because 13 <= initialAge<\/em> < 18<\/strong>, our code should print that the person is a teenager. Three years pass and age<\/em> = 19<\/strong>, so we print that the person is old.<\/p>\n\n\n\nTest Case 3:<\/em> initialAge<\/em> = 18<\/strong>
Because initialAge<\/em> => 18<\/strong>, our code should print that the person is old. Three years pass and the person is still old at age<\/em> = 21<\/strong>, so we print the old message again.<\/p>\n\n\n\n<\/span>Solution – Day 4: Class vs. Instance<\/strong><\/span><\/h2>\n\n\n\n<\/span>C++<\/strong><\/span><\/h3>\n\n\n\nusing namespace std;\n#include <iostream>\n#include <iostream>\n\nusing namespace std;\n\nclass Person {\npublic:\n int age;\n\n Person(int initialAge);\n\n void amIOld();\n\n void yearPasses();\n};\n\nPerson::Person(int initialAge) \n{\n \/\/ Add some more code to run some checks on initialAge\n if (initialAge > 0) age = initialAge;\n else \n {\n cout << \"Age is not valid, setting age to 0.\" << endl;\n age = 0;\n }\n}\n\nvoid Person::amIOld() \n{\n \/\/ Do some computations in here and print out the correct statement to the console\n if (age < 13) cout << \"You are young.\" << endl;\n else if (age < 18) cout << \"You are a teenager.\" << endl;\n else cout << \"You are old.\" << endl;\n}\n\nvoid Person::yearPasses() \n{\n \/\/ Increment the age of the person in here\n age++;\n}\nint main(){\n int t;\n\tint age;\n cin >> t;\n for(int i=0; i < t; i++) {\n \tcin >> age;\n Person p(age);\n p.amIOld();\n for(int j=0; j < 3; j++) {\n \tp.yearPasses(); \n }\n p.amIOld();\n \n\t\tcout << '\\n';\n }\n\n return 0;\n}<\/pre>\n\n\n\n