// inheritance.ino
// derived class (child)
// base class (parent)
/* ------------------------------------------ */
// Base class
class Vehicle {
public:
void honk() {
Serial.println("Tuut, tuut!");
}
};
/* ------------------------------------------ */
// Derived class
class Car: public Vehicle {
public:
char* model = "Mustang";
void autopilot() {
Serial.println("accelerate and brake");
}
};
/* ------------------------------------------ */
// multilevel inheritance
class Hatchback: public Car {
public:
char* model = "Celta";
};
/* ------------------------------------------ */
// multiple inheritance
class Bag {
public:
void openBag() {
Serial.println("Bag is open!");
}
};
class Sedan: public Car, public Bag {
public:
char* model = "Sedan";
};
/* ------------------------------------------ */
int main(void) {
/* ------------------------------------------ */
// setup
init();
Serial.begin(9600);
/* ------------------------------------------ */
// loop
for (;; delay(5000)) {
// base class
Vehicle myVehicle;
myVehicle.honk();
// derived class
Car myCar;
myCar.honk();
myCar.autopilot();
// multilevel inheritance
Hatchback myCelta;
myCelta.honk();
myCar.autopilot();
// multiple inheritance
Sedan mySedan;
mySedan.honk();
mySedan.autopilot();
mySedan.openBag();
}
/* ------------------------------------------ */
return 0;
}