i have a code to read some products from a file.(.dat). it hasthe name, model number, price and its purpose.
example. = knife 103 10 cut
currently it can read the first 3. The question asks i make a derived class to read the purpose. Here is my code
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int maxProds = 20;
class product {
private:
string name;
string model;
float price;
string purpose;
public:
void createProduct(string,string,float,string);
void readRecord(ifstream &);
void displayDetails();
};
void product::createProduct(string n, string m, float p, string u) {
name =n;
model = m;
price =p;
purpose =u;
}
void product::readRecord(ifstream &infile)
{
infile>>name;
infile>>model;
infile>>price;
infile>>purpose;
}
void product::displayDetails()
{
cout<<"Name:"<<name<<" model no.: "<<model<<endl;
cout<<"Price: "<<price<<"Purpose: "<<purpose<<"\n"<<endl;
}
class weapon:public product
{
private:
product prods[maxProds];
ifstream infile;
string purpose;
public:
void readProducts();
void run();
};
void weapon::readProducts() {
int count=0;
ifstream infile;
infile.open("C:\\weapons.dat");
while(infile.peek()!=EOF) {
prods[count].readRecord(infile);
count++;
}
infile.close();
for(int i=0;i<count;i++)
prods[i].displayDetails();
}
//-------------------------------------------------------------------------------------------
int main(){
weapon w;
cout<<"This is from the class"<<endl;
w.readProducts();
system("pause");
return 0;
}
in my code i added purpose into the base class. However after re-reading the question, it actually asks me to include the purpose string into the derived class(weapon). the derived class is responsible for purpose string and the readProducts bit. How would i go about doing it?
The base class is asked to read or display the inherited data members and add code to read or display the additional data member.
how would i go about achieving this? any help is appreciated.