Solution: Polymorphism
Get a detailed explanation of the solution to the polymorphism exercise with if, switch, and hash tables.
We'll cover the following...
Solution using if
Here is the solution to the challenge using if
.
Press + to interact
#include <chrono>#include <iostream>enum class MessageSeverity{information,warning,fatal,};auto start = std::chrono::steady_clock::now();void writeElapsedTime(){auto now = std::chrono::steady_clock::now();std::chrono::duration<double> diff = now - start;std::cerr << diff.count() << " sec. elapsed: ";}void writeInformation(){ std::cerr << "information" << '\n'; }void writeWarning(){ std::cerr << "warning" << '\n'; }void writeUnexpected(){ std::cerr << "unexpected" << '\n'; }void writeMessage(MessageSeverity messServer){writeElapsedTime();if (MessageSeverity::information == messServer){writeInformation();}else if (MessageSeverity::warning == messServer){writeWarning();}else{writeUnexpected();}}int main(){std::cout << '\n';writeMessage(MessageSeverity::information);writeMessage(MessageSeverity::warning);writeMessage(MessageSeverity::fatal);std::cout << '\n';}
Code explanation
- Line 25: After calling
writeElapsedTime()
, we check the value ofmessServer
using anif-else
statement. - Line 19: If
messServer
is equal toMessageSeverity::information
, the function calls