The class MusicPlayer represents a portable electronic system with headphones. C
ID: 3771104 • Letter: T
Question
The class MusicPlayer represents a portable electronic system with headphones. Consider the class diagram shown below. Each instance of a MusicPlayer has a brand, price and capacity associated with it. For the constructor shown below, here is the class definition:
Based on the information shown here, a possible implementation (.cpp) for MusicPlayer is shown below.
myBrand = b;
myPrice = p;
myCapacity = c;
return( myBrand );
myBrand = brand;
return( myPrice );
myPrice = price;
return(myCapacity);
myCapacity = capacity;
Consider the following instance information.
IPod mine; // by default, has a brand of "Apple IPod"
// a price of 249.99 and a capacity of // 10,000 songs
IPod hp( 150.00, 1000, "Hewlett-Packard IPod" );
// this instance has a brand of "Hewlett-Packard IPod",
// a price of 150.00 and a capacity of 1,000 songs
TO RECEIVE FULL CREDIT, YOUR IPOD CONSTRUCTOR MUST CALL THE PARENT-CLASS MUSICPLAYER CONSTRUCTOR.
string getBrand() const;
void setBrand( string brand );
double getPrice() const;
void setPrice( double price );
int getCapacity() const;
void setCapacity( int capacity ); std::string myBrand;
int myCapacity;
double myPrice; class MusicPlayer {
public:
MusicPlayer(string b,double p,int c);
std::string getBrand() const;
void setBrand( std::string brand );
double getPrice() const;
void setPrice( double price );
int getCapacity() const;
void setCapacity( int capacity );
private:
std::string myBrand;
double myPrice;
int myCapacity;
};
Explanation / Answer
//Program:
//Ipod.h
#ifndef Ipod_h
#define Ipod_h
#include "MusicPlayer.h"
class IPod : public MusicPlayer
{
public:
IPod();
IPod(double p,int c,string b);
};
#endif
-----------------------------------------------
//Ipod.cpp
#include "Ipod.h"
//default constructor passes parameter to base class
IPod::IPod()::MusicPlayer("Apple IPod",249.99,10000);
{}
//parameterized constructor passes parameter to base class
IPod(double p,int c,string b)::MusicPlayer(b,p,c)
{}
-----------------------------------------------------
//MusicDriver.cpp contains main()
#include <iostream>
#include "Ipod.h"
#include "MusicPlayer.h"
int main()
{
//instance information
MusicPlayer Walkman("Sony Walkman",99.99, 10);
cout<<endl<<"Walkman"<<endl;
cout<<" Brand Name:"<<Walkman.getBrand()<<endl;
cout<<" Price:"<<Walkman.getPrice()<<endl;
cout<<" Capacity:"<<Walkman.getCapacity()<<endl;
//Ipod
// by default, has a brand of "Apple IPod"
// a price of 249.99 and a capacity of // 10,000 songs
IPod mine;
cout<<endl<<"Ipod 1"<<endl;
cout<<" Brand Name:"<<mine.getBrand()<<endl;
cout<<" Price:"<<mine.getPrice()<<endl;
cout<<" Capacity:"<<mine.getCapacity()<<endl<<endl;
// this instance has a brand of "Hewlett-Packard IPod",
// a price of 150.00 and a capacity of 1,000 songs
IPod hp( 150.00, 1000, "Hewlett-Packard IPod" );
cout<<endl<<"Ipod 2"<<endl;
cout<<" Brand Name:"<<hp.getBrand()<<endl;
cout<<" Price:"<<hp.getPrice()<<endl;
cout<<" Capacity:"<<hp.getCapacity()<<endl<<endl;
return 0;
}
----------------------------------------------------------
//MusicPlayer.h
#ifndef MusicPlayer_h
#define MusicPlayer_h
using namespace std;
class MusicPlayer {
public:
MusicPlayer(string b,double p,int c);
std::string getBrand() const;
void setBrand( std::string brand );
double getPrice() const;
void setPrice( double price );
int getCapacity() const;
void setCapacity( int capacity );
private:
std::string myBrand;
double myPrice;
int myCapacity;
};
#endif
------------------------------------------
//MusicPlayer.cpp
#include "MusicPlayer.h"
MusicPlayer::MusicPlayer(string b,double p,int c) {
myBrand = b;
myPrice = p;
myCapacity = c;
}
std::string MusicPlayer::getBrand() const {
return( myBrand );
}
void MusicPlayer::setBrand( std::string brand ) {
myBrand = brand;
}
double MusicPlayer::getPrice() const {
return( myPrice );
}
void MusicPlayer::setPrice( double price ) {
myPrice = price;
}
int MusicPlayer::getCapacity() const {
return(myCapacity);
}
void MusicPlayer::setCapacity( int capacity ) {
myCapacity = capacity;
}
//---------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.