Update CPP class for week 10
This commit is contained in:
parent
9ef1dbc6c9
commit
a651d2c8f7
BIN
cpp/10.6.2-operator-overloading/a.out
Executable file
BIN
cpp/10.6.2-operator-overloading/a.out
Executable file
Binary file not shown.
60
cpp/10.6.2-operator-overloading/main.cpp
Normal file
60
cpp/10.6.2-operator-overloading/main.cpp
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
#include <iostream>
|
||||||
|
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;
|
||||||
|
}
|
BIN
cpp/10.7.1-lab-vending-machine/a.out
Executable file
BIN
cpp/10.7.1-lab-vending-machine/a.out
Executable file
Binary file not shown.
68
cpp/10.8-lab-BankAccount-class/BankAccount.cpp
Normal file
68
cpp/10.8-lab-BankAccount-class/BankAccount.cpp
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
}
|
29
cpp/10.8-lab-BankAccount-class/BankAccount.h
Normal file
29
cpp/10.8-lab-BankAccount-class/BankAccount.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#ifndef BANKACCOUNTH
|
||||||
|
#define BANKACCOUNTH
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
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
|
BIN
cpp/10.8-lab-BankAccount-class/a.out
Executable file
BIN
cpp/10.8-lab-BankAccount-class/a.out
Executable file
Binary file not shown.
21
cpp/10.8-lab-BankAccount-class/main.cpp
Normal file
21
cpp/10.8-lab-BankAccount-class/main.cpp
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
|
18
cpp/10.9-lab-calculator-class/Calculator.cpp
Normal file
18
cpp/10.9-lab-calculator-class/Calculator.cpp
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#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; }
|
8
cpp/10.9-lab-calculator-class/Calculator.cpp~
Normal file
8
cpp/10.9-lab-calculator-class/Calculator.cpp~
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include "Calculator.h"
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// TODO: Define default constructor
|
||||||
|
|
||||||
|
// TODO: Define member functions -
|
||||||
|
// Add(), Subtract(), Multiply(), Divide(), Clear(), GetValue()
|
23
cpp/10.9-lab-calculator-class/Calculator.h
Normal file
23
cpp/10.9-lab-calculator-class/Calculator.h
Normal file
@ -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
|
15
cpp/10.9-lab-calculator-class/Calculator.h~
Normal file
15
cpp/10.9-lab-calculator-class/Calculator.h~
Normal file
@ -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
|
BIN
cpp/10.9-lab-calculator-class/a.out
Executable file
BIN
cpp/10.9-lab-calculator-class/a.out
Executable file
Binary file not shown.
39
cpp/10.9-lab-calculator-class/main.cpp
Normal file
39
cpp/10.9-lab-calculator-class/main.cpp
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
|
#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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user