We are providing you with 4 code files: Pointers.h, pointer-sandbox.cpp, Pointer
ID: 3685085 • Letter: W
Question
We are providing you with 4 code files: Pointers.h, pointer-sandbox.cpp, Pointers_test.h, and manip.h.
This is the pointers.h file
#include <iostream>
using namespace std;
class Pointers {
public:
Pointers(){
a = 5;
b = NULL;
c = 10;
}
Pointers(int a, int* b){
this->a = a;
this->b = b;
c = 0;
}
int* getA(){
return &a;
}
int* getB() const{
return b;
}
int getC() const{
return c;
}
void setB(int* b){
this->b = b;
}
void setC(){
c = *b;
}
private:
int a;
int* b;
int c;
};
#endif
Like past assignments, make will compile pointer-sandbox.cpp to pointer-sandbox. This sandbox is completely for your use in developing solutions to this problem. Pointer-sandbox.cpp will not be submitted.
make test will compile the unit tests located in Pointers_test.h. These tests use the Pointers class implemented in Pointers.h. You’ll note that EVERY unit tests calls a manipX function, with X matching the unit test number. These functions are located in manip.h and are currently empty. Your job is to fill in the manip functions in manip.h so all unit tests pass!
Writing the manip functions will require you to FULLY understand what the Pointers class does, what private variables it stores, and most importantly what the member functions can do. Study the unit tests and understand what needs to change in order for them to pass. Each manip function is passed parameters which allow it to change a Pointer instance somehow in order for the unit test to pass.
DRAW PICTURES of what memory looks like! Each manip function can be filled in with less than or equal to 3 lines of code, with 7 of them requiring only one line.
This is the pointers_test.h file
#include "Pointers.h"
#include "manip.h"
using namespace std;
class myComplexGetters : public CxxTest::TestSuite {
public:
void test1(){
Pointers a;
manip1(&a);
TS_ASSERT_EQUALS(*(a.getA()), 10);
}
void test2(){
int number = 56;
Pointers a(number, &number);
manip2(&a);
TS_ASSERT_EQUALS(*(a.getB()), 45);
}
void test3(){
int number = rand() % 1000;
int number2 = 4;
Pointers a(number, &number2);
manip3(&a);
TS_ASSERT_EQUALS(*(a.getB()), number);
}
void test4(){
int number2 = 4;
Pointers a;
manip4(&a, &number2);
TS_ASSERT_EQUALS(a.getB(), &number2);
}
void test5(){
int number2 = 4;
Pointers a(0, &number2);
manip5(&a, &number2);
TS_ASSERT_EQUALS(a.getC(), 45);
}
void test6(){
int numbers[] = {5,6,7,8};
Pointers a(0, numbers);
manip6(&a);
TS_ASSERT_EQUALS(numbers[2], 10);
}
void test7(){
int* number = new int;
Pointers a(0, number);
manip7(&a);
TS_ASSERT_EQUALS(*number, 15);
delete number;
}
void test8(){
Pointers a;
manip8(&a);
TS_ASSERT_EQUALS(a.getC(), 199);
}
void test9(){
int* number = new int;
Pointers* a = new Pointers();
manip9(a, number);
TS_ASSERT_EQUALS(a->getB(), number);
delete number;
delete a;
}
void test10(){
Pointers* a = new Pointers[10];
manip10(a);
TS_ASSERT_EQUALS(a[5].getC(), 199);
delete[] a;
}
};
#endif
This is the manip.h file
#include "Pointers.h"
void manip1(Pointers* p){
// TODO: Fill me in
}
void manip2(Pointers* p){
// TODO: Fill me in
}
void manip3(Pointers* p){
// TODO: Fill me in
}
void manip4(Pointers* p, int* other){
// TODO: Fill me in
}
void manip5(Pointers* p, int* other){
// TODO: Fill me in
}
void manip6(Pointers* p){
// TODO: Fill me in
}
void manip7(Pointers* p){
// TODO: Fill me in
}
void manip8(Pointers* p){
// TODO: Fill me in
}
void manip9(Pointers* p, int* other){
// TODO: Fill me in
}
void manip10(Pointers* p){
// TODO: Fill me in
}
#endif
Explanation / Answer
void manip1(Pointers* p){
*(p->getA()) = 10;
}
void manip2(Pointers* p){
*(p->getB()) = 45;
}
void manip3(Pointers* p){
*(p->getB()) = *(p->getA());
}
void manip4(Pointers* p, int* other){
p->setB(other);
}
void manip5(Pointers* p, int* other){
*(p->getB()) = 45;
p->setC();
}
void manip6(Pointers* p){
*((p->getB())+2) = 10;
}
void manip7(Pointers* p){
*(p->getB()) = 15;
}
void manip8(Pointers* p){
int temp = 199;
p->setB(&tmp);
p->setC();
}
void manip9(Pointers* p, int* other){
p->setB(other);
}
void manip10(Pointers* p){
int tmp = 199;
Pointers* a = p+5;
a->setB(&tmp);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.