Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/cs104/copyassign/intarray_assign by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
Add the assignment operator to your class.
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/cs104/copyassign/intarray_copycon (author: redekopp@usc.edu)
Copied from problem cpp/cs104/copyassign/intarray_destructor (author: redekopp@usc.edu)
Copied from problem cpp/cs104/copyassign/intarray1 (author: redekopp@usc.edu)
remove
Engine
Template / Reference solution
 
#include <iostream>
using namespace std;
class MyArray
{
 public:
  MyArray() { cout << "In default constructor" << endl;
              dat = NULL; len = 0; }
  MyArray(int d[], int num); //normal
  MyArray(const MyArray& other); // copy constructor
\[
REDACTED
]\
  ~MyArray();
  int& operator[](int loc) { return dat[loc]; }
  int const & operator[](int loc) const { return dat[loc]; }
  int size() const         { return len;      }
 private:
  int len; int *dat;
};
// Normal constructor
MyArray::MyArray(int d[], int num)
{
  cout << "In normal constructor" << endl;
  dat = new int[num]; len = num;
  for(int i=0; i < len; i++){
    dat[i] = d[i];
  }
}
// Copy Constructor
MyArray::MyArray(const MyArray& other)
{
   cout << "In copy constructor" << endl;
   len = other.len;
   dat = new int[len];
   for(int i=0; i < len; i++){
     dat[i] = other.dat[i];  
   }
}
// Assignment operator
\[ REDACTED ]\
{
   cout << "In assignment operator" << endl;
   \[
REDACTED
]\
}
MyArray::~MyArray()
{  
 cout << "In destructor" << endl;
 delete [] dat; 
}
void printVals(const MyArray& arr)
{
  for(int i=0; i < arr.size(); i++){
    cout << arr[i] << " ";
  }
  cout << endl;
}
int main()
{
  int vals[] = {9,3,7,5};
  MyArray a1(vals,4);
  MyArray a2(a1); 
  MyArray a3 = a1; 
  MyArray a4;  
  a1[0] = 11;
  a4 = a2 = a1; // a4.operator=( a2.operator=(a1) )
  printVals(a4);
  a4[0] = 15;
  printVals(a4);
   
  return 0;
}
C++ test suite
json list of stdin/args tests
e.g. [{"stdin":"hi", "args":["4", "5"]},
{"stdin":"noargs"}]

to just run once with no input use [{}]
 
[{}]
Solution visibility remove
Is example?
i.e., just a demo
remove
Add compiler flag(s)
json list of flags
e.g. ["-Wno-unused-variable"]
 
["-Wno-unused-variable"]
remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.