Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objective: The objective of this assignment is for you to reverse engineer C++ code and create C++ statements which will change the state of a

Objective:

The objective of this assignment is for you to reverse engineer C++ code and create C++ statements which will change the state of a class so your output will match the sample output.

Your object is therefore to devise a way, given the knowledge you have about classes, pointers, call-by-value, call-by-reference, and call-by-pointer, to enable you to get the correct value.

Instructions

We are providing you with 3 files: Pointers.h, pointer-sandbox.cpp, and manip.h

The sandbox file should be complete. If you compile it you will see what the output should be and what it is now. Your goal is to make them the same.

You will be editing the manip.h file and compiling the sandbox file.

You will use the Pointers class implemented in Pointers.h. Youll note that EVERY sample output calls a manipX function, with X matching the function 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 your output would match the sample code!

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 sample output and try to imagine how to setup your pointers to match the output.. Each manip function is passed parameters which allow it to change a Pointer instance somehow in order for you to get the correct output

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. You may use ANY C/C++ tricks you can to get the tests to pass.

Your proposed changes will only affect the manip.h. File.

Sample Output

maninp1: 10 = 10 maninp2: 45 = 45 maninp3: 383 = 383 maninp4: 0x7fff8072b6fc = 0x7fff8072b6fc 1 == 1 maninp5: 45 = 45 maninp6: 10 = 10 maninp7: 15 = 15 maninp8: 199 = 199 maninp9: 0xbe9c20 = 0xbe9c20 maninp10: 199 = 199

Mainp.h code

#ifndef MANIP_H #define MANIP_H

// Author: Fill in your name // Source File: manip.h // Description: A set of functions where you should manipulate // the passed parameters to change the object in a specific way, // so that Pointers_test.h passes all tests.

#include "Pointers.h"

// A little something to get you going void manip1(Pointers* p){ *(p->getA()) = 10; }

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

pointer-sandbox code

#include "Pointers.h" #include "manip.h" #include #include

using namespace std;

int main() { // maninp1 Pointers a; manip1(&a); cout << "maninp1:" << endl; cout << (*(a.getA())) << "\t== 10 " << endl; // maninp1 2 int number = 56; Pointers b(number, &number); manip2(&b); cout << "maninp2:" << endl; cout << (*(b.getB())) << "\t== 45" << endl; // maninp1 3 number = rand() % 1000; int number2 = 4;

Pointers c(number, &number2); manip3(&c); cout << "maninp3:" << endl; cout << (*(c.getB())) << "\t== " << number << endl;

number2 = 4; Pointers d; manip4(&d, &number2); cout << "maninp4:" << endl; cout << d.getB() << "\t== " << &number2 << endl; cout << (d.getB() == &number2) << "\t== 1" << endl; number2 = 4; Pointers e(0, &number2); manip5(&e, &number2); cout << "maninp5:" << endl; cout << e.getC() << "\t== " << 45 << endl; int numbers[] = {5,6,7,8}; Pointers f(0, numbers); manip6(&f); cout << "maninp6:" << endl; cout << numbers[2] << "\t== " << 10 << endl;

int* number3 = new int; Pointers g(0, number3); manip7(&g); cout << "maninp7:" << endl; cout << *number3 << "\t== " << 15 << endl; delete number3; Pointers h; manip8(&h); cout << "maninp8:" << endl; cout << h.getC() << "\t== " << 199 << endl;

int* number4 = new int; Pointers* i = new Pointers(); manip9(i, number4); cout << "maninp9:" << endl; cout << i->getB() << "\t== " << number4 << endl; delete number4; delete i;

Pointers* j = new Pointers[10]; manip10(j); cout << "maninp10:" << endl; cout << j[5].getC() << "\t== " << 199 << endl; delete[] j;

return 0; }

Pointers.h code

#ifndef POINTERS_H #define POINTERS_H

// Author: Paul Talaga // Source File: Pointers.h // Description: Implements a convoluted class to exercise pointers, // dynamic memory, and reference manipulations.

#include

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases And Information Systems 1 International Baltic Conference Dbandis 2020 Tallinn Estonia June 19 2020 Proceedings

Authors: Tarmo Robal ,Hele-Mai Haav ,Jaan Penjam ,Raimundas Matulevicius

1st Edition

303057671X, 978-3030576714

More Books

Students also viewed these Databases questions

Question

Briefly describe three important coding schemes.

Answered: 1 week ago