Question

C++ please! OVERVIEW This homework builds on HW4. We will modify the classes to do a...

C++ please!

OVERVIEW

This homework builds on HW4. We will modify the classes to do a few new things

  • We will add copy constructors to the Antique and Merchant classes.
  • We will overload the == operator for Antique and Merchant classes.
  • We will overload the + operator for the antique class.
  • We will add a new class the "MerchantGuild."

Please use the provided templates for the Antique and Merchant classes, as we simplified their functionality for this homework. Also, you should use the provided main.cpp to test your classes.

Antique Class

  • Overload the == operator
    • one antique is equal to another if the name and price are the same
  • Overload the + operator
    • sometimes you want to sell antiques together, such as fork and knife pairs
Antique fork;
Antique knife;
fork.setName("fork");
fork.setPrice(3.25);
knife.setName("knife");
knife.setPrice(2.25);
Antique Silverware = fork + knife;
  • Silverware.name = "fork and knife"
  • Silverware.price = 5.50

Merchant Class

  • modify the class you implemented for Homework 4 to use dynamic arrays for holding antiques and their quantities
    • the default constructor will initialize things to zero or nullptr
    • the copy constructor will perform a deep copy
    • the constructor with one value will initialize revenue to the float and everything else to zero or nullptr
    • the destructor will free all allocated memory (delete pointers and delete[ ] dynamic arrays)
  • Overload the assignment operator ( = )
    • this will work almost exactly like the copy constructor Except, there may already be allocated memory in the pointers you must delete first
  • Overload the == operator
    • 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
  • Add an addAntique function
    • 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.

MerchantGuild Class

This class will hold a dynamic array of merchants of size guildSize. It also keeps track of the number of members it has.

It has the following constructors:

  • constructor with parameter size
    • sets guildSize to size, allocates members array and sets numMem to 0. If size < 1, use default value 10
  • copy constructor
    • this will perform a deep copy

It also has a destructor

  • free the allocated memory

It overloads the assignment operator (=)

  • this works like the copy constructor except there is allocated memory you have to delete first

It has the following member functions:

  • 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 member and print "Guild is full."
  • getMembers

    • returns the pointer to the dynamic array

note: the test cases are in the order of how they should be completed. In other words they build upon the last.

note: The main.cpp can be used for testing, but use the original template to pass the final test case.

IMPORTANT

  • Classes and methods names must match exactly for unit testing to succeed.
  • Submissions with hard coded answers will receive a grade of 0.

_____________________________________________________________________________________

Files for homework 4 are as below:

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;

}

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;

}

/*

   put == operator overload here

*/

/*

   put + operator overload here

*/

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 */

merchant.cpp

#include "merchant.h"

using namespace std;

//default constructor here

//constructor with only a float value

//destructor here

// copy constructor

//==operator overload here

//add antique here

merchant.h

#include "antique.h"

#include <iostream>

#include <fstream>

using namespace std;

#ifndef merchant_h

#define merchant_h

class Merchant {

private:

/*

put the dynamic arrays here

*/

int numAnt;

float revenue;

public:

Merchant();

Merchant(float r);

~Merchant();

Merchant(const Merchant &copy);

/*

== operator overload here

addAntique function here

*/

};

#endif /* merchant_h */

MerchantGuild.cpp

//emapty

MerchantGuild.h

#include "merchant.h"

using namespace std;

#ifndef MerchantGuild_h

#define MerchantGuild_h

class MerchantGuild

{

public:

MerchantGuild();

MerchantGuild(const MerchantGuild &copy);

~MerchantGuild();

MerchantGuild& operator=(const MerchantGuild &other);

void addMember(Merchant newM);

Merchant* getMembers();

private:

Merchant* members;

int guildSize;

int numMem;

};

#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

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 &copy)
{
   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 &copy)
{
   size = copy.size;
   revenue = copy.revenue;

   if (this != &copy) { // 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:

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 &copy);
   Merchant operator=(const Merchant &copy);
   bool operator==(const Merchant &other);
   void addAntique(Antique newAnt, int newQuan);
};


#endif /* merchant_h */

Code Screenshots:

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:

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:

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 &copy)
{
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:

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 &copy);
   ~MerchantGuild();
   MerchantGuild& operator=(const MerchantGuild &other);
   void addMember(Merchant newM);
   Merchant* getMembers();
private:
   Merchant *members;
   int guildSize;
   int numMem;
};

#endif

Code Screenshots:

Output Screenshots:

Hope it helps. Thank you.

Add a comment
Know the answer?
Add Answer to:
C++ please! OVERVIEW This homework builds on HW4. We will modify the classes to do a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN c++ i post this question 5 times. hope this time somebody answer.. 16.5 Homework 5...

    IN c++ i post this question 5 times. hope this time somebody answer.. 16.5 Homework 5 OVERVIEW This homework builds on Hw4(given in bottom) We will modify the classes to do a few new things We will add copy constructors to the Antique and Merchant classes. We will overload the == operator for Antique and Merchant classes. We will overload the + operator for the antique class. We will add a new class the "MerchantGuild." Please use the provided templates...

  • You are given a partial implementation of two classes. GroceryItem is a class that holds the...

    You are given a partial implementation of two classes. GroceryItem is a class that holds the information for each grocery item. GroceryInventory is a class that holds an internal listing of GroceryItems as well as the tax rate for the store. Your inventory could be 100 items it could be 9000 items. You won’t know until your program gets the shipment at runtime. For this you should use the vector class from the C++ standard library. I've completed the GroceryItem.h...

  • NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...

    NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • Write a MyString class that stores a (null-terminated) char* and a length and implements all of...

    Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator: prints "Move assignment" and endl in addition...

  • Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will...

    Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will call our version MyString. This object is designed to make working with sequences of characters a little more convenient and less error-prone than handling raw c-strings, (although it will be implemented as a c-string behind the scenes). The MyString class will handle constructing strings, reading/printing, and accessing characters. In addition, the MyString object will have the ability to make a full deep-copy of itself...

  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

  • C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all...

    C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Submit your completed source (.cpp) file. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator:...

  •       C++ -- Vector of Bank Accounts (15 pts) Please help! :( Use csegrid or Clion for...

          C++ -- Vector of Bank Accounts (15 pts) Please help! :( Use csegrid or Clion for this part Add on to the lab12a solution(THIS IS PROVIDED BELOW). You will add an overload operator function to the operator << and a sort function (in functions.h and functions.cpp)    Print out the customer’s name, address, account number and balance using whatever formatting you would like    Sort on account balance (hint: you can overload the < operator and use sort from the...

  • Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor...

    Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT