Static polymorphism (method overloading)

Theory

Confused compiler

Report a typo

Something is causing a compiler error. Please correct it. The program below should print "Playing at default volume" when the play() method is called without any argument.

You don't have to write the main function. Please correct the overloaded method play only.

Write a program in C++
class MediaPlayer {
public:
void play() { std::cout << "Playing at default volume" << std::endl; }

void play(int volume = 0) {
if (volume == 0) {
std::cout << "Playing muted" << std::endl;
} else {
std::cout << "Playing at volume: " << volume << std::endl;
}
}

};

Create a free account to access the full topic