diff --git a/cpp/10.6.2-operator-overloading/a.out b/cpp/10.6.2-operator-overloading/a.out new file mode 100755 index 0000000..1a662b0 Binary files /dev/null and b/cpp/10.6.2-operator-overloading/a.out differ diff --git a/cpp/10.6.2-operator-overloading/main.cpp b/cpp/10.6.2-operator-overloading/main.cpp new file mode 100644 index 0000000..a36e2d6 --- /dev/null +++ b/cpp/10.6.2-operator-overloading/main.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +class FamilyVacation { + public: + void SetNumDays(int dayCount); + void SetNumPeople(int peopleCount); + void Print() const; + FamilyVacation operator+(int moreDays); + private: + int numDays; + int numPeople; +}; + +void FamilyVacation::SetNumDays(int dayCount) { + numDays = dayCount; +} + +void FamilyVacation::SetNumPeople(int peopleCount) { + numPeople = peopleCount; +} + +// FIXME: Overload + operator so can write newVacation = oldVacation + 5, +// which adds 5 to numDays, while just copying numPeople. + +/* Your solution goes here */ +// Overload + operator for TimeHrMn +FamilyVacation FamilyVacation::operator+(int moreDays) { + FamilyVacation total; + + total.numDays = numDays + moreDays; + total.numPeople = numPeople; + + return total; +} + +void FamilyVacation::Print() const { + cout << "Days: " << numDays << ", People: " << numPeople << endl; +} + +int main() { + FamilyVacation firstVacation; + FamilyVacation secondVacation; + int userDays; + int userPeople; + + cin >> userDays; + cin >> userPeople; + + cout << "First vacation: "; + firstVacation.SetNumDays(userDays); + firstVacation.SetNumPeople(userPeople); + firstVacation.Print(); + + cout << "Second vacation: "; + secondVacation = firstVacation + 5; + secondVacation.Print(); + + return 0; +} diff --git a/cpp/10.7.1-lab-vending-machine/a.out b/cpp/10.7.1-lab-vending-machine/a.out new file mode 100755 index 0000000..60584c0 Binary files /dev/null and b/cpp/10.7.1-lab-vending-machine/a.out differ diff --git a/cpp/10.8-lab-BankAccount-class/BankAccount.cpp b/cpp/10.8-lab-BankAccount-class/BankAccount.cpp new file mode 100644 index 0000000..a1af96b --- /dev/null +++ b/cpp/10.8-lab-BankAccount-class/BankAccount.cpp @@ -0,0 +1,68 @@ +#include +#include "BankAccount.h" +using namespace std; +// TODO: Define public member functions +string cust_name; +double checking; +double savings; + + + BankAccount::BankAccount(string newName, double amt1, double amt2){ + cust_name = newName; + checking = amt1; + savings = amt2; + } + + void BankAccount::SetName(string newName){ + cust_name = newName; + } + + string BankAccount::GetName(){ + return cust_name; + } + + void BankAccount::SetChecking(double amt){ + checking = amt; + } + + double BankAccount::GetChecking(){ + return checking; + } + void BankAccount::SetSavings(double amt){ + savings = amt; + } + + double BankAccount::GetSavings(){ + return savings; + } + + void BankAccount::DepositChecking(double amt){ + if (amt>0){ + checking+=amt; + } + } + + void BankAccount::DepositSavings(double amt){ + if (amt>0){ + savings+=amt; + } + } + + void BankAccount::WithdrawChecking(double amt){ + if (amt>0){ + checking-=amt; + } + } + + void BankAccount::WithdrawSavings(double amt){ + if (amt>0){ + savings-=amt; + } + } + + void BankAccount::TransferToSavings(double amt){ + if (amt>0){ + checking -= amt; + savings += amt; + } + } diff --git a/cpp/10.8-lab-BankAccount-class/BankAccount.h b/cpp/10.8-lab-BankAccount-class/BankAccount.h new file mode 100644 index 0000000..85fa85f --- /dev/null +++ b/cpp/10.8-lab-BankAccount-class/BankAccount.h @@ -0,0 +1,29 @@ +#ifndef BANKACCOUNTH +#define BANKACCOUNTH + +#include +using namespace std; + +class BankAccount { + public: + // TODO: Declare public member functions + BankAccount(string newName, double amt1, double amt2); + void SetName(string newName); + string GetName(); + void SetChecking(double amt); + double GetChecking(); + void SetSavings(double amt); + double GetSavings(); + void DepositChecking(double amt); + void DepositSavings(double amt); + void WithdrawChecking(double amt); + void WithdrawSavings(double amt); + void TransferToSavings(double amt); + private: + // TODO: Declare private data members + string cust_name; + double savings; + double checking; +}; + +#endif diff --git a/cpp/10.8-lab-BankAccount-class/a.out b/cpp/10.8-lab-BankAccount-class/a.out new file mode 100755 index 0000000..95d0625 Binary files /dev/null and b/cpp/10.8-lab-BankAccount-class/a.out differ diff --git a/cpp/10.8-lab-BankAccount-class/main.cpp b/cpp/10.8-lab-BankAccount-class/main.cpp new file mode 100644 index 0000000..4b58060 --- /dev/null +++ b/cpp/10.8-lab-BankAccount-class/main.cpp @@ -0,0 +1,21 @@ +#include +#include +#include "BankAccount.h" +using namespace std; + +int main() { + BankAccount account("Mickey", 500.00, 1000.00); + account.SetChecking(500); + account.SetSavings(500); + account.WithdrawSavings(100); + account.WithdrawChecking(100); + account.TransferToSavings(300); + + cout << account.GetName() << endl; + cout << fixed << setprecision(2); + cout << account.GetChecking() << endl; + cout << account.GetSavings() << endl; + + return 0; +} + diff --git a/cpp/10.9-lab-calculator-class/Calculator.cpp b/cpp/10.9-lab-calculator-class/Calculator.cpp new file mode 100644 index 0000000..b74838a --- /dev/null +++ b/cpp/10.9-lab-calculator-class/Calculator.cpp @@ -0,0 +1,18 @@ +#include +#include "Calculator.h" +using namespace std; + +double value; +// TODO: Define default constructor +Calculator::Calculator(){ + value = 0; +} +// TODO: Define member functions - +// Add(), Subtract(), Multiply(), Divide(), Clear(), GetValue() + +void Calculator::Add(double n) { value += n; } +void Calculator::Subtract(double n) { value -= n; } +void Calculator::Multiply(double n) { value *= n; } +void Calculator::Divide(double n) { value /= n; } +void Calculator::Clear() { value = 0; } +double Calculator::GetValue(){ return value; } diff --git a/cpp/10.9-lab-calculator-class/Calculator.cpp~ b/cpp/10.9-lab-calculator-class/Calculator.cpp~ new file mode 100644 index 0000000..5f8bfd5 --- /dev/null +++ b/cpp/10.9-lab-calculator-class/Calculator.cpp~ @@ -0,0 +1,8 @@ +#include +#include "Calculator.h" +using namespace std; + +// TODO: Define default constructor + +// TODO: Define member functions - +// Add(), Subtract(), Multiply(), Divide(), Clear(), GetValue() diff --git a/cpp/10.9-lab-calculator-class/Calculator.h b/cpp/10.9-lab-calculator-class/Calculator.h new file mode 100644 index 0000000..adf377d --- /dev/null +++ b/cpp/10.9-lab-calculator-class/Calculator.h @@ -0,0 +1,23 @@ +#ifndef CALCULATORH +#define CALCULATORH + +class Calculator { + public: + // TODO: Declare default constructor + + // TODO: Declare member functions - + // Add(), Subtract(), Multiply(), Divide(), Clear(), GetValue() + Calculator(); + void Add(double n); + void Subtract(double n); + void Multiply(double n); + void Divide(double n); + void Clear(); + double GetValue(); + + private: + // TODO: Declare private data member - value + double value; +}; + +#endif diff --git a/cpp/10.9-lab-calculator-class/Calculator.h~ b/cpp/10.9-lab-calculator-class/Calculator.h~ new file mode 100644 index 0000000..e411c6f --- /dev/null +++ b/cpp/10.9-lab-calculator-class/Calculator.h~ @@ -0,0 +1,15 @@ +#ifndef CALCULATORH +#define CALCULATORH + +class Calculator { + public: + // TODO: Declare default constructor + + // TODO: Declare member functions - + // Add(), Subtract(), Multiply(), Divide(), Clear(), GetValue() + + private: + // TODO: Declare private data member - value +}; + +#endif diff --git a/cpp/10.9-lab-calculator-class/a.out b/cpp/10.9-lab-calculator-class/a.out new file mode 100755 index 0000000..0408dfd Binary files /dev/null and b/cpp/10.9-lab-calculator-class/a.out differ diff --git a/cpp/10.9-lab-calculator-class/main.cpp b/cpp/10.9-lab-calculator-class/main.cpp new file mode 100644 index 0000000..90925b2 --- /dev/null +++ b/cpp/10.9-lab-calculator-class/main.cpp @@ -0,0 +1,39 @@ +#include +#include +#include "Calculator.h" +using namespace std; + +int main() { + Calculator calc; + double num1; + double num2; + + cin >> num1; + cin >> num2; + + cout << fixed << setprecision(1); + // 1. The initial value + cout << calc.GetValue() << endl; + + // 2. The value after adding num1 + calc.Add(num1); + cout << calc.GetValue() << endl; + + // 3. The value after multiplying by 3 + calc.Multiply(3); + cout << calc.GetValue() << endl; + + // 4. The value after subtracting num2 + calc.Subtract(num2); + cout << calc.GetValue() << endl; + + // 5. The value after dividing by 2 + calc.Divide(2); + cout << calc.GetValue() << endl; + + // 6. The value after calling the clear() method + calc.Clear(); + cout << calc.GetValue() << endl; + + return 0; +}