Homework 2
1. For the class called IntArray implement a deep copy constructor, a move constructor, a destructor, and a move assignment operators. Use the std::swap function if appropriate, and below where you used the std::swap function, write as a comment the code to perform the same steps without using the std::swap function.Implement the methods outside the class interface.You may implement the put and get methods to test your code. You will not be graded on these methods.Nicolai, our TA, has created a unit test for this question. Your code must pass this unit test. The unit test is in a file called main.cc.class IntArray{ public:IntArray(int n = 0): size(n), array(new int[n]){}// add your methods herevoid put (int i, int value); int get( int i); private:int * array; int size;};2. Write a functor to determine if an integer is even or odd.3. Write a functor that compares two strings, str1 and str2. If str1s length is less than str2s length, your functor returns true. Otherwise it returns false.Written Part1. What is printed if you ran the following code. Would this be what you wanted to happen? Is there anything that goes wrong?class IntArray{ public:IntArray(int n = 0): size(n), array(new int[n]){} void put (int i, int value); int get( int i); private:int * array; int size;}; void someFun( ){IntArray myA1( 1 );IntArray myA2( myA1 ); IntArray myA3( 1 ); myA1.put( 0, 12 ); cout << myA1.get( 0 ) << endl; myA2.put( 0, 10 ); cout << myA1.get( 0 ) << endl; myA3.put( 0, 5 ); myA1 = myA3; cout << myA1.get( 0 ) <=1; k = (int) k/3) sum += k;cout << sum;(b) for (int j = 1; j 1; i/=9)cout << “(” << i << “, ” << j << “) ” ;cout << endl;}(c) template int min(const vector & items){ if (items.size() == 0) return -1; int min_index = 0;for (int i = 0; i < items.size(); ++i) if (items[i] < items[min_index])min_index = i;return min_index;}(d) for( i = 0; i < n; ++i) for (j = 0; j < n; ++j)a[i][j] = b[i][j] + c[i][j];(e) void in_order( vector & a ){int j = 0; for( int p = 1; p 0 && tmp < a[ j 1 ]; j ) a[ j ] = a[ j 1 ];a[ j ] = tmp;}}(f) void f( vector v ){if ( v.size( ) > 0 ) v[0] = 1;} int main( ){// some code to create a vector of size n that you do not include in your run time f( v );}3. What is the difference between delete [] and delete?4. For programming problem 1, Using big-Oh notation if size = n, give the worst case running time for the:(a) copy constructor(b) move constructor5. Using big-Oh notation what is the running time of the following code snippets where v is a vector of n > 0 integers.(a) void f( vector b ){ cout << b[0];} int main( ){// code to create the vector v which will not be included in your run time f( v );}(b) void f( vector & b ){ cout << b[0];} int main( ){// code to create the vector v which will not be included in your run time f( v );}6. If your code contained the following functions// Function 1 void f( const string & s ){ cout << “Function 1 was called.” << endl;}// Function 2 void f( string & s ){ cout << “Function 2 was called.” << endl;}// Function 3 void f( string && s ){ cout << “Function 3 was called.” << endl;}State which function was called for each of the following.(a) f(string(“hello”));(b) string s1 = “hello”; f(s1);(c) string s2 = “hello”; f( std::move(s2) );(d) const string s3 = “hello”; f(s3);7. Determine if the specified expression is an rvalue or an lvalue: int x = 3; // what is x char * ptr = new char[3]; // what is new char[3] int f( ) return 1; // What is the return value of the function f string && sRef = string(“What?”); // what is sRef8. Anything wrong with this code? If there is, please describe what the problem is, otherwisestate there is no problem.string & getName( ){ string name; cout <> name; cout << “Hi ” << name << ! << endl; return name;} int main( ){ cout << getName( );}
Reviews
There are no reviews yet.