CJCoding With Joseph

Age Check Using an Inherited Field

A derived class can build new logic on top of a field it inherits.

Base class Person has a protected int age and a constructor Person(int a).

Create a derived class Citizen with:
1. a constructor Citizen(int a) that forwards a to Person
2. a method bool canVote() const that returns whether the inherited age is at least 18

The runner prints the boolean (C++ prints 1 for true, 0 for false). For age 20 it prints:
1

Do NOT write a main function.

Expected Output:

1
Topics:
Inheritance
Code Editor
1
Tab to indent ยท Ctrl+Enter to run ยท Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.