SHARE
TWEET

Untitled

a guest Mar 26th, 2020 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Working code implemented in C++ and comments for better understanding:
  2.  
  3. Here I am attaching these 7 files:
  4.  
  5. main.cpp
  6. merchant.cpp
  7. merchant.h
  8. antique.cpp
  9. antique.h
  10. MerchantGuild.cpp
  11. MerchantGuild.h
  12. Code for main.cpp
  13.  
  14. #include <iostream>
  15. #include "MerchantGuild.h"
  16. #include "merchant.h"
  17. #include "antique.h"
  18. using namespace std;
  19.  
  20. int main() {
  21.  
  22. cout << "KEY: " << "True is: " << true << " False is: " << false << endl << endl;
  23.  
  24. //antique tests
  25.    Antique a1, a2;
  26.  
  27.    a1.setName("fork");
  28.    a1.setPrice(3.25);
  29.    a2.setName("knife");
  30.    a2.setPrice(2.50);
  31.    cout << "antique test" << endl;
  32.    Antique a3 = a1 + a2;
  33.  
  34.    cout << a3.toString() << endl;
  35.    cout << bool(a1 == a2) << " : Ans=0" << endl;
  36.    cout << bool(a1 == a1) << " : Ans=1" << endl;
  37.  
  38.    //merchant tests
  39.    Merchant m1(1.2), m2(2.5);
  40.    cout << "merchant test" << endl;
  41.    m1.addAntique(a1, 2);
  42.    m1.addAntique(a2, 5);
  43.    m2.addAntique(a3, 3);
  44.  
  45.    cout << bool(m1 == m2) << " : Ans=0" << endl;
  46.    cout << bool(m1 == m1) << " : Ans=1" << endl;
  47.  
  48.    Merchant m3(m1);
  49.    cout << bool(m3 == m1) << " : Ans=1" << endl;
  50.  
  51.    //merchant guild tests
  52.    MerchantGuild mg1;
  53.    cout << "merchant guild tests" << endl;
  54.  
  55.    mg1.addMember(m1);
  56.  
  57.    Merchant* tmp = mg1.getMembers();
  58.    cout << bool(tmp[0] == m1) << " : Ans=1" << endl;
  59.  
  60.    mg1.addMember(m2);
  61.  
  62.    tmp = mg1.getMembers();
  63.    cout << bool(tmp[0] == m1 && tmp[1] == m2) << " : Ans=1" << endl;
  64.  
  65. return 0;
  66. }
  67.  
  68. Code Screenshots:
  69.  
  70. #include <iostream> #include MerchantGuild.h #include merchant.h #include antique.h using namespace std; int main() { c
  71.  
  72. Code for merchant.cpp
  73.  
  74. #include "merchant.h"
  75.  
  76. using namespace std;
  77.  
  78. //Default constructor here: the default constructor will initialize things to zero or nullptr
  79. Merchant::Merchant()
  80. {
  81.    revenue = 0.0;
  82.    antiqueList = nullptr;
  83.    numAnt = nullptr;
  84.    size = 0;
  85. }
  86.  
  87. //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
  88. Merchant::Merchant(float r)
  89. {
  90.    revenue = r;
  91.    antiqueList = nullptr;
  92.    numAnt = nullptr;
  93.    size = 0;
  94. }
  95.  
  96. //Destructor here: the destructor will free all allocated memory (delete pointers and delete[ ] dynamic arrays)
  97. Merchant::~Merchant()
  98. {
  99.    delete[] antiqueList;
  100.    delete[] numAnt;
  101.  
  102.    antiqueList = nullptr;
  103.    numAnt = nullptr;
  104. }
  105.  
  106. //Copy constructor here: the copy constructor will perform a deep copy
  107. Merchant::Merchant(const Merchant &copy)
  108. {
  109.    size = copy.size;
  110.    revenue = copy.revenue;
  111.  
  112.    antiqueList = new Antique[size];
  113.    numAnt = new int[size];
  114.    for (int i = 0; i < size; i++)
  115.    {
  116.        antiqueList[i] = copy.antiqueList[i];
  117.        numAnt[i] = copy.numAnt[i];
  118.    }
  119. }
  120.  
  121. /*
  122. *Overload the assignment operator (=) here: this will work almost exactly like the copy constructor
  123. *Except, there may already be allocated memory in the pointers you must delete first
  124. */
  125. Merchant Merchant::operator=(const Merchant &copy)
  126. {
  127.    size = copy.size;
  128.    revenue = copy.revenue;
  129.  
  130.    if (this != &copy) { // 1. Don't self-assign
  131.        delete[] antiqueList; // 2. Delete old memory
  132.        delete[] numAnt;
  133.        antiqueList = new Antique[size]; // 3. Allocate new memory
  134.        numAnt = new int[size];
  135.        for (int i = 0; i < size; i++)
  136.        {
  137.            antiqueList[i] = copy.antiqueList[i];
  138.            numAnt[i] = copy.numAnt[i];
  139.        }
  140.    }
  141.    return *this;
  142. }
  143.  
  144. /*
  145. *Overload operator (==) here:
  146. *One merchant is equal to another if:
  147. *Their revenue is equal
  148. *They have the same number of antiques
  149. *The antiques are the same and in the same order
  150. *They have the same quantity of each antique
  151. */
  152. bool Merchant::operator==(const Merchant &other)
  153. {
  154.    if (fabs(revenue - other.revenue) < 0.0001)
  155.    {
  156.        if (size == other.size)
  157.        {
  158.            for (int i = 0; i < size; i++)
  159.            {
  160.                if ((antiqueList[i].getName().compare(other.antiqueList[i].getName()) != 0) || (antiqueList[i].getPrice() != other.antiqueList[i].getPrice()) || (numAnt[i] != other.numAnt[i]))
  161.                {
  162.                    return false;
  163.                }
  164.            }
  165.            return true;
  166.        }
  167.        else
  168.        {
  169.            return false;
  170.        }
  171.    }
  172.    else
  173.    {
  174.        return false;
  175.    }
  176. }
  177.  
  178. /*
  179. *AddAntique here: this is a void function that takes as arguments an antique object and an int quantity of that antique.
  180. *It will add that antique and its quantity to the end of the respective dynamic arrays.
  181. *The arrays will be 1 size larger after the addition.
  182. */
  183. void Merchant::addAntique(Antique newAnt, int newQuan)
  184. {
  185.    if (size == 0)
  186.    {
  187.        size++;
  188.        antiqueList = new Antique[size];
  189.        numAnt = new int[size];
  190.  
  191.        antiqueList[0] = newAnt;
  192.        numAnt[0] = newQuan;
  193.    }
  194.    else
  195.    {
  196.        Antique *tempAnt = new Antique[size];
  197.        int *tempQuan = new int[size];
  198.  
  199.        for (int i = 0; i < size; i++)
  200.        {
  201.            tempAnt[i] = antiqueList[i];
  202.            tempQuan[i] = numAnt[i];
  203.        }
  204.  
  205.        delete[] antiqueList;
  206.        delete[] numAnt;
  207.  
  208.        size++;
  209.  
  210.        antiqueList = new Antique[size];
  211.        numAnt = new int[size];
  212.  
  213.        for (int i = 0; i < size - 1; i++)
  214.        {
  215.            antiqueList[i] = tempAnt[i];
  216.            numAnt[i] = tempQuan[i];
  217.        }
  218.  
  219.        antiqueList[size - 1] = newAnt;
  220.        numAnt[size - 1] = newQuan;
  221.  
  222.        delete[] tempAnt;
  223.        delete[] tempQuan;
  224.    }  
  225. }
  226.  
  227. Code Screenshots:
  228.  
  229. #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
  230.  
  231. Code for merchant.h
  232.  
  233.  
  234. #include "antique.h"
  235. #include <iostream>
  236. #include <fstream>
  237. #include <cmath>
  238.  
  239. using namespace std;
  240.  
  241. #ifndef merchant_h
  242. #define merchant_h
  243.  
  244. class Merchant {
  245. private:
  246.    Antique *antiqueList;
  247.    int *numAnt;
  248. float revenue;
  249.    int size;
  250. public:
  251.    Merchant();
  252.    Merchant(float r);
  253.    ~Merchant();
  254.    Merchant(const Merchant &copy);
  255.    Merchant operator=(const Merchant &copy);
  256.    bool operator==(const Merchant &other);
  257.    void addAntique(Antique newAnt, int newQuan);
  258. };
  259.  
  260.  
  261. #endif /* merchant_h */
  262.  
  263. Code Screenshots:
  264.  
  265. 2 #include antique.h #include <iostream> #include <fstream> #include <cmath> 6 using namespace std; 10 #ifndef merchant_h #
  266.  
  267. Code for antique.cpp
  268.  
  269. #include "antique.h"
  270.  
  271. using namespace std;
  272.  
  273. void Antique::setName(string NAME)
  274. {
  275.    name = NAME;
  276. }
  277.  
  278. void Antique::setPrice(float PRICE)
  279. {
  280.    price = PRICE;
  281. }
  282.  
  283. string Antique::getName() const
  284. {
  285.    return name;
  286. }
  287.  
  288. float Antique::getPrice() const
  289. {
  290.    return price;
  291. }
  292.  
  293. string Antique::toString()
  294. {
  295.    //string price_s;
  296.    ostringstream streamObj;
  297.  
  298.    streamObj << fixed;
  299.    streamObj << setprecision(2);
  300.  
  301.    streamObj << price;
  302.    return name + ": $" + streamObj.str();
  303. }
  304.  
  305. Antique::Antique()
  306. {
  307.    name = "";
  308.    price = 0;
  309. }
  310.  
  311. bool Antique::operator==(const Antique &other)
  312. {
  313.    bool isSame = false;
  314.  
  315.    //Using compare() to compare two strings
  316.    if ((price == other.price) && (name.compare(other.name) == 0))
  317.    {
  318.        isSame = true;
  319.    }
  320.    return isSame;
  321. }
  322.  
  323. Antique Antique::operator+(const Antique &other)
  324. {
  325.    Antique antiqueTotal;
  326.  
  327.    antiqueTotal.price = price + other.price;
  328.  
  329.    //Creating a string stream
  330.    ostringstream outSS;
  331.  
  332.    outSS << name << " and " << other.name;
  333.    antiqueTotal.name = outSS.str();
  334.  
  335.    return antiqueTotal;
  336. }
  337.  
  338. Code Screenshots:
  339.  
  340. #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
  341.  
  342. Code for antique.h
  343.  
  344. #include <iostream>
  345. #include <string>
  346. #include <sstream>
  347. #include <iomanip>
  348.  
  349. using namespace std;
  350.  
  351. #ifndef antique_h
  352. #define antique_h
  353.  
  354. class Antique {
  355. private:
  356. string name;
  357. float price;
  358. public:
  359. Antique();
  360.    bool operator==(const Antique &other);
  361.    Antique operator+(const Antique &other);
  362. string toString();
  363. void setName(string NAME);
  364. void setPrice(float PRICE);
  365. string getName() const;
  366. float getPrice() const;
  367. };
  368.  
  369. #endif /* antique_h */
  370.  
  371. Code Screenshots:
  372.  
  373. 1 #include <iostream> #include <string> #include <sstream> #include <iomanip> using namespace std; 13 #ifndef antique_h #defi
  374.  
  375. Code for MerchantGuild.cpp
  376.  
  377. #include "MerchantGuild.h"
  378.  
  379. using namespace std;
  380.  
  381. /*
  382. *Constructor with parameter size (with default value 10):
  383. *Sets guildSize to size, allocates members array and sets numMem to 0.
  384. *If size < 1, use default value 10
  385. */
  386. MerchantGuild::MerchantGuild(int size)
  387. {
  388. if (size < 1)
  389. {
  390. size = 10;
  391. }
  392. guildSize = size;
  393.  
  394. members = new Merchant[guildSize];
  395. numMem = 0;
  396. }
  397.  
  398. //Copy constructor: perform a deep copy
  399. MerchantGuild::MerchantGuild(const MerchantGuild &copy)
  400. {
  401. guildSize = copy.guildSize;
  402. numMem = copy.numMem;
  403.  
  404. members = new Merchant[guildSize];
  405. for (int i = 0; i < guildSize; i++)
  406. {
  407. members[i] = copy.members[i];
  408. }
  409. }
  410.  
  411. //Destructor: free the allocated memory
  412. MerchantGuild::~MerchantGuild()
  413. {
  414. delete[] members;
  415. members = nullptr;
  416. }
  417.  
  418. //Overloads the assignment operator (=): this works like the copy constructor except there is allocated memory you have to delete first
  419. MerchantGuild& MerchantGuild::operator=(const MerchantGuild &other)
  420. {
  421. guildSize = other.guildSize;
  422. numMem = other.numMem;
  423.  
  424. if (this != &other)
  425. {
  426. delete[] members;
  427. members = new Merchant[guildSize];
  428. for (int i = 0; i < guildSize; i++)
  429. {
  430. members[i] = other.members[i];
  431. }
  432. }
  433.  
  434. return *this;
  435. }
  436.  
  437. /*
  438. *addMember:
  439. *This is a void function that takes as an argument a Merchant object,
  440. *adds it to the end of its array of members, and increases it the number of members by 1.
  441. *If the array is full(numMem == guildSize), do not add the new memberand print "Guild is full."
  442. */
  443. void MerchantGuild::addMember(Merchant newM)
  444. {
  445. if (numMem == guildSize)
  446. {
  447. cout << "Guild is full." << endl;
  448. }
  449. else
  450. {
  451. members[numMem] = newM;
  452. numMem++;
  453. }
  454. }
  455.  
  456. //getMembers: returns the pointer to the dynamic array
  457. Merchant* MerchantGuild::getMembers()
  458. {
  459. return members;
  460. }
  461.  
  462. Code Screenshots:
  463.  
  464. 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
  465.  
  466. Code for MerchantGuild.h
  467.  
  468. #include "merchant.h"
  469. #include <iostream>
  470.  
  471.  
  472. using namespace std;
  473.  
  474. #ifndef MerchantGuild_h
  475. #define MerchantGuild_h
  476.  
  477. class MerchantGuild
  478. {
  479. public:
  480.    MerchantGuild(int size = 10);
  481.    MerchantGuild(const MerchantGuild &copy);
  482.    ~MerchantGuild();
  483.    MerchantGuild& operator=(const MerchantGuild &other);
  484.    void addMember(Merchant newM);
  485.    Merchant* getMembers();
  486. private:
  487.    Merchant *members;
  488.    int guildSize;
  489.    int numMem;
  490. };
  491.  
  492. #endif
  493.  
  494. Code Screenshots:
  495.  
  496. HNM #include merchant.h #include <iostream> L using namespace std; #ifndef MerchantGuild_h #define MerchantGuild h 00 class
  497.  
  498. Output Screenshots:
  499.  
  500. clang version 7.0.0-3-ubuntu0.18.04.1 (tags/RELEASE 700/final) > clang++-7 -pthread -o main MerchantGuild.cpp antique.cpp mai
  501.  
  502. Hope it helps, if you found the answer helpful, give it thumbs up. Thank you.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top