Inheritance (part 2)

This lesson contains approximately 30 minutes of video content.

Conceptual inheritance walkthrough

Additional material by example

Activity: Generic Device Inheritance

Graded Playground Autograder

Activity Prompt:

Impressed by your programming skills, Merlin's friends ask you to help them write code for their new dorm - they're trying to write basic code for internet-controlled devices, and they want your help! In particular, Merlin's friends have 3 different types of devices they're trying to work with: GenericDevice, Lightbulb, and Speaker.

GenericDevice

The first class that Merlin's friends have been working on is the GenericDevice class: they already know what they want, but they don't know how to write this class. They ask you to implement the following:

Member functions:
Function Description
GenericDevice(); Default Constructor for GenericDevice, = Default. Relies on in-class initialization.
GenericDevice(bool working, std::string producer, int id, double cost); Parameterized constructor for GenericDevice. Set the data members with the values stored in their corresponding parameters.
bool GetWorking() const; Returns whether or not the current device is working.
void SetWorking(bool working); Set the current device's status to the passed parameter.
std::string GetProducer() const; Returns the producer from which the device was acquired.
int GetId() const; Returns the device Id.
double GetCost() const; Returns the cost required to purchase a device.
Data members:
Data member Description
bool working_ = true; Represents whether or not the device is working.
std::string producer_; Represents the source of the device.
int id_ = 0; Represents the unique device ID.
double cost_ = 100; Represents the cost of purchasing said device.
Speaker

Merlin's friends didn't stop here! They also wrote some code for the Speaker type, but it wasn't very well-written: you'll need to work with the incomplete code and comments to ensure it works!

The Speaker's constructors should pass along the relevant parameters to the parameterized base class constructor via the initializer list. The constructors should also use the initializer list to set the derived class's portion with values stored in the corresponding parameters. Make sure the base class is responsible for the base portion of the object and the derived class for the derived portion.

There are getters and setters for song_. The Play behavior simply assigns the song being played (i.e., the std::string argument to the function) to song_. A Speaker is a GenericDevice.

Lightbulb

Merlin's friends are impressed with your coding skills and ask you to implement the Lightbulb type from scratch. A Lightbulb is a GenericDevice. Merlin's friends give you the following specifications.

Member functions:
Function Description
Lightbulb(); Default Constructor for Lightbulb, = Default. Relies on in-class initialization.
Lightbulb(bool working, std::string producer, int id, double cost); Passes the relevant parameters to the parameterized base class constructor via the initializer list. All other data members are initialized with their default values using in-class initialization.
Lightbulb(bool working, std::string producer, int id, double cost, bool enabled, int lifespan); Passes along the relevant parameters to the parameterized base class constructor via the initializer list. Uses initalizer list to set the derived class's portion with values stored in the corresponding parameters.
bool GetStatus() const; Returns whether or not the lightbulb is enabled.
void TurnOn(); Enables the lightbulb.
void TurnOff(); Disables the lightbulb.
int GetLifespan() const; Return how many more "lives" the lightbulb has remaining.

The lifespan of a lightbulb decreases by one every time it changes state from on to off - you will need to account for this! Make sure you consider whether the light was on before decrementing its lifespan. Don't worry too much about negative lifespans; we won't be testing that today.

Data members:
Data member Description
bool enabled_ = true; Represents whether or not the lightbulb is on.
int lifespan_ = 15; Represents how many more "cycles" the lightbulb can run for.
#include "GenericDevice.hpp" #include "Speaker.hpp" #include "Lightbulb.hpp" #include <iostream> int main() { return 0; }
#ifndef GENERICDEVICE_H #define GENERICDEVICE_H #include <string> //Your code here! #endif
#include "GenericDevice.hpp" //Your code here!
#include "Lightbulb.hpp" //Your code here!
#ifndef LIGHTBULB_H #define LIGHTBULB_H #include "GenericDevice.hpp" // Your code here! #endif
#include "Speaker.hpp" Speaker::Speaker(bool working, std::string producer, int id, double cost) { } Speaker::Speaker(bool working, std::string producer, int id, double cost, std::string song) : song_(song) { } void Play(std::string song) { song_ = song; } void StopPlaying() { song_ = ""; } void Speaker::GetSong() const { return song_; }
#ifndef SPEAKER_H #define SPEAKER_H #include "GenericDevice.hpp" // Speaker class extends GenericDevice class Speaker: public GenericDevice { public: // No-argument constructor Speaker() = default; // No song given Speaker(bool working, std::string producer, int id, double cost); // Given all arguments Speaker(bool working, std::string producer, int id, double cost, std::string song); void Play(std::string song_); void StopPlaying(); std::string GetSong() const; private: std::string song_; }; #endif