C++ program. please show it working, it should use prog7client.cpp prog7instrume
ID: 3711950 • Letter: C
Question
C++ program. please show it working, it should use prog7client.cpp prog7instrument.cpp prog7brass.cpp prog7string.cpp for the g++. Thank you!!!
The text file that needs to be read is prog7.txt
S
Messiah Stradivarius
18000000
1
4
S
The Titanic Violin
1700000
1
2
S
The Lady Tennant
2032000
1
4
S
The Hammer Stradivarius
3544000
1
4
B
French horm
8500
valves
brass
S
Eric Clapton's Blackie
959500
0
6
S
Jimmy Page's double-neck Gibson EDS-1275
1245600
0
12
S
Willie Nelson's Lucille
30
0
6
I
Bass drum
239
B
Trumpet
1200
valves
brass
I
Saxophone
3800
I
Piccolo
149
S
Jimi Hendrix's First Wife
1250000
0
6
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
class MusicalInstrument
{
private:
string name;
double cost;
public:
MusicalInstrument()
{
}
MusicalInstrument(string n,double c)
{
name=n;
cost=c;
}
string getName()
{
return name;
}
void setName(string n)
{
name=n;
}
double getCost()
{
return cost;
}
void setCost(double c)
{
cost=c;
}
virtual void displayInstrument();
};
class BrassInstrument:public MusicalInstrument
{
private :
string sound;
string material;
public :
BrassInstrument()
{
this->sound="";
this->material="";
}
BrassInstrument(string n,double c,string sound,string material):MusicalInstrument(n,c)
{
this->sound=sound;
this->material=material;
}
void displayInstrument()
{
cout<<"Name :"<<MusicalInstrument::getName()<<endl;
cout<<"Cost :"<<MusicalInstrument::getCost()<<endl;
cout<<"Method :"<<sound<<endl;
cout<<"Material :"<<material<<endl;
}
};
class StringInstrument:public MusicalInstrument
{
private :
bool bowed;
int noOfStrings;
public :
StringInstrument()
{
this->bowed=false;
this->noOfStrings=0;
}
StringInstrument(string n,double c, bool bowed,int noOfStrings):MusicalInstrument(n,c)
{
this->bowed=bowed;
this->noOfStrings=noOfStrings;
}
void displayInstrument()
{
cout<<"Name :"<<MusicalInstrument::getName()<<endl;
cout<<"Cost :"<<MusicalInstrument::getCost()<<endl;
if(bowed)
cout<<"Bowed : No"<<endl;
else
cout<<"Bowed : Yes"<<endl;
cout<<"Strings :"<<noOfStrings<<endl;
}
};
int main ()
{
//defines an input stream for the data file
ifstream dataIn;
int count=0,noOfStrings;
char type;
string name,method,material;
double cost;
int bowed;
int SIZE=15;
// Creating array dynamically
MusicalInstrument* mi = new MusicalInstrument[SIZE];
//Opening the input file
dataIn.open("prog7.txt");
//checking whether the file name is valid or not
if(dataIn.fail())
{
cout<<"** File Not Found **";
return 1;
}
else
{
while(!dataIn.eof())
{
dataIn>>type;
if(type=='S')
{
getline(dataIn,name);
cout<<"Name :"<<name;
dataIn>>cost>>bowed>>noOfStrings;
StringInstrument si(name,cost,bowed,noOfStrings);
mi[count]=si;
count++;
}
else if(type=='B')
{
getline(dataIn,name);
dataIn>>cost>>method>>material;
BrassInstrument bi(name,cost,bowed,noOfStrings);
mi[count]=bi;
count++;
}
else if(type=='I')
{
getline(dataIn,name);
dataIn>>cost;
MusicalInstrument m(name,noOfStrings);
mi[count]=m;
count++;
}
}
}
return 0;
}
__________________Have to do few Modificatios..I will do it.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.