Problem 24
Consider the following C++ code: int *p; p = new int[10]; for (int j = 0; j < 10; j++) p[i] = 2 * j - 2; Write the C++ statement that deallocates the memory space occupied by the array to which p points.
Problem 25
Explain the difference between a shallow copy and a deep copy of data.
Problem 26
What is wrong with the following C++ code? int *p; //Line 1 int *q; //Line 2 p = new int[5]; //Line 3 *p = 2; //Line 4 for (int i = 1; i < 5; i++) //Line 5 p[i] = p[i - 1] + i; //Line 6 q = p; //Line 7 delete [] p; //Line 8 for (int j = 0; j < 5; j++) //Line 9 cout << q[j] << " "; //Line 10 cout << endl; //Line 11
Problem 27
What is the output of the following C++ code? int *myList = new int[5]; int *yourList = new int[10]; myList[0] = 3; for (int i = 1; i < 5; i++) myList[i] = myList[i - 1] + i; for (int i = 0; i < 5; i++) { yourList[i] = myList[i] + 4; yourList[i + 5] = myList[4 - i] - 3; } cout << "myList: "; for (int i = 0; i < 5; i++) cout << myList[i] << " "; cout << endl; cout << "yourList: "; for (int i = 0; i < 10; i++) cout << yourList[i] << " "; cout << endl;
Problem 28
a. Write a statement that declares sales to be a pointer to a pointer of type double. b. Write a C++ code that dynamically creates a two-dimensional array of five rows and seven columns and sales contains the base address of that array. c. Write a C++ code that inputs data from the standard input device into the array sales. d. Write a C++ code that deallocates the memory space occupied by the two- dimensional array to which sales points.
Problem 29
What is the purpose of a copy constructor?
Problem 32
Suppose that you have the following classes, classA and classB: class classA { public: virtual void print() const; void doubleNum(); classA(int a = 0); private: int x; }; void classA::print() const { cout << "ClassA x: " << x << endl; } void classA::doubleNum() { x = 2 * x; } classA::classA(int a) { x = a; } class classB: public classA { public: void print() const; void doubleNum(); classB(int a = 0, int b = 0); private: int y; };void classB::print() const { classA::print(); cout << "ClassB y: " << y << endl; } void classB::doubleNum() { classA::doubleNum(); y = 2 * y; } classB::classB(int a, int b) : classA(a) { y = b; } What is the output of the following function main? int main() { classA *ptrA; classA objectA(2); classB objectB(3, 5); ptrA = &objectA ptrA->doubleNum(); ptrA->print(); cout << endl; ptrA = &objectB ptrA->doubleNum(); ptrA->print(); cout << endl; return 0; }
Problem 34
What is the difference between compile-time binding and run-time binding?
Problem 35
Is it legal to have an abstract class with all member functions pure virtual?
Problem 36
Consider the following definition of the class studentType: public studentType: public personType { public: void print(); void calculateGPA(); void setID(long id); void setCourses(const string c[], int noOfC); void setGrades(const char cG[], int noOfC); void getID(); void getCourses(string c[], int noOfC); void getGrades(char cG[], int noOfC); void studentType(string fName = "", string lastName = "", long id, string c[] = NULL, char cG[] = NULL, int noOfC = 0); private: long studentId; string courses[6]; char coursesGrade[6] int noOfCourses; } Rewrite the definition of the class studentType so that the functions print and calculateGPA are pure virtual functions.