EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

Similar documents
IS0020 Program Design and Software Tools Midterm, Fall, 2004

Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Review: C++ Basic Concepts. Dr. Yingwu Zhu

More Advanced Class Concepts

Intermediate Programming & Design (C++) Classes in C++

Review. What is const member data? By what mechanism is const enforced? How do we initialize it? How do we initialize it?

Homework #3 CS2255 Fall 2012

Ch. 12: Operator Overloading

Use the dot operator to access a member of a specific object.

Overloaded Operators, Functions, and Students

Operator overloading. Instructor: Bakhyt Bakiyev

A <Basic> C++ Course

A <Basic> C++ Course

Overloading Operators in C++

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

Object oriented programming

Come and join us at WebLyceum

Circle all of the following which would make sense as the function prototype.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Object-Oriented Principles and Practice / C++

Due Date: See Blackboard

Intermediate Programming, Spring 2017*

Ch 8. Operator Overloading, Friends, and References

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

pointers + memory double x; string a; int x; main overhead int y; main overhead

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

CS201- Introduction to Programming Current Quizzes

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Polymorphism. Zimmer CSCI 330

Program construction in C++ for Scientific Computing

CS 247: Software Engineering Principles. ADT Design

Concepts (this lecture) CSC 143. Classes with Dynamically Allocated Data. List of ints w/dups (cont.) Example: List of ints with dups.

Programming, numerics and optimization

Overloading & Polymorphism

Do not write in this area TOTAL. Maximum possible points: 75

CS 162, Lecture 25: Exam II Review. 30 May 2018

Introduction to Computer Science Midterm 3 Fall, Points

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

SFU CMPT Topic: Class Templates

CS201 Latest Solved MCQs

14.1. Chapter 14: static member variable. Instance and Static Members 8/23/2014. Instance and Static Members

Due Date: See Blackboard

Shahram Rahatlou. Computing Methods in Physics. Overloading Operators friend functions static data and methods

CPSC 427: Object-Oriented Programming

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan

C++ 8. Constructors and Destructors

Object oriented programming

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

11.2. Overloading Operators

Name: Username: I. 10. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Lecture 3 ADT and C++ Classes (II)

Quiz Start Time: 09:34 PM Time Left 82 sec(s)


Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Introduction to Programming using C++

Set Implementation Version 1

KEY printed. Do not start the test until instructed to do so! CS 2704 Object-Oriented Software Design and Construction Test 1.

Operator Overloading in C++ Systems Programming

pointers & references

Searching Algorithms. Chapter 11

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Inheritance and Polymorphism

EL2310 Scientific Programming

CS11 Advanced C++ Spring 2018 Lecture 1

Midterm Review. PIC 10B Spring 2018

Binary Tree Implementation

Documentation. Programming / Documentation Slide 42

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Lecture 2. Binary Trees & Implementations. Yusuf Pisan

Object-Oriented Design (OOD) and C++

CS11 Intro C++ Spring 2018 Lecture 5

Object Oriented Programming. Solved MCQs - Part 2

CSC 330 Object Oriented Programming. Operator Overloading Friend Functions & Forms

STRUCTURING OF PROGRAM

VIRTUAL FUNCTIONS Chapter 10

Assignment of Objects

Module 1. C++ Classes Exercises

Short Notes of CS201

Collected By Anonymous

CMPT 135: Midterm Answer Key

C++ (Non for C Programmer) (BT307) 40 Hours

CS201 - Introduction to Programming Glossary By

University of Swaziland Department Of Computer Science Supplementary Examination JULY 2012

Program template-smart-pointers-again.cc

CS105 C++ Lecture 7. More on Classes, Inheritance

Outline. A C++ Linked Structure Class A C++ Linked List C++ Linked Dynamic Memory Errors In-class work. 1 Chapter 11: C++ Linked Structures

Roxana Dumitrescu. C++ in Financial Mathematics

Module Operator Overloading and Type Conversion. Table of Contents

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

SFU CMPT Topic: Has-a Relationship

CS250 Final Review Questions

Stacks and their Applications

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Lecture 23: Pointer Arithmetic

C++_ MARKS 40 MIN

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CS250 Final Review Questions

CPSC 427: Object-Oriented Programming

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Transcription:

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