EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science Written examination Homologation C++ and Computer Organization (2DMW00) Part I: C++ - on Tuesday, November 1st 2016, 9:00h-12:00h. Hand-in the answers to this 1st part of the examination on separate sheets of paper, and make sure to put your name and identification number on the paper. Read all the questions carefully. Select one of more answers, when needed, marking the selected answers. Points for each question are indicated between parentheses and sum up to 10 points. No laptop or smart devices are allowed to be used. Good luck! 1. (0.25) What is the output of the following program? int main() { int n = 210, m = 110; funcone(n, m); cout << n = << n <<, m = << m << endl; return 0; void funcone(int n, int &m) { n = 240; m = 125 a. n = 240, m = 110 b. n = 240, m = 125 c. n = 210, m = 110 d. n =210, m = 125 2. (0.25) Which of the following functions will increment an integer passed as argument to the functions? void inc1(int a) { a++; void inc2(int *p) { p++; void inc3(int *p) { (*p)++; void inc4(int &i) { i++; a. Function inc1 and inc4 b. Functions inc2 and inc1
c. Functions inc3 and inc1 d. Functions inc3 and inc4 3. (0.25) Which constructor function is designed to copy objects of the same class type? a. Create constructor b. Object constructor c. Dynamic constructor d. Copy constructor 4. (0.25) Which of the following function declaration is/are incorrect? a. int sum(int a, int b = 2, int c = 3); b. int sum(int a = 5, int b); c. int sum(int a = 0, int b, int c = 3); d. Both B and C are incorrect. e. All are correct. 5. (0.25) A protected member of the class is accessible in a. Only same class b. Same class and derived class c. Outside the class d. None of the above. 6. (0.25) What does a class hierarchy depict? a. It shows the relationships between the classes in the form of an organization chart. b. It describes "has a" relationships. c. It describes "kind of" relationships. d. It shows the same relationship as a family tree. 7. (0.25) Choose the pure virtual function definition from the following: a. virtual void f() = 0 { b. void virtual f() = 0 { c. virtual void f() { = 0; d. None of the above.
8. (0.25) Choose the respective delete operator usage for the expression ptr=new int[100]. a. delete ptr; b. delete ptr[]; c. delete[] ptr; d. []delete ptr; 9. (6.0) Create a class Calculator which contains only a single component of type double. The class implements the four arithmetic operations: addition, subtraction, multiplication and division. The class can be used, by example, in this way: Calculator a(20), b(30), c, d, e, f; c = a + b; d = a b; e = a * b; f = b / a; cout << "c = " << c; The structure of the class looks like : private: double a; // The constructor // Overloaded the + operator // Overloaded the - operator // Overloaded the * operator // Overloaded the / operator // Overloaded the input >> operator // Overloaded the output << operator ; Implement or overload the methods of the class: a. the constructor b. the + operator c. the - operator d. the * operator e. the / operator f. the input >> operator g. the output << operator and write a test program to test the class methods.
Solution // calculator.h file // define the methods // Constructor Calculator(double a = 0) : a(a) { // overload the operators Calculator operator+(const Calculator& c) { return Calculator(a + c.a); Calculator operator-(const Calculator& c) { return Calculator(a - c.a); Calculator operator*(const Calculator& c) { return Calculator(a * c.a); Calculator operator/(const Calculator& c) { if (c.a!= 0.0) return Calculator(a / c.a); else cout << "Division by zero\n"; return Calculator(1); // define the output stream operator friend ostream& operator<<(ostream& output, const Calculator& c) { output << "Calculator a = " << c.a << endl; return output; // define the input stream operator friend istream& operator>>(istream& input, Calculator& c) { cout << "Typein the value of a: "; input >> c.a; return input; // atrribute double a; ; // end calculator.h // mainprogram for testing #include "calculator.h" int main() { Calculator c1, c2(10), c3(100); cout << c1 << c2 << endl; cout << c3 + c2 << endl; cout << c3 - c2 << endl; cout << c3 * c2 << endl; cout << c3 / c2 << endl; // end mainprogram.cpp
10. (2.0) Create a class Calculator which contains only a single component of type double * (pointer to double). The class can be used, for example, in this way: Calculator a(20), b(30), c; c = a + b; count << "c = " << c; The structure of the class looks like: private: double *a; // The constructor // The copy constructor // The destructor // The assignment = operator // Overloaded the + operator ; Implement or overload only the following methods: a. the constructor b. the destructor c. the + operator Note: You can get bonus points (0.5 points per operator) if you can implement more operators (-, *, /, =) or the copy constructor for the class defined in question 10! Solution // calculator.h file // define the methods // Constructor Calculator(double a = 0) { // allocate memory
this->a = new double; *(this->a) = a; // copy constructor Calculator(const Calculator& c) { // allocate memory this->a = new double; // copy data *(this->a) = *(c.a); // define the destructor ~Calculator() { //de-allocate the data delete a; // overload the operators Calculator operator+(const Calculator& c) { return Calculator(*a + *(c.a)); Calculator operator-(const Calculator& c) { return Calculator(*a - *(c.a)); Calculator operator*(const Calculator& c) { return Calculator(*a * *(c.a)); Calculator operator/(const Calculator& c) { if (*(c.a)!= 0.0) return Calculator(*a / *(c.a)); else cout << "Division by zero\n"; return Calculator(1); // the assignment operator Calculator& operator=(const Calculator& c) { if (this!= &c) {// test the situation c = c // *a = *(c.a); return *this; // for c1=c2=c3...=c3 case // define the output stream operator friend ostream& operator<<(ostream& output, const Calculator& c) { output << "Calculator a = " << *(c.a) << endl; return output; // define the input stream operator friend istream& operator >> (istream& input, Calculator& c) { cout << "Typein the value of a: "; input >> *(c.a); return input; // attribute double *a; ; // end calculator.h file // mainprogram for testing #include "calculator.h" int main()
{ Calculator c1, c2(10), c3(100); cout << c1 << c2 << endl; cout << c3 + c2 << endl; cout << c3 - c2 << endl; cout << c3 * c2 << endl; cout << c3 / c2 << endl; // end mainprogram.cpp