C++ please help by showing working program. clearer instructions have already be
ID: 3711529 • Letter: C
Question
C++ please help by showing working program. clearer instructions have already been uploaded. search Q&A for
Program #7 DUE: Specified on Canvas Program Purpose For this program you are asked to use C++ inheritance to create a base class and two derived lasses You will also be working with construcors and redefining/ovedoading class member functions Mandatory Instructions 1. Design a Masicalinsment dass that has the following members A mamber variable for the name of the instrument (string) -A member variable for the cost the instrument (double) - A default constructor that sets the name to the empty string and the aost to zero An overloeded constructor that accepts values for the name and cost and assigns them to the duta members - Accessors and mutators for the name and cost A virtual display fiunction display Instrument that displays the instrument's name and the dost Store the declaration for this class in a file called prog Tinstrumenth. Implament funcations in epp file 2. Design a Brassins trument class that is derived from the Musical io strument class with A mamber variable for the method of sound making (string): slides, valves, - Amember variable for the material from which the instrument is made (stongi brass or wood - A default constructor that invokes the Musicallrnstrument default constructor to initialize the inher ited data members and sets he Bras sin strnment fields to empty string - A second constructor that accepts an instrument's name, cost, method of making sound, and materal fom which it as made Pass the first two arguments to the Musicalinstrument construtor and use the last twe to set the Brassiestrment data membrs A displaylastrument function that overrides the displaylastrument function in the hase class It should display the instrument's name, cost, method of mak ing sound, and material from which it is made Store the declaration for the BrassInstrument class in the file progTbras.&.Implemanst functions in cpp file of same name 3. Design a Strnginstrument class that is derived frogn the Musicalinstramernt class with - Amember variable for the flag to indicate abowed instrument (bool) - Amamber variable for the number of - A defnult constructor that invokes t bowed fag to faise and number of strings to zero nt) default constructor to initialixe the inhenited data members and sets name, oost, bowed flag, and number of strings Pass the first two A second constructor that acceptsa arguments to the Msicallnstrumentconstructór and use the last two to set the Stringins mnt's data members . A displaylestrument dass's function (to display the displayl nstrument function in the hase class It should tirst call the hase s name and cost) and then display the string instrument's data members nr class in the file prog7string.h. Implement functions in epp file of same na Store the dechration progidientepp to demons trate the thre musical instrument classes Define an am of peicrs to to store pointers of up to fifeen Afusicalin strumen, Brasslnstrument o String sinentobects 4. Wribe data from the file prog? txt. an B'rass instrument or S'tring instrumant For each set of instrument data it contains a code to indkale whether the daa tht If B' then the instrument's name, cost, method of making sound, and material are also induded in the data file If the code is "S" then the code is followed by the instrument's rume, cost, bowed flag(1 for true, O Rw false) and number of Use the mew operator to create either a Brassinstrument or a Saringinssnument object, storing the pomeer to the object in the next dement of the army 6. In your header file for the Adusicaliustrumenr class, make your displayinstrument function virtual Then wise a loop to display the data for all of the instrument data stored in the arrayExplanation / Answer
// File Name: prog7instrument.h
#ifndef PROG7INSTRUMENT_H
#define PROG7INSTRUMENT_H
#include<string>
using namespace std;
// Class MusicalInstrument definition
class MusicalInstrument
{
// Data member to store data
string name;
double cost;
public:
// Prototype of member functions
// Constructors
MusicalInstrument();
MusicalInstrument(string, double);
// Setter method to set data to data member
void setName(string);
void setCost(double);
// Getter function to return data member
string getName();
double getCost();
// Virtual function to display Musical class data
virtual void displayInstrument();
};// End of class
#endif
--------------------------------------------------------------------
// File Name: prog7brass.h
#ifndef PROG7BRASS_H
#define PROG7BRASS_H
#include "prog7instrument.h"
#include<string>
using namespace std;
// Class BrassInstument derived from MusicalInstrument
class BrassInstument : public MusicalInstrument
{
// Data member to store data
string soundMaking;
string instrumentMade;
public:
// Prototype of member functions
// Constructors
BrassInstument();
BrassInstument(string, double, string, string);
// Setter method to set data to data member
void setSoundMaking(string);
void setInstrumentMade(string);
// Getter function to return data member
string getSoundMaking();
string getInstrumentMade();
// Virtual function to display brass class data
virtual void displayInstrument();
};// End of class
#endif
-------------------------------------------------------
// File Name: prog7string.h
#ifndef PROG7STRING_H
#define PROG7STRING_H
#include "prog7instrument.h"
#include<string>
using namespace std;
// Class StringInstument derived from MusicalInstrument
class StringInstument : public MusicalInstrument
{
// Data member to store data
bool bowedInstrument;
int numberOfStrings;
public:
// Prototype of member functions
// Constructors
StringInstument();
StringInstument(string, double, bool, int);
// Setter method to set data to data member
void setBowedInstrument(bool);
void setNumberOfStrings(int);
// Getter function to return data member
bool getBowedInstrument();
int getNumberOfStrings();
// Virtual function to display String class data
virtual void displayInstrument();
};// End of class
#endif
-----------------------------------------------------
// File Name: prog7instrument.cpp
#include<iostream>
#include<string>
#include "prog7instrument.h"
using namespace std;
// Default constructor definition to initialize data member
MusicalInstrument::MusicalInstrument()
{
name = "";
cost = 0.0;
}// End of default constructor
// Parameterized constructor definition to initialize parameter values to data member
MusicalInstrument::MusicalInstrument(string name, double cost)
{
// When class data member name and parameter variable name is same
// this is used to refer to class data member
this->name = name;
this->cost = cost;
}// End of parameterized constructor
// Function to set name
void MusicalInstrument::setName(string name)
{
this->name = name;
}// End of function
// Function to set cost
void MusicalInstrument::setCost(double cost)
{
this->cost = cost;
}// End of function
// Function to return name
string MusicalInstrument::getName()
{
return name;
}// End of function
// Function to return cost
double MusicalInstrument::getCost()
{
return cost;
}// End of function
// Function to display Musical data
void MusicalInstrument::displayInstrument()
{
cout<<" Name: "<<name;
cout<<" Cost: "<<cost;
}// End of function
--------------------------------------------------------
// File Name: prog7brass.cpp
#include<iostream>
#include "prog7brass.h"
#include "prog7instrument.cpp"
using namespace std;
// Default constructor definition and calls base class default constructor
BrassInstument::BrassInstument() : MusicalInstrument()
{
soundMaking = "";
instrumentMade = "";
}// End of default constructor
// Parameterized constructor definition to initialize parameter values to data member
// Calls the base class constructor by passing name and cost
BrassInstument::BrassInstument(string name, double cost, string sound, string made) : MusicalInstrument(name, cost)
{
// When class data member name and parameter variable name is same
// this is used to refer to class data member
soundMaking = sound;
instrumentMade = made;
}// End of parameterized constructor
// Function to set sound
void BrassInstument::setSoundMaking(string sound)
{
soundMaking = sound;
}// End of function
// Function to set made
void BrassInstument::setInstrumentMade(string made)
{
instrumentMade = made;
}// End of function
// Function to return sound
string BrassInstument::getSoundMaking()
{
return soundMaking;
}// End of function
// Function to return made
string BrassInstument::getInstrumentMade()
{
return instrumentMade;
}// End of function
// Overrides base class displayInstrument() function to display musical and brass class data
void BrassInstument::displayInstrument()
{
cout<<" Type: Brass Instrument";
// Calls the base class function to display musical data
MusicalInstrument::displayInstrument();
cout<<" Method: "<<soundMaking;
cout<<" Material: "<<instrumentMade;
}// End of function
-------------------------------------------------------
// File Name: prog7string.cpp
#include<iostream>
#include<string>
#include "prog7instrument.h"
#include "prog7string.h"
using namespace std;
// Default constructor definition and calls base class default constructor
StringInstument::StringInstument() : MusicalInstrument()
{
bowedInstrument = false;
numberOfStrings = 0;
}// End of default constructor
// Parameterized constructor definition to initialize parameter values to data member
// Calls the base class constructor by passing name and cost
StringInstument::StringInstument(string name, double cost, bool bowed, int number) : MusicalInstrument(name, cost)
{
bowedInstrument = bowed;
numberOfStrings = number;
}// End of parameterized constructor
// Function to set bowed
void StringInstument::setBowedInstrument(bool bowed)
{
bowedInstrument = bowed;
}// End of function
// Function to set number of string
void StringInstument::setNumberOfStrings(int number)
{
numberOfStrings = number;
}// End of function
// Function to set bowed
bool StringInstument::getBowedInstrument()
{
return bowedInstrument;
}// End of function
// Function to return number of strings
int StringInstument::getNumberOfStrings()
{
return numberOfStrings;
}// End of function
// Overrides base class displayInstrument() function to display musical and string class data
void StringInstument::displayInstrument()
{
cout<<" Type: String Instrument";
// Calls the base class function to display musical data
MusicalInstrument::displayInstrument();
// Checks if bowedInstrument value is one display "yes"
if(bowedInstrument)
cout<<" Bowed: Yes";
// Otherwise bowedInstrument value is zero display "no"
else
cout<<" Bowed: No";
cout<<" Strings: "<<numberOfStrings;
}// End of function
---------------------------------------------------------------
// File Name: prog7string.cpp
#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
#include "prog7brass.cpp"
#include "prog7string.cpp"
#define MAX 15
using namespace std;
// main function definition
int main()
{
// Declares array of objects for the classes
MusicalInstrument musicI[MAX];
BrassInstument brassI[MAX];
StringInstument stringI[MAX];
// Declares counter for each class
int counterM = 0, counterB = 0, counterS = 0;
// ifstream object declared
ifstream rFile;
// To store temporary data read from file
char type;
string data;
double co;
bool bo;
int no;
// Opens the file for reading
rFile.open("prog7.txt");
// Check that file can be opened or not
// is_open() function returns true if a file is open and associated with this stream object.
// Otherwise returns false.
if(!rFile.is_open())
{
// Displays error message
cout<<" Error: Unable to open the file! ";;
exit(1);
}// End of if condition
// Loops till end of file
while(!rFile.eof())
{
// Reads the type character
rFile>>type;
// Checks if the type is 'I'
if(type == 'I')
{
// Ignore the unnecessary data from file
rFile.ignore();
// Reads data from file and stores it in counterM index position of musicI array of objects
// Using setter method
getline(rFile, data);
musicI[counterM].setName(data);
rFile>>co;
musicI[counterM].setCost(co);
// Increase the music class counter by one
counterM++;
}// End of if condition
// Checks if the type is 'S'
else if(type == 'S')
{
rFile.ignore();
// Reads data from file and stores it in counterS index position of stringI array of objects
// Using setter method
getline(rFile, data);
stringI[counterS].setName(data);
rFile>>co;
stringI[counterS].setCost(co);
rFile>>bo;
stringI[counterS].setBowedInstrument(bo);
rFile>>no;
stringI[counterS].setNumberOfStrings(no);
// Increase the String class counter by one
counterS++;
}// End of else if condition
// Checks if the type is 'B'
else if(type == 'B')
{
rFile.ignore();
// Reads data from file and stores it in counterB index position of brassI array of objects
// Using setter method
getline(rFile, data);
brassI[counterB].setName(data);
rFile>>co;
brassI[counterB].setCost(co);
rFile>>data;
brassI[counterB].setSoundMaking(data);
rFile>>data;
brassI[counterB].setInstrumentMade(data);
// Increase the Brass class counter by one
counterB++;
}// End of else if condition
}// End of while loop
// Loops till number of music
for(int x = 0; x < counterM; x++)
// Calls the function to display music data
musicI[x].displayInstrument();
// Loops till number of brass
for(int x = 0; x < counterB; x++)
// Calls the function to display brass data
brassI[x].displayInstrument();
// Loops till number of string
for(int x = 0; x < counterS; x++)
// Calls the function to display string data
stringI[x].displayInstrument();
}// End of main function
prgo7.txt file contents
S
Messiah Stradivarious
18000000
1
4
S
The Titanic Violin
1700000
1
2
S
The Lady Tennant
2302000
1
4
S
The Hammer Stradivarius
35744000
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
Brass Drum
239
B
Trumpet
1200
valves
brass
I
Saxophone
3800
I
Saxophone
3800
I
Piccolo
149
S
Jimi Hendrix's First Wife
1250000
0
6
Sample Output:
Name: Brass Drum
Cost: 239
Name: Saxophone
Cost: 3800
Name: Saxophone
Cost: 3800
Name: Piccolo
Cost: 149
Type: Brass Instrument
Name: French horm
Cost: 8500
Method: valves
Material: brass
Type: Brass Instrument
Name: Trumpet
Cost: 1200
Method: valves
Material: brass
Type: String Instrument
Name: Messiah Stradivarious
Cost: 1.8e+007
Bowed: Yes
Strings: 4
Type: String Instrument
Name: The Titanic Violin
Cost: 1.7e+006
Bowed: Yes
Strings: 2
Type: String Instrument
Name: The Lady Tennant
Cost: 2.302e+006
Bowed: Yes
Strings: 4
Type: String Instrument
Name: The Hammer Stradivarius
Cost: 3.5744e+007
Bowed: Yes
Strings: 4
Type: String Instrument
Name: Eric Clapton's Blackie
Cost: 959500
Bowed: No
Strings: 6
Type: String Instrument
Name: Jimmy Page's double-neck Gibson EDS-1275
Cost: 1.2456e+006
Bowed: No
Strings: 12
Type: String Instrument
Name: Willie Nelson's Lucille
Cost: 30
Bowed: No
Strings: 6
Type: String Instrument
Name: Jimi Hendrix's First Wife
Cost: 1.25e+006
Bowed: No
Strings: 6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.