Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User/paula/basic-recruit-training #325

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Recruit-Training/Basic-Recruit-Training/person/Person.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "Person.h"
#include <iostream>

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

15 changes: 12 additions & 3 deletions Recruit-Training/Basic-Recruit-Training/person/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;

Expand All @@ -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;
}
Binary file not shown.
28 changes: 20 additions & 8 deletions Recruit-Training/Basic-Recruit-Training/pointers/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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;
}
Expand All @@ -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;
}
Binary file not shown.