Objective: 1. Become familiar with design and creation of new classes. 2. Become
ID: 3621657 • Letter: O
Question
Objective:1. Become familiar with design and creation of new classes.
2. Become familiar with the concept of operator overloading.
3. More exercises in dynamic memory management.
Description:
Develop an array class that addresses some of the short comings of the standard c++ array implementation.
This class should have the following attributes:
1. Named intArray
2. One dimensional integer type
3. Dynamic in size
4. Overload the following operators: - (unary negation), +, -, ==, !=, =, << and >>
5. Your class should contain at least a default constructor and a copy constructor
6. Assignment (=), addition (+), subtraction (-), string insertion (<<), and string extraction (>>) operations need to be capable of acasscaded operations.
This program must have the "main.cpp", "intArray.cpp", and "intArray.h" files.
Explanation / Answer
This should get you started. The idea is that the class will have a pointer into the heap (dynamic memory). This is denoted as a * type. So, for a dynamic array of ints, we declare m_array as "int *m_array" The rest is just syntax. INITIAL_SIZE = 100; 1. class intArray { 2. int *m_array; // a pointer to our dynamic array int m_size; 5. intArray() { m_array = new int[INITIAL_SIZE]; m_size = INITIAL_SIZE } // default constructor intArray(const intArray ©) { m_array = new int[copy.m_size]; m_size = copy.m_size; memcpy(m_array, copy.m_array); } // copy constructor /* Bonus: Destructor */ ~intArray() { delete m_array; } // free up the memory we allocated in the constructor/copy constructor For help on overloading operators, see this really great explanation: http://www.cs.caltech.edu/courses/cs11/material/cpp/donnie/cpp-ops.html Overloaded operators are really just functions with special names. Good luck!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.