int main() { SpreadsheetCell myCell; myCell.setValue(6); return 0; }
SpreadsheetCell operator/(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs) { if (rhs.getValue() == 0) { throw invalid_argument("Divide by zero."); } return SpreadsheetCell(lhs.getValue() / rhs.getValue()); }
SpreadsheetCell operator/(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs) { if (rhs.mValue == 0) throw invalid_argument("Divide by zero."); SpreadsheetCell newCell; newCell.set(lhs.mValue / rhs.mValue); // call set to update mValue and mString return newCell; }
int main() { SpreadsheetCell myCell(4), anotherCell(5); SpreadsheetCell aThirdCell = myCell.add(anotherCell); cout << aThirdCell.getValue() << endl; return 0; }
int main() { SpreadsheetCell* myCellp = new SpreadsheetCell(); myCellp->setValue(3.7); cout << "cell 1: " << myCellp->getValue() << " " << myCellp->getString() << endl; delete myCellp; myCellp = nullptr; return 0; }
int main() { // Using the default constructor SpreadsheetCell myCell; myCell.setValue(6); cout << "cell 1: " << myCell.getValue() << endl; // The next line will crash when compiled with SpreadsheetCellInitListBackward.cpp SpreadsheetCell myCell2("6"); auto smartCellp = make_unique<SpreadsheetCell>(); // Or with a naked pointer (not recommended) SpreadsheetCell* myCellp = new SpreadsheetCell(); // ... use myCellp delete myCellp; myCellp = nullptr; return 0; }
bool operator>(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs) { return (lhs.getValue() > rhs.getValue()); }
SpreadsheetCell operator*(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs) { return SpreadsheetCell(lhs.getValue() * rhs.getValue()); }
void printCell(const SpreadsheetCell& inCell) { cout << inCell.getString() << endl; }
SpreadsheetCell operator*(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs) { SpreadsheetCell newCell; newCell.set(lhs.mValue * rhs.mValue); // call set to update mValue and mString return newCell; }
int main() { // On the stack (destroyed when out of scope) SpreadsheetCell stackCell, stackCell2; // Omit parentheses for the default constructor stackCell.set(6); stackCell2.set("3.2"); cout << "stack cell 1: " << stackCell.getValue() << endl; cout << "stack cell 2: " << stackCell2.getValue() << endl; // Constructor on the stack SpreadsheetCell stackCell3(4.2), stackCell4(9.2); cout << "stack cell 3: " << stackCell3.getValue() << endl; cout << "stack cell 4: " << stackCell4.getValue() << endl; // On the heap SpreadsheetCell* heapCell = new SpreadsheetCell(); heapCell->set(3.7); cout << "heap cell 1: " << heapCell->getValue() << endl; delete heapCell; heapCell = nullptr; // Constructor on the heap auto heapCell2 = new SpreadsheetCell(44); cout << "heap cell 2: " << heapCell2->getValue() << endl; delete heapCell2; heapCell2 = nullptr; // Smart pointers for the heap auto smartCell = make_unique<SpreadsheetCell>(); smartCell->set(3.14159); cout << "smart cell 1: " << smartCell->getValue() << endl; // Constructor with for pointer to the heap auto smartCell2 = make_unique<SpreadsheetCell>(42); cout << "smart cell 2: " << smartCell2->getValue() << endl; // Manual heap delayed instantiation SpreadsheetCell* heapCell3 = nullptr; heapCell3 = new SpreadsheetCell(55); cout << "heap cell 3: " << heapCell3->getValue() << endl; delete heapCell3; heapCell3 = nullptr; // Array instantiation requires a default constructor (as to STL collections) const int size = 3; SpreadsheetCell cells[3]; SpreadsheetCell* moreCells = new SpreadsheetCell[10]; // HEAP! // Stack arrays can use an initializer for non-default constructors SpreadsheetCell stackCells[3] = { SpreadsheetCell(3), SpreadsheetCell(4), SpreadsheetCell(5) }; for (int i = 0; i < size; i++) { cout << "arr cell " << i << ": " << cells[i].getValue() << endl; cout << "heap arr cell " << i << ": " << moreCells[i].getValue() << endl; cout << "stack arr cell " << i << ": " << stackCells[i].getValue() << endl; } delete[] moreCells; // CLEAN THE HEAP! moreCells = nullptr; // Copy auto copyCell = SpreadsheetCell(stackCell); cout << "copy cell: " << copyCell.getValue() << endl; // Initializer-List Constructor // EvenSequence p1 = {1.0, 2.0, 3.0, 4.0, 5.5, 7.7}; // or EvenSequence p1 {1.0, 2.0, 3.0, 4.0, 5.5, 7.7}; p1.dump(); try { EvenSequence p2 = {1.0, 2.0, 3.0}; } catch (const invalid_argument& err) { cout << err.what() << endl; } // std initializer list vector<string> myVec {"Hello", "World"}; for (auto value : myVec) { cout << value << endl; } // Done return 0; }
SpreadsheetCell SpreadsheetCell::operator+(const SpreadsheetCell& cell) const { return SpreadsheetCell(getValue() + cell.getValue()); }