Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- using namespace std;
- // Base class for houses
- class House {
- protected:
- string address;
- public:
- House() {}
- House(string addr) : address(addr) {}
- virtual void setFields(string input) {
- cout << address;
- }
- virtual void printInfo() = 0;
- virtual string getType() = 0;
- virtual int getNumResidents() = 0;
- string getAddress() { return address; }
- void setAddress(string addr) { address = addr; }
- };
- // Residential House class
- class ResidentialHouse : public House {
- private:
- int numResidents;
- public:
- ResidentialHouse() {}
- ResidentialHouse(string addr, int residents) : House(addr), numResidents(residents) {}
- void setFields(int input) {
- numResidents = input;
- }
- int getNumResidents() {
- return numResidents;
- }
- void printInfo() {
- cout << endl;
- cout << "Address: " << address << endl;
- cout << "Type: Residential House" << endl;
- cout << "Number of Residents: " << numResidents << endl;
- }
- string getType() { return "Residential House"; }
- };
- // Store class
- class Store : public House {
- private:
- string storeType;
- public:
- Store() {}
- Store(string addr, string type) : House(addr), storeType(type) {}
- void setFields(string input) override {
- House::setFields(input);
- storeType = input;
- }
- int getNumResidents() {
- return NULL;
- }
- void printInfo() {
- cout << endl;
- cout << "Address: " << address << endl;
- cout << "Type: Store" << endl;
- cout << "Store Type: " << storeType << endl;
- }
- string getType() { return "Store"; }
- };
- // School class
- class School : public House {
- private:
- int numStudents;
- string level;
- public:
- School() {}
- School(string addr, int students, string lvl) : House(addr), numStudents(students), level(lvl) {}
- void setFields(string input) {
- level = input;
- }
- int getNumResidents() {
- return numStudents;
- }
- void printInfo() {
- cout << endl;
- cout << "Address: " << address << endl;
- cout << "Type: School" << endl;
- cout << "Number of Students: " << numStudents << endl;
- cout << "Level: " << level << endl;
- }
- string getType() { return "School"; }
- };
- // Hospital class
- class Hospital : public House {
- private:
- string hospitalType;
- public:
- Hospital() {}
- Hospital(string addr, string type) : House(addr), hospitalType(type) {}
- void setFields(string input) {
- hospitalType = input;
- }
- int getNumResidents() {
- return NULL;
- }
- void printInfo() {
- cout << endl;
- cout << "Address: " << address << endl;
- cout << "Type: Hospital" << endl;
- cout << "Hospital Type: " << hospitalType << endl;
- }
- string getType() { return "Hospital"; }
- };
- class Street {
- private:
- vector<House*> houses;
- public:
- Street() {}
- void addHouse(House* house) {
- houses.push_back(house);
- }
- void printHouses() {
- for (auto house : houses) {
- house->printInfo();
- }
- }
- void writeToFile(string fileName) {
- ofstream outfile;
- outfile.open(fileName);
- if (!outfile) {
- cerr << "Unable to open file " << fileName << endl;
- exit(1);
- }
- for (auto house : houses) {
- outfile << house->getType() << endl;
- outfile << house->getAddress() << endl;
- if (house->getType() != "Hospital" && house->getType() != "Store")
- outfile << house->getNumResidents() << endl;
- }
- outfile.close();
- }
- void readFromFile(string fileName) {
- ifstream infile;
- infile.open(fileName);
- if (!infile) {
- cerr << "Unable to open file " << fileName << endl;
- exit(1); // call system to stop
- }
- string type, address, data;
- while (getline(infile, type)) {
- getline(infile, address);
- if (type != "Hospital" && type != "Store") {
- getline(infile, data);
- }
- else {
- data = "";
- }
- House* house;
- if (type == "Residential House") {
- int numResidents = stoi(data);
- house = new ResidentialHouse(address, numResidents);
- }
- else if (type == "Store") {
- string storeType = data;
- house = new Store(address, storeType);
- }
- else if (type == "School") {
- string level = data;
- int numStudents = stoi(level);
- house = new School(address, numStudents, level);
- }
- else if (type == "Hospital") {
- string hospitalType = data;
- house = new Hospital(address, hospitalType);
- }
- else {
- cerr << "Unknown house type: " << type << endl;
- continue;
- }
- addHouse(house);
- }
- infile.close();
- }
- };
- int main() {
- Store shop("456 Maple Ave", "grocery");
- Hospital hospital("789 Oak St", "general");
- School school("101 Elm St", 800, "lyceum");
- ResidentialHouse resH("65 Maple Ave", 456);
- Street street;
- street.addHouse(&shop);
- street.addHouse(&hospital);
- street.addHouse(&school);
- street.addHouse(&resH);
- street.printHouses();
- street.writeToFile("file.txt");
- street.readFromFile("file.txt");
- street.printHouses();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment