diff --git a/Recruit-Training/Basic-Recruit-Training/person/Person.cpp b/Recruit-Training/Basic-Recruit-Training/person/Person.cpp new file mode 100644 index 00000000..9f24e03d --- /dev/null +++ b/Recruit-Training/Basic-Recruit-Training/person/Person.cpp @@ -0,0 +1,53 @@ +#include "Person.h" +#include + +Person::Person(){ + name_ = ""; + age_ = new int(0); +} + +Person::Person(const std::string& name, int age){ + name_ = name; + age_ = new int(age); +} + +Person::~Person(){ + delete age_; +} + +int Person::getAge() const { + return *age_; +} + +const std::string& Person::getName() const { + return name_; +} + +void Person::setName(const std::string& newName){ + name_ = newName; +} + +void Person::setAge(int newAge) { + *age_ = newAge; +} + +void Person::printInfo() const { + std::cout << "Name = " << name_ << ", Age = " << *age_ << std::endl; +} + +int Person::combinedAge(Person** personArray, int size) { + int sum = 0; + + for (int i = 0; i < size; i++) + { + sum += personArray[i] -> getAge(); + } + + return sum; +} + +void Person::birthday(Person& x) { + int currentAge = x.getAge(); + x.setAge(currentAge + 1); + } + diff --git a/Recruit-Training/Basic-Recruit-Training/person/main.cpp b/Recruit-Training/Basic-Recruit-Training/person/main.cpp index ecbaec5b..3ad003b2 100644 --- a/Recruit-Training/Basic-Recruit-Training/person/main.cpp +++ b/Recruit-Training/Basic-Recruit-Training/person/main.cpp @@ -20,14 +20,18 @@ void compareAnswers(const std::string myAnswer, const std::string correctAnswer) // Swaps a & b. void mySwap(int& a, int& b) { + int temp = a; + a = b; + b = temp; } // Increment the int that x is pointing to by one. void myIncrement(int* x) { + *x = *x + 1; } // Uncomment this when person.cpp has been created. -/* void personFunctions() +void personFunctions() { Person Bill; Bill.setName("Bill"); @@ -58,15 +62,18 @@ void myIncrement(int* x) << myBirthdayAnswer.str() << std::endl; compareAnswers(myBirthdayAnswer.str(), correctBirthdayAnswer); delete Joe; -} */ +} int main() { int a = 5; int b = 6; + int *p = &a; // Pass a & b into mySwap here // Pass in a to myIncrement here + mySwap(a, b); + myIncrement(p); std::cout << "Checking Swap and Increment: " << std::endl; @@ -78,8 +85,10 @@ int main() compareAnswers(swapIncrementAnswer.str(), correctSwapIncrementAnswer); // Pass a and b into mySwap // Pass in a to myIncrement + mySwap(a, b); + myIncrement(p); - //personFunctions(); // Uncomment this once you have completed the definitions of the Person class. + personFunctions(); // Uncomment this once you have completed the definitions of the Person class. return 0; } diff --git a/Recruit-Training/Basic-Recruit-Training/person/person.exe b/Recruit-Training/Basic-Recruit-Training/person/person.exe new file mode 100644 index 00000000..7091f7b9 Binary files /dev/null and b/Recruit-Training/Basic-Recruit-Training/person/person.exe differ diff --git a/Recruit-Training/Basic-Recruit-Training/pointers/main.cpp b/Recruit-Training/Basic-Recruit-Training/pointers/main.cpp index b78bb5c1..3482f793 100644 --- a/Recruit-Training/Basic-Recruit-Training/pointers/main.cpp +++ b/Recruit-Training/Basic-Recruit-Training/pointers/main.cpp @@ -11,6 +11,7 @@ using namespace std; void foo(int *a, int b); void bar(int *a, int b); +void mySwap(int *a, int *b); int main() { @@ -19,27 +20,32 @@ int main() int *p = &x; //TODO in the line below predict what what is going to be output - cout << "Predicted value of p: " /* << PLACE YOUR PREDICTION HERE*/ << endl; + cout << "Predicted value of p: " << "memory address of x" << endl; cout << "Actual value of p: " << p << endl; - cout << "Predicted value &x: " /* << PLACE YOUR PREDICTION HERE*/ << endl; + cout << "Predicted value &x: " << "memory address of x" << endl; cout << "Actual value &x: " << &x << endl; - cout << "Predicted value of *p: " /* << PLACE YOUR PREDICTION HERE*/ << endl; + cout << "Predicted value of *p: " << 3 << endl; cout << "Actual value of *p: " << *p << endl; foo(p, x); - cout << "Predicted value of *p: " /* << PLACE YOUR PREDICTION HERE*/ << endl; + cout << "Predicted value of *p: " << 42 << endl; cout << "Actual value of *p: " << *p << endl; - cout << "Predicted value of x: " /* << PLACE YOUR PREDICTION HERE*/ << endl; + cout << "Predicted value of x: " << 42 << endl; cout << "Actual value of x: " << x << endl; foo(p, y); - cout << "Predicted value of *p: " /* << PLACE YOUR PREDICTION HERE*/ << endl; + cout << "Predicted value of *p: " << 42 << endl; cout << "Actual value of *p: " << *p << endl; - cout << "Predicted value of y: " /* << PLACE YOUR PREDICTION HERE*/ << endl; + cout << "Predicted value of y: " << 4 << endl; cout << "Actual value of y: " << y << endl; + // x = 42, y = 4 - //start writing mySwap here + int *q = &y; + mySwap(p, q); + + cout << "New value of x (Should be 4): " << x << endl; + cout << "New value of y (Should be 42): " << y << endl; return 0; } @@ -55,3 +61,9 @@ void var(int *a, int b) *a = 365; b = 912; } + +void mySwap(int *a, int *b){ + int temp = *a; + *a = *b; + *b = temp; +} diff --git a/Recruit-Training/Basic-Recruit-Training/pointers/pointers.exe b/Recruit-Training/Basic-Recruit-Training/pointers/pointers.exe new file mode 100644 index 00000000..3fcc477c Binary files /dev/null and b/Recruit-Training/Basic-Recruit-Training/pointers/pointers.exe differ