Question

In, C++ Design a class instrument, each instrument has a string family: wood winds, brass, percussion, strings and keyboard. Then derive from class instrument a class type, each type object has a name...

In, C++

Design a class instrument, each instrument has a string family: wood winds, brass, percussion, strings and keyboard. Then derive from class instrument a class type, each type object has a name, create a data file with the following information in it, load the data from the file into an array of 10 type objects. Display the data but do not display any type objects with no data in them.

Wood wind alto flute

Brass fluegelhorn

String guitar

Percussion kettle drum

Keyboard harpsicord

Brass euphoniu

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOLUTION ;-

#include<bits/stdc++.h>
using namespace std;
class instrument{ // this is the instrument family
private:
string family;
public:
void setFamily(string family) //this is a setter function
{
this->family=family;
}
string getFamily() // this is the getter function
{
return family;
}
};
class type:public instrument // this is the class type
{
private:
string name;
public:
void setType(string type) // this will take the line from the file and then assign family and it's name
{
int len=type.length();
char arr[50];
int i=0;
while(type[i]!=' ') // this loop will extract the first word
{
arr[i]=type[i];
i++;
}
if(arr[0]=='W') // as Wood wind is a two word family
{
arr[i]=type[i];
i++;
while(type[i]!=' ')
{
arr[i]=type[i];
i++;
}
}
arr[i]='\0';

setFamily(arr); // set the family of the instrument
i++;
int j=0;
char ar[50];

while(i<len) // this will extract name from the line
{
ar[j]=type[i];
i++;
j++;
  

}
  
ar[j]='\0';
name=ar; // assign the name
}
string getName()
{
return name;
}

};
int main()
{
ifstream file;
file.open("data.txt"); // open the data file
type obj[10];
string line;
int i=0;
while(file)
{
getline(file,line);
obj[i].setType(line);
i++;
}
i--; // one object is copied twice
for(int j=0;j<i;j++)
{
cout<<"\n"<<obj[j].getFamily()<<" "<<obj[j].getName();
}
}

This is a program which explain OOP concept  

if u have any doubtd pls comment below ..ill be solve

Add a comment
Know the answer?
Add Answer to:
In, C++ Design a class instrument, each instrument has a string family: wood winds, brass, percussion, strings and keyboard. Then derive from class instrument a class type, each type object has a name...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Assignment: Create a class called FitnessMember that has only one variable, called name. The FitnessMember...

    C++ Assignment: Create a class called FitnessMember that has only one variable, called name. The FitnessMember class should have a constructor with no parameters, a constructor with a parameter, a mutator function and an accessor function. Also, include a function to display the name. Define a class called Athlete which is derived from FitnessMember. An Athlete record has the Athlete's name (defined in the FitnessMember class), ID number of type String and integer number of training days. Define a class...

  • Lab 5.1 C++ Utilizing the code from Lab 4.2, replace your Cargo class with a new...

    Lab 5.1 C++ Utilizing the code from Lab 4.2, replace your Cargo class with a new base class. This will be the base for two classes created through inheritance. The Cargo class will need to have virtual functions in order to have them redefined in the child classes. You will be able to use many parts of the new Cargo class in the child classes since they will have the same arguments/parameters and the same functionality. Child class one will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT