Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I need help about this coding part! I got an error in the EggCarton.h and EggCarton.cpp files. How can I resolve it? Please let

Hello, I need help about this coding part!

I got an error in the EggCarton.h and EggCarton.cpp files. How can I resolve it? Please let me know

the correct codes! Thank you in advance!

*Detail: https://github.com/Seneca-244200/OOP-Workshops/tree/main/WS05

main.cpp

/*********************************************************************** // OOP244 Workshop 5 Part1: Operators // File main.cpp // Version 1.0 // Date 2023/02 // Author Fardad Soleimanloo // Description // Tests EggCarton module // // Revision History // ----------------------------------------------------------- // Name Date Reason // ///////////////////////////////////////////////////////////////// ***********************************************************************/ #include #include "EggCarton.h" using namespace std; using namespace sdds; void IOTest(); void typeConversionTest(); void unaryOperatorTest(); void BinaryOperators(); int main() { IOTest(); typeConversionTest(); unaryOperatorTest(); BinaryOperators(); return 0; } void BinaryOperators() { EggCarton e1, e2(6, 4), e3(6, 5), bad(40); cout << endl << "Binary Member operator tests" << endl; cout << "e1: " << int(e1) << ", e2: " << int(e2) << ", e3: " << int(e3) << endl; e1 = e2 += e3; cout << "e1 = e2 += e3;" << endl; cout << "e1: " << int(e1) << ", e2: " << int(e2) << ", e3: " << int(e3) << endl; bad += e3; cout << "bad += e3;" << endl; cout << "bad: " << int(bad) << ", e3: " << int(e3) << endl; bad += e3; cout << "---------------------------------------------" << endl; e1 = 20; e2 = 2; cout << "e1 = 20; e2 = 2;" << endl; cout << "e1: " << int(e1) << ", e2: " << int(e2) << endl; e1 = 2; cout << "e1 = 2;" << endl; cout << "e1: " << int(e1) << endl; cout << "---------------------------------------------" << endl; e1 += 2; e2 += 1; e3 += 4; cout << "e1 += 1; e2 += 1; e3 += 4;" << endl; cout << "e1: " << int(e1) << ", e2: " << int(e2) << ", e3: " << int(e3) << endl; cout << "---------------------------------------------" << endl; e1 = EggCarton(6, 2, true); e2 = EggCarton(6, 3, false); e3 = EggCarton(18, 18, true); cout << "e1: " << int(e1) << ", e2: " << int(e2) << ", e3: " << int(e3) << endl; cout << "e1:" << endl << e1 << "e2:" << endl << e2 << "e3:" << endl << e3 << endl; cout << "e1 is " << double(e1) << " kgs and e2 is " << double(e2) << " kgs therefore thier weights are " << (e1 == e2 ? "equal" : "different") << endl; cout << "e1 is " << double(e1) << " kgs and e3 is " << double(e3) << " kgs therefore thier weights are " << (e1 == e3 ? "equal" : "different") << endl; cout << "---------------------------------------------" << endl; cout << endl << "Binary non-member operator test" << endl; cout << "I have 5 eggs and there are " << int(e3) << " eggs in the Carton." << endl << "In total I have " << 5 + e3 << " eggs!" << endl; } void unaryOperatorTest() { EggCarton e1(6, 4), e2; cout << endl << "Unary operator tests" << endl; cout << "e1: " << int(e1) << endl; cout << "e2: " << int(e2) << endl; e2 = --e1; cout << "e2 = --e1;" << endl; cout << "e1: " << int(e1) << endl; cout << "e2: " << int(e2) << endl; cout << "--------------------------------" << endl; e2 = ++e1; cout << "e2 = ++e1;" << endl; cout << "e1: " << int(e1) << endl; cout << "e2: " << int(e2) << endl; cout << "--------------------------------" << endl; e2 = e1--; cout << "e2 = e1--;" << endl; cout << "e1: " << int(e1) << endl; cout << "e2: " << int(e2) << endl; cout << "--------------------------------" << endl; e2 = e1++; cout << "e2 = e1++;" << endl; cout << "e1: " << int(e1) << endl; cout << "e2: " << int(e2) << endl; cout << "End Unary operator tests" << endl;

}

void typeConversionTest() { EggCarton eggs[] = { {}, {36,20,true}, {36,20,false}, {42} }; cout << endl << "Type Conversion operator tests" << endl; for (int i = 0; i < 4; i++) { cout.setf(ios::fixed); cout.precision(2); if (eggs[i]) { cout << int(eggs[i]) << " Eggs:" << endl; cout << double(eggs[i]) << " kgs" << endl << "---------------" << endl; } else { cout << "Bad or Broken Carton" << endl; } } cout << "END Type Conversion operator tests" << endl; } void IOTest() { cout << endl << "Operator <<, Operator >>, display and read test" << endl; EggCarton eggs[] = { {}, {10}, {10,5}, {6,-1}, {42,10}, {36,37}, {6,4,true}, {6,4,false}, {18,10,true}, {18,10,false} }; for (int i = 0; i < 10; i++) { cout << eggs[i] << "----------" << endl; } cout << "Enter the following valid values:" << endl << " j,24,20" << endl << ">"; cin >> eggs[0]; cout << eggs[0] << "----------" << endl; cout << "Enter the following valid values:" << endl << " r,24,20" << endl << ">"; cin >> eggs[0]; cout << eggs[0] << "----------" << endl; cout << "Enter the following invalid values:" << endl << " j,44,20" << endl << ">"; cin >> eggs[0]; cout << eggs[0] << "----------" << endl; cout << "Enter the following invalid values:" << endl << " j,24,40" << endl << ">"; cin >> eggs[0]; cout << eggs[0] << "----------" << endl; cout << "END Operator <<, Operator >>, display and read test" << endl; }

EggCarton.h

#ifndef SDDS_EGGCARTON_H #define SDDS_EGGCARTON_H #include using namespace std; namespace sdds { const int RegularEggWieght = 50; const int JumboEggWieght = 75; class EggCarton { int m_size; int m_noOfEggs; bool m_jumbo; void setBroken(); public:EggCarton(int size = 6, int noOfEggs = 0, bool jumboSize = false); ostream& display(std::ostream& ostr) const; istream& read(std::istream& istr); operator bool() const; operator int() const; operator double() const; EggCarton& operator--(); EggCartonoperator--(int); EggCarton& operator++(); EggCarton operator++(int); EggCarton& operator=(int value); EggCarton& operator+=(int value); EggCarton& operator+=(EggCarton& right); bool operator==(const EggCarton& right) const; }; int operator+(int left, const EggCarton& right); ostream& operator<<(ostream& ostr, const EggCarton& right); istream& operator>>(istream& istr, EggCarton& right); } #endif

EggCarton.cpp

#include #include "EggCarton.h" using namespace std; namespace sdds { void EggCarton::setBroken() { m_size = -1; m_noOfEggs = -1; } EggCarton::EggCarton(int size, int noOfEggs, bool jumboSize) { if (size % 6 == 0 && size >= 6 && size <= 36 && noOfEggs >= 0 && noOfEggs <= size) { m_size = size; m_noOfEggs = noOfEggs; m_jumbo = jumboSize; } else { setBroken(); } } ostream& EggCarton::display(std::ostream& ostr) const { if (*this) { // displays an Egg Cartonint cartonWidth = m_size == 6 ? 3 : 6;for (int i = 0; i < m_size; i++) { cout << ((i < m_noOfEggs) ? (m_jumbo ? 'O' : 'o') : '~'); if ((i + 1) % cartonWidth == 0) cout << endl; } } else { ostr << "Broken Egg Carton!" << endl; } return ostr; } istream& EggCarton::read(std::istream& istr) { char type; istr >> type; if (type == 'j') { m_jumbo = true; } else { m_jumbo = false; } // m_jumbo = type == 'j'; istr.ignore(); istr >> m_size; istr.ignore(); istr >> m_noOfEggs; if (!(m_size % 6 == 0 && m_size >= 6 && m_size <= 36 && m_noOfEggs >= 0 && m_noOfEggs <= m_size))setBroken(); return istr; } EggCarton::operator bool() const { return (m_size > 0); } EggCarton::operator int() const { int r = -1; if (*this) { r = m_noOfEggs; } return r; } EggCarton::operator double() const { double r = -1; if (*this) { if (m_jumbo) { r = (m_noOfEggs * JumboEggWieght) / 1000.0; } else { r = (m_noOfEggs * RegularEggWieght) / 1000.0; } } return r; } EggCarton& EggCarton::operator --() { if (*this && m_noOfEggs > 0) { m_noOfEggs--; } return *this; } EggCarton EggCarton::operator --(int) { EggCarton temp = *this; operator--(); return temp; } EggCarton& EggCarton::operator ++() { if (*this) { m_noOfEggs++; }if (m_noOfEggs > m_size) { setBroken(); }return *this; } EggCarton EggCarton::operator ++(int) { EggCarton temp = *this; operator++(); return temp; } EggCarton& EggCarton::operator=(int value) { m_noOfEggs = value; if (m_noOfEggs > m_size) { setBroken(); } return *this; } EggCarton& EggCarton::operator+=(int value) { if (*this) { m_noOfEggs += value; } if (m_noOfEggs > m_size) { setBroken(); } return *this; } EggCarton& EggCarton::operator+=(EggCarton& right) { if (*this) { m_noOfEggs += right.m_noOfEggs; if (m_noOfEggs > m_size) { right.m_noOfEggs = m_noOfEggs - m_size; m_noOfEggs = m_size; } else { right.m_noOfEggs = 0; } } return *this; } bool EggCarton::operator==(const EggCarton& right) const { bool ret = false; double diff = double(*this) - double(right); if (diff > -0.001 && diff < 0.001) { ret = true; }return ret; } int operator+(int left, const EggCarton& right) { if (right) { left += int(right); } return left; } ostream& operator<<(ostream& ostr, const EggCarton& right) { return right.display(ostr); } istream& operator>>(istream& istr, EggCarton& right) { return right.read(istr); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions