# BEST代写-线上编程学术专家

Best代写-最专业靠谱代写IT | CS | 留学生作业 | 编程代写Java | Python |C/C++ | PHP | Matlab | Assignment Project Homework代写

# C++代写 | HW 6: C++ Classes and Smart Pointers

### C++代写 | HW 6: C++ Classes and Smart Pointers

HW 6: C++ Classes and Smart Pointers

## Part 1: setVariables() for Num, Sum, Var

If you run make right now, it should print a warning about not returning anything in setVariables(), but it will generate the ./main executable. If you run ./main it will segfault. We need to implement the setVariables() function for Num, Sum, and Var.

This is a recursive function which will replace some or all of the Var nodes with Num nodes, based on the map from variable names to double values.

For example, in main.cc they call exp1->setVariables(values), which should return an expression with all the “x” variables replaced with 42 and all the “y” variables replaced with “43”.

Note: setVariables() always returns a copy, it should never modify the current object.

setVariables() has two base cases: Num and Var. The implementation for Num is the simplest, it should just return a copy of itself. Var should check if its own variable name is in the map passed in. If it is not, then just return a copy of itself. If its own variable name is in the map, it should return a Num where the number is the value associated with its variable name.

Sum is the recursive case; just call setVariables on the left and right expression.

Feel free to take inspiration from clone()!

After doing this, you should be able to recompile and not get a segmentation fault when running ./main.

## Part 2: Prod and operator*()

Now, implement a Prod node representing multiplication (product). You should see the class in Expr.h. This process should be pretty similar to implementing Sum since they are both operators which have two expressions.

You will have to add the functions to Expr.cc.

For operator*(), check out the operator+() implementation provided in Expr.cc.

After doing this you should be able to uncomment the next block of code in main.cc. When you run make it should compile ./main. Running ./main, you should see the actual output match the expected output

## Part 3: Pow and operator^()

Now, implement a Pow node representing an exponent (e.g. x^2). The Pow class is in Expr.h, but it doesn’t have any of the methods or data members in it, so fill those in. It should be very similar to Sum and Prod.

Implement each of those functions as well as operator^().

You should now be able to uncomment all of main.cc. Run make and ./main to ensure that it matches the expected output.

## Part 4: plot.cc

Before working on the plot part of this homework there are two commands you need to run just once to get the Python3 script we have provided to work:

 pip3 install –user matplotlib pip3 install –user numpy