--- layout: exercise language: "cpp" permalink: /Modules/Cpp/Polymorphism title: "CS174: Module 12: C++ Inheritance Exercise 2" excerpt: "CS174: Module 12: C++ Inheritance Exercise 2" canvasasmtid: "103997" canvaspoints: "1" canvashalftries: 5 info: points: 1 instructions: "Change the code in the Person class so that runtime polymorphism is achieved on the celebrateBirthday() method in the Button class, even when we hold a reference to a button object that's Person*." goals: - Work with classes and objects in C++ - Work with inheritance in C++ processor: correctfeedback: "Correct!!" incorrectfeedback: "Try again" submitformlink: false feedbackprocess: | var pos = runtime.text.trim(); correctcheck: | pos.includes("20") incorrectchecks: - incorrectcheck: | pos.includes("40") feedback: "Try again. It appears that you're still calling the celebrateBirthday() method on the Person class." files: - filename: "student.cpp" name: student ismain: false isreadonly: false isvisible: true code: | #include #include using namespace std; class Person { protected: string name; int age; public: Person(string name, int age) { this->name = name; this->age = age; } void celebrateBirthday() { age++; } int getAge() { return age; } }; class Button: public Person { public: Button(string name, int age):Person(name, age){}; void celebrateBirthday() { age--; } }; - filename: "driver.cpp" name: main ismain: true isreadonly: true isvisible: true code: | int main() { Person* benjamin = new Button("Benjamin Button", 30); for (int i = 0; i < 10; i++) { benjamin->celebrateBirthday(); } printf("\n%i\n", benjamin->getAge()); delete benjamin; return 0; } openFilesOnLoad: ["driver.cpp", "student.cpp"] ---