TA - not ready

Exercise 1a

Given the following code will the code compile? If not fix it. How many bytes are allocated on the stack and what is the output?

class MyClass{
  int x,y;
public:
    MyClass(){x=1; y=2; cout << " MyClass()"<< endl;}

    MyClass( int new_x ){x= new_x; y=3; cout << " MyClass(int)"<< endl;}

    MyClass( double new_x, double new_y ){
        x= new_x; y=new_y;
        cout << " MyClass(double, double)"<< endl;
    }

    string to_string(){ 
      return "["+std::to_string(x)+","+std::to_string(y)+"]";
    }
};

int main() {
    MyClass a[4]; 
    MyClass b[4] {11, 22};
    MyClass c {11,21};
    MyClass d();
    MyClass f[5] {{11, 22}, 33, {55, 22}}; 
}

Answer

output

Memory Allocation in bytes

TODO check the pointers in bytes

An object takes up memory to store it’s (non-static) data members allocation explanationarrow-up-right size_of_int on 32 bit system = 4 bytes bytes: 14* 2 times size_of_int = 112 bytes

Exercise 1b

What will happen if replace MyClass d() with MyClass d{} or MyClass d

Answer

We will add MyClass() to the output before outputs of MyClass f[5].

bytes: 116 bytes

Exercise 2 - Makefiles

create a makefile using variable for the following files: main.cpp,sum.cpp, sum.hpp

TODO

Exercise 3 - LinkedList

Create a linked list class with the following functions: add(int), remove(int), get(int), contains(int). Properly divide the code into cpp and hpp files. How much memory does your code take?

TODO

Last updated