Please find the code below
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
using namespace std;
class Person
{
public:
Person() {
setData("unknown-first", "unknown-last");
}
Person(string first, string last) {
setData(first, last);
}
void setData(string first, string last) {
firstName = first;
lastName = last;
}
void printData() const {
cout << "\nName: " << firstName << " " <<
lastName << endl;
}
private:
string firstName;
string lastName;
};
class Musician : public Person
{
public:
Musician() {
// TODO: set this derived class member
// variable instrument to "unknown-instrument". The base class
content will be
// initialized according to the base class's default
constructor.
instrument = "unknown-instrument";
}
Musician(string first, string last, string inst) : Person(first,
last)
// TODO: Before the opening brace.
//construct the base class using the base class parameterized
// constructor. Pass the base class content (first, last)
// up to the base class.
{
// After the opening brace, set the one
// derived class member variable instrument according to the
corresponding
// parameter.
instrument = inst;
}
void setData(string first, string last, string inst) {
// TODO: The base class and derived classes both have setData
functions.
// The derived class setData function overrides the base class one.
However,
// we can invoke the base class version of the function here if we
scope to it.
// Since the member variables first, last are stored
// in the base class, use the base class's setData function to set
those
// values according to the corresponding parameters.
// Then set the remaining derived class variable.
Person::setData(first, last);
instrument = inst;
}
void printData() const {
Person::printData();
cout << "Instrument: " << instrument <<
endl;
}
private:
string instrument;
};
class Writer : public Person
{
public:
Writer() {
// TODO: set this derived class member
// variable genre to "unknown-genre". The base class content will
be
// initialized according to the base class's default
constructor.
genre = "unknown-genre";
}
Writer(string first, string last, string gen) : Person(first,
last)
// TODO: Before the opening brace.
//construct the base class using the base class parameterized
// constructor. Pass the base class content (first, last)
// up to the base class.
{
// After the opening brace, set the one
// derived class member variable genre according to the
corresponding
// parameter.
genre = gen;
}
void setData(string first, string last, string gen) {
// TODO: The base class and derived classes both have setData
functions.
// The derived class setData function overrides the base class one.
However,
// we can invoke the base class version of the function here if we
scope to it.
// Since the member variables first, last are stored
// in the base class, use the base class's setData function to set
those
// values according to the corresponding parameters.
// Then set the remaining derived class variable.
Person::setData(first, last);
genre = gen;
}
void printData() const {
Person::printData();
cout << "Genre: " << genre << endl;
}
private:
string genre;
};
int main() {
int numberOfMusicians = 0;
int numberOfWriters = 0;
string str1, str2, str3;
ifstream fin;
// TODO: Open the file music.txt. Print an error message and
exit
// the program if the file cannot be opened. 5 pts.
fin.open("music.txt");
if (!fin) {
cout << "Unable to music.txt file";
exit(1); // terminate with error
}
// TODO: Read the number at the beginning of the text file. That
number
// indicates the number of musicians in the list (which is also the
length
// of the musicians' array). 5 pts.
fin >> numberOfMusicians;
// Dynamically allocate an array of musician objects. The array
should be
// the exact length needed to read the file. 10 pts.
//
string arrMusicians[numberOfMusicians];
// Use a for-loop to load the array with the content from the text
file. 15 pts.
//
for(int i = 0; i < numberOfMusicians; ++i)
{
fin >> arrMusicians[i];
}
// Close the text file when you are done with it. 5 pts.
fin.close();
// TODO: Open the file writer.txt. Print an error message and
exit
// the program if the file cannot be opened. 5 pts.
fin.open("writer.txt");
if (!fin) {
cout << "Unable to open writer.txt file";
exit(1); // terminate with error
}
// TODO: Read the number at the beginning of the text file. That
number
// indicates the number of writers in the list (which is also the
length
// of the writers' array). 5 pts.
fin >> numberOfWriters;
// Dynamically allocate an array of writer objects. The array
should be
// the exact length needed to read the file. 10 pts.
string arrWriters[numberOfWriters];
// Use a for-loop to load the array with the content from the text
file. 15 pts.
//
for(int i = 0; i < numberOfWriters; ++i)
{
fin >> arrWriters[i];
}
// Close the text file when you are done with it. 5 pts.
fin.close();
// TODO: Loop through the musicians' array and print all of its
data.
for(int i = 0; i < numberOfMusicians; ++i) {
std::string str = arrMusicians[i];
std::string delimiter = "-";
size_t pos = 0;
std::string token;
int j = 0;
while ((pos = str.find(delimiter)) != std::string::npos) {
token = str.substr(0, pos);
if (j==0) {
str1 = token;
}
else if (j==1) {
str2 = token;
}
else {
break;
}
//std::cout << token << std::endl;
str.erase(0, pos + delimiter.length());
j++;
}
str3 = str;
Musician music;
music.setData(str1,str2,str3);
music.printData();
}
// Then loop through the writers' array and print all of its data.
15 pts.
// Remember to release all dynamically allocated memory before
exiting. 5 pts.
for(int i = 0; i < numberOfWriters; ++i) {
std::string str = arrWriters[i];
std::string delimiter = "-";
size_t pos = 0;
std::string token;
int j = 0;
while ((pos = str.find(delimiter)) != std::string::npos) {
token = str.substr(0, pos);
if (j==0) {
str1 = token;
}
else if (j==1) {
str2 = token;
}
else {
break;
}
//std::cout << token << std::endl;
str.erase(0, pos + delimiter.length());
j++;
}
str3 = str;
Writer writer;
writer.setData(str1,str2,str3);
writer.printData();
}
return 0;
}
And the text files I have used below
music.txt
6
musicF-musicL-musicI
musicF1-musicL1-musicI1
musicF2-musicL2-musicI2
musicF3-musicL3-musicI3
musicF4-musicL4-musicI4
musicF5-musicL5-musicI5
writer.txt
5
writerF-writerL-writerG
writerF1-writerL1-writerG1
writerF2-writerL2-writerG2
writerF3-writerL3-writerG3
writerF4-writerL4-writerG4
Answer this in c++ #include <iostream> #include <fstream> #include <string> using namespace std; class Person {...
#include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...
Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass { public: void print() const; baseClass(string s = " ", int a = 0); //Postcondition: str = s; x = a; protected: int x; private: string str; }; class derivedClass: public baseClass { public: void print() const; derivedClass(string s = "", int a = 0, int b = 0); //Postcondition: str = s; x = a; y = b; private: int y; }; int...
So my test.h file has: #include <iostream> #include <fstream> using namespace std; class test { private: int data[]; public: test(string filename); }; My test.cpp file has: #include "sort.h" test::test(string filename) { ifstream inFile; inFile.open(filename); int iter = 0; while(!inFile.eof()) { cin >> data[iter]; iter++; } int numberOfNumbers = iter; for (iter = 0; iter < numberOfNumbers; iter++) cout << data[iter] << " "; ...
employee.h ---------- #include <stdio.h> #include <iostream> #include <fstream> class Employee { private: int employeeNum; std::string name; std::string address; std::string phoneNum; double hrWage, hrWorked; public: Employee(int en, std::string n, std::string a, std::string pn, double hw, double hwo); std::string getName(); void setName(std::string n); int getENum(); std::string getAdd(); void setAdd(std::string a); std::string getPhone(); void setPhone(std::string p); double getWage(); void setWage(double w); double getHours(); void setHours(double h); double calcPay(double a, double b); static Employee read(std::ifstream& in); void write(std::ofstream& out); }; employee.cpp ---------- //employee.cpp #include...
#code for creature.cpp #include <iostream> using namespace std; class Creature { public: Creature(); void run() const; protected: int distance; }; Creature::Creature(): distance(10) {} void Creature::run() const { cout << "running " << distance << " meters!\n"; } class Wizard : public Creature { public: Wizard(); void hover() const; private: int distFactor; }; Wizard::Wizard() : distFactor(3) {} void Wizard::hover() const { cout << "hovering " << (distFactor * distance) << " meters!\n"; } //Created new derived class from Creature class Widget...
USING THIS CODE: #include <iostream> #include <string> using namespace std; class Contact { //this class cannot be viewed from outside of the private class. //only the class functions within the class can access private members. private: string name; string email; string phone; //the public class is accessible from anywhere outside of the class //however can only be within the program. public: string getName() const { return name; } void setName(string name) { this->name = name; } string getEmail() const {...
#include <iostream> #include <string> #include <cstring> using namespace std; class Node{ private: int data; Node* nextNodePtr; public: Node(){} void setData(int d){ data = d; } int getData(){ return data; } void setNextNodePtr(Node* nodePtr){ nextNodePtr = nodePtr; } ...
in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> // for string tokenizer and c-style string processing #include <algorithm> // max function #include <stdlib.h> #include <time.h> using namespace std; // Extend the code here as needed class BTNode{ private: int nodeid; int data; int levelNum; BTNode* leftChildPtr; BTNode* rightChildPtr; public: BTNode(){} void setNodeId(int id){ nodeid = id; } int getNodeId(){ return nodeid; } void setData(int d){ data = d; } int getData(){ return data;...
//Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected: string make; int year; double mpg; Warranty warranty; static int numOfVehicles; public: Vehicle(); Vehicle(string s, int y, double m, Warranty warranty); Vehicle(Vehicle& v); ~Vehicle(); string getMake(); int getYear(); double getGasMileage(); void setMake(string s); void setYear(int y); void setYear(string y); void setGasMileage(double m); void setGasMileage(string m); void displayVehicle(); static int getNumVehicles(); Warranty getWarranty(); void setWarranty(Warranty& ); }; //Vehicle.cpp #include "Vehicle.h" #include <string> Vehicle::Vehicle() { make = "unknown"; year =...
#include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...