In: Computer Science
C++ QUESTION ABOUT FIXING CODE AND RUNNING TESTS:
class SpeakerSystem {
public:
void vibrateSpeakerCones(unsigned signal) const {
cout << "Playing: " << signal << "Hz sound..."
<< endl;
cout << "Buzz, buzzy, buzzer, bzap!!!\n";
}
};
class Amplifier {
public:
void attachSpeakers(const SpeakerSystem& spkrs)
{
if(attachedSpeakers)
cout << "already have speakers attached!\n";
else
attachedSpeakers = &spkrs;
}
void detachSpeakers() { // should there be an "error" message if
not attached?
attachedSpeakers = nullptr;
}
void playMusic( ) const {
if (attachedSpeakers)
attachedSpeakers -> vibrateSpeakerCones(440);
else
cout << "No speakers attached\n";
}
private:
SpeakerSystem* attachedSpeakers = nullptr;
};
QUESTION:
In the code above, you will find a problem compiling. What is it? Note what the method attachSpeakers is trying to do with the address of spkrs. Why is this a problem?
What do you think is the right way to fix it? There is more than one way and how you want to fix it depends on what you think the relationship will be between the amplifier and the speaker system. Know why you choose to pick one solution or the other is at least as important as making a change that gets the code working.
Create some SpeakerSystem and Amplifier variables.
Test by playing music on various speaker system configurations
connected at various times to different speaker systems and trying
to play some amps without speakers and by attaching another set of
speakers to an already complete system (complete here means has
speakers already attached - we're skipping the part about audio
input devised.)
How do you cause a speaker system to become attached to an
amplifier?
In a one-way association, the associated object (SpeakerSystem
here) does not need to know anything about or communicate with the
object it is associated with (Amplifier here).
If either of these situations is needed, then two-way association
would be required (one-way would be the wrong design
decision).
Are there any issues that might need to be considered that a
one-way association cannot deal with in this problem?
#include <iostream>
class SpeakerSystem
{
public:
void vibrateSpeakerCones(unsigned signal) const {
std::cout << "Playing: " << signal << "Hz sound..." << std::endl;
std::cout << "Buzz, buzzy, buzzer, bzap!!!\n";
}
};
class Amplifier {
public:
Amplifier( ) : attachedSpeakers( NULL ) {}
void attachSpeakers(const SpeakerSystem& spkrs)
{
if(attachedSpeakers)
std::cout << "already have speakers attached!\n";
else
attachedSpeakers = &spkrs;
}
void detachSpeakers() {
attachedSpeakers = nullptr;
}
// should there be an "error" message if not attached?
void playMusic( ) const {
if (attachedSpeakers)
attachedSpeakers -> vibrateSpeakerCones(440);
else
std::cout << "No speakers attached\n";
}
private:
//SpeakerSystem* attachedSpeakers = nullptr;
const SpeakerSystem* attachedSpeakers;
};
//main function
int main()
{
//create object spk for class SpeakerSystem
SpeakerSystem spk;
spk.vibrateSpeakerCones(11);
//create object amp for class Amplifier
Amplifier amp;
std::cout << "\nattachSpeakers and playMusic call :"<<std::endl;
amp.attachSpeakers(spk);
amp.playMusic();
std::cout << "\ndetachSpeakers and playMusic call :"<<std::endl;
amp.detachSpeakers();
amp.playMusic();
return 0;
}
output ::
I did some changes you can see in above code as well as given output methods calls from class(refer screenshot ).