---
layout: exercise
language: "cpp"
permalink: /Modules/Cpp/Inheritance1
title: "CS174: Module 12: C++ Inheritance Exercise 1"
excerpt: "CS174: Module 12: C++ Inheritance Exercise 1"
canvasasmtid: "103996"
canvaspoints: "1"
canvashalftries: 5
info:
points: 1
instructions: "Override the celebrateBirthday() method in the Button class so that it subtracts one from the age for Button objects instead of adding to the age."
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 looks like you're still using the default implementation of celebrateBirthday()."
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){};
};
- filename: "driver.cpp"
name: main
ismain: true
isreadonly: true
isvisible: true
code: |
int main() {
Button benjamin("Benjamin Button", 30);
for (int i = 0; i < 10; i++) {
benjamin.celebrateBirthday();
}
printf("\n%i\n", benjamin.getAge());
return 0;
}
openFilesOnLoad: ["driver.cpp", "student.cpp"]
---