Update CPP class for week 10

This commit is contained in:
Logen Kain 2021-03-28 14:53:22 -04:00
parent 9ef1dbc6c9
commit a651d2c8f7
13 changed files with 281 additions and 0 deletions

Binary file not shown.

View 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;
}

Binary file not shown.

View 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;
}
}

View 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

Binary file not shown.

View 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;
}

View 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; }

View 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()

View 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

View 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

Binary file not shown.

View 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;
}