Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Working code implemented in C++ and comments for better understanding:
- Here I am attaching these 7 files:
- main.cpp
- merchant.cpp
- merchant.h
- antique.cpp
- antique.h
- MerchantGuild.cpp
- MerchantGuild.h
- Code for main.cpp
- #include <iostream>
- #include "MerchantGuild.h"
- #include "merchant.h"
- #include "antique.h"
- using namespace std;
- int main() {
- cout << "KEY: " << "True is: " << true << " False is: " << false << endl << endl;
- //antique tests
- Antique a1, a2;
- a1.setName("fork");
- a1.setPrice(3.25);
- a2.setName("knife");
- a2.setPrice(2.50);
- cout << "antique test" << endl;
- Antique a3 = a1 + a2;
- cout << a3.toString() << endl;
- cout << bool(a1 == a2) << " : Ans=0" << endl;
- cout << bool(a1 == a1) << " : Ans=1" << endl;
- //merchant tests
- Merchant m1(1.2), m2(2.5);
- cout << "merchant test" << endl;
- m1.addAntique(a1, 2);
- m1.addAntique(a2, 5);
- m2.addAntique(a3, 3);
- cout << bool(m1 == m2) << " : Ans=0" << endl;
- cout << bool(m1 == m1) << " : Ans=1" << endl;
- Merchant m3(m1);
- cout << bool(m3 == m1) << " : Ans=1" << endl;
- //merchant guild tests
- MerchantGuild mg1;
- cout << "merchant guild tests" << endl;
- mg1.addMember(m1);
- Merchant* tmp = mg1.getMembers();
- cout << bool(tmp[0] == m1) << " : Ans=1" << endl;
- mg1.addMember(m2);
- tmp = mg1.getMembers();
- cout << bool(tmp[0] == m1 && tmp[1] == m2) << " : Ans=1" << endl;
- return 0;
- }
- Code Screenshots:
- #include <iostream> #include MerchantGuild.h #include merchant.h #include antique.h using namespace std; int main() { c
- Code for merchant.cpp
- #include "merchant.h"
- using namespace std;
- //Default constructor here: the default constructor will initialize things to zero or nullptr
- Merchant::Merchant()
- {
- revenue = 0.0;
- antiqueList = nullptr;
- numAnt = nullptr;
- size = 0;
- }
- //Constructor with only a float value here: the constructor with one value will initialize revenue to the float and everything else to zero or nullptr
- Merchant::Merchant(float r)
- {
- revenue = r;
- antiqueList = nullptr;
- numAnt = nullptr;
- size = 0;
- }
- //Destructor here: the destructor will free all allocated memory (delete pointers and delete[ ] dynamic arrays)
- Merchant::~Merchant()
- {
- delete[] antiqueList;
- delete[] numAnt;
- antiqueList = nullptr;
- numAnt = nullptr;
- }
- //Copy constructor here: the copy constructor will perform a deep copy
- Merchant::Merchant(const Merchant ©)
- {
- size = copy.size;
- revenue = copy.revenue;
- antiqueList = new Antique[size];
- numAnt = new int[size];
- for (int i = 0; i < size; i++)
- {
- antiqueList[i] = copy.antiqueList[i];
- numAnt[i] = copy.numAnt[i];
- }
- }
- /*
- *Overload the assignment operator (=) here: this will work almost exactly like the copy constructor
- *Except, there may already be allocated memory in the pointers you must delete first
- */
- Merchant Merchant::operator=(const Merchant ©)
- {
- size = copy.size;
- revenue = copy.revenue;
- if (this != ©) { // 1. Don't self-assign
- delete[] antiqueList; // 2. Delete old memory
- delete[] numAnt;
- antiqueList = new Antique[size]; // 3. Allocate new memory
- numAnt = new int[size];
- for (int i = 0; i < size; i++)
- {
- antiqueList[i] = copy.antiqueList[i];
- numAnt[i] = copy.numAnt[i];
- }
- }
- return *this;
- }
- /*
- *Overload operator (==) here:
- *One merchant is equal to another if:
- *Their revenue is equal
- *They have the same number of antiques
- *The antiques are the same and in the same order
- *They have the same quantity of each antique
- */
- bool Merchant::operator==(const Merchant &other)
- {
- if (fabs(revenue - other.revenue) < 0.0001)
- {
- if (size == other.size)
- {
- for (int i = 0; i < size; i++)
- {
- if ((antiqueList[i].getName().compare(other.antiqueList[i].getName()) != 0) || (antiqueList[i].getPrice() != other.antiqueList[i].getPrice()) || (numAnt[i] != other.numAnt[i]))
- {
- return false;
- }
- }
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- /*
- *AddAntique here: this is a void function that takes as arguments an antique object and an int quantity of that antique.
- *It will add that antique and its quantity to the end of the respective dynamic arrays.
- *The arrays will be 1 size larger after the addition.
- */
- void Merchant::addAntique(Antique newAnt, int newQuan)
- {
- if (size == 0)
- {
- size++;
- antiqueList = new Antique[size];
- numAnt = new int[size];
- antiqueList[0] = newAnt;
- numAnt[0] = newQuan;
- }
- else
- {
- Antique *tempAnt = new Antique[size];
- int *tempQuan = new int[size];
- for (int i = 0; i < size; i++)
- {
- tempAnt[i] = antiqueList[i];
- tempQuan[i] = numAnt[i];
- }
- delete[] antiqueList;
- delete[] numAnt;
- size++;
- antiqueList = new Antique[size];
- numAnt = new int[size];
- for (int i = 0; i < size - 1; i++)
- {
- antiqueList[i] = tempAnt[i];
- numAnt[i] = tempQuan[i];
- }
- antiqueList[size - 1] = newAnt;
- numAnt[size - 1] = newQuan;
- delete[] tempAnt;
- delete[] tempQuan;
- }
- }
- Code Screenshots:
- #include merchant.h HNM using namespace std; //Default constructor here: the default constructor will initialize things tonumАnt = new int[size]; for (int i = 0; i < size; i++) ୫ ଓ ୫ ୫ ? ୫ ଓ ୧୯ antiqueList[i] = copy.antiqueList[i]; numAnt[i] = copelse 123 124 Antique *tempAnt = new Antique[size]; int *tempQuan = new int[size]; 125 for (int i = 0; i < size; i++) 126 127
- Code for merchant.h
- #include "antique.h"
- #include <iostream>
- #include <fstream>
- #include <cmath>
- using namespace std;
- #ifndef merchant_h
- #define merchant_h
- class Merchant {
- private:
- Antique *antiqueList;
- int *numAnt;
- float revenue;
- int size;
- public:
- Merchant();
- Merchant(float r);
- ~Merchant();
- Merchant(const Merchant ©);
- Merchant operator=(const Merchant ©);
- bool operator==(const Merchant &other);
- void addAntique(Antique newAnt, int newQuan);
- };
- #endif /* merchant_h */
- Code Screenshots:
- 2 #include antique.h #include <iostream> #include <fstream> #include <cmath> 6 using namespace std; 10 #ifndef merchant_h #
- Code for antique.cpp
- #include "antique.h"
- using namespace std;
- void Antique::setName(string NAME)
- {
- name = NAME;
- }
- void Antique::setPrice(float PRICE)
- {
- price = PRICE;
- }
- string Antique::getName() const
- {
- return name;
- }
- float Antique::getPrice() const
- {
- return price;
- }
- string Antique::toString()
- {
- //string price_s;
- ostringstream streamObj;
- streamObj << fixed;
- streamObj << setprecision(2);
- streamObj << price;
- return name + ": $" + streamObj.str();
- }
- Antique::Antique()
- {
- name = "";
- price = 0;
- }
- bool Antique::operator==(const Antique &other)
- {
- bool isSame = false;
- //Using compare() to compare two strings
- if ((price == other.price) && (name.compare(other.name) == 0))
- {
- isSame = true;
- }
- return isSame;
- }
- Antique Antique::operator+(const Antique &other)
- {
- Antique antiqueTotal;
- antiqueTotal.price = price + other.price;
- //Creating a string stream
- ostringstream outSS;
- outSS << name << " and " << other.name;
- antiqueTotal.name = outSS.str();
- return antiqueTotal;
- }
- Code Screenshots:
- #include antique.h using namespace std; void Antique::setName(string NAME) name = NAME; void Antique::setPrice(float PRICE)1/Creating a string stream ostringstream outss; outss << name << and << other.name; antiqueTotal.name = outss.str(); retu
- Code for antique.h
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <iomanip>
- using namespace std;
- #ifndef antique_h
- #define antique_h
- class Antique {
- private:
- string name;
- float price;
- public:
- Antique();
- bool operator==(const Antique &other);
- Antique operator+(const Antique &other);
- string toString();
- void setName(string NAME);
- void setPrice(float PRICE);
- string getName() const;
- float getPrice() const;
- };
- #endif /* antique_h */
- Code Screenshots:
- 1 #include <iostream> #include <string> #include <sstream> #include <iomanip> using namespace std; 13 #ifndef antique_h #defi
- Code for MerchantGuild.cpp
- #include "MerchantGuild.h"
- using namespace std;
- /*
- *Constructor with parameter size (with default value 10):
- *Sets guildSize to size, allocates members array and sets numMem to 0.
- *If size < 1, use default value 10
- */
- MerchantGuild::MerchantGuild(int size)
- {
- if (size < 1)
- {
- size = 10;
- }
- guildSize = size;
- members = new Merchant[guildSize];
- numMem = 0;
- }
- //Copy constructor: perform a deep copy
- MerchantGuild::MerchantGuild(const MerchantGuild ©)
- {
- guildSize = copy.guildSize;
- numMem = copy.numMem;
- members = new Merchant[guildSize];
- for (int i = 0; i < guildSize; i++)
- {
- members[i] = copy.members[i];
- }
- }
- //Destructor: free the allocated memory
- MerchantGuild::~MerchantGuild()
- {
- delete[] members;
- members = nullptr;
- }
- //Overloads the assignment operator (=): this works like the copy constructor except there is allocated memory you have to delete first
- MerchantGuild& MerchantGuild::operator=(const MerchantGuild &other)
- {
- guildSize = other.guildSize;
- numMem = other.numMem;
- if (this != &other)
- {
- delete[] members;
- members = new Merchant[guildSize];
- for (int i = 0; i < guildSize; i++)
- {
- members[i] = other.members[i];
- }
- }
- return *this;
- }
- /*
- *addMember:
- *This is a void function that takes as an argument a Merchant object,
- *adds it to the end of its array of members, and increases it the number of members by 1.
- *If the array is full(numMem == guildSize), do not add the new memberand print "Guild is full."
- */
- void MerchantGuild::addMember(Merchant newM)
- {
- if (numMem == guildSize)
- {
- cout << "Guild is full." << endl;
- }
- else
- {
- members[numMem] = newM;
- numMem++;
- }
- }
- //getMembers: returns the pointer to the dynamic array
- Merchant* MerchantGuild::getMembers()
- {
- return members;
- }
- Code Screenshots:
- 1 include MerchantGuild.h 3 using namespace std; 5 6 *Constructor with parameter size (with default value 10): | *Sets guil*addMember: *This is a void function that takes as an argument a Merchant object, *adds it to the end of its array of members
- Code for MerchantGuild.h
- #include "merchant.h"
- #include <iostream>
- using namespace std;
- #ifndef MerchantGuild_h
- #define MerchantGuild_h
- class MerchantGuild
- {
- public:
- MerchantGuild(int size = 10);
- MerchantGuild(const MerchantGuild ©);
- ~MerchantGuild();
- MerchantGuild& operator=(const MerchantGuild &other);
- void addMember(Merchant newM);
- Merchant* getMembers();
- private:
- Merchant *members;
- int guildSize;
- int numMem;
- };
- #endif
- Code Screenshots:
- HNM #include merchant.h #include <iostream> L using namespace std; #ifndef MerchantGuild_h #define MerchantGuild h 00 class
- Output Screenshots:
- clang version 7.0.0-3-ubuntu0.18.04.1 (tags/RELEASE 700/final) > clang++-7 -pthread -o main MerchantGuild.cpp antique.cpp mai
- Hope it helps, if you found the answer helpful, give it thumbs up. Thank you.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement