Programming in C++: Assignment Week 8

Similar documents
Programming in C++: Assignment Week 8

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

CPSC 427: Object-Oriented Programming

CSC 330 Object-Oriented Programming. Exception Handling CSC 330

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

TEMPLATES AND EXCEPTION HANDLING

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

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

AN OVERVIEW OF C++ 1

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

G52CPP C++ Programming Lecture 16

CMSC 202 Midterm Exam 1 Fall 2015

Exercise 1.1 Hello world

W3101: Programming Languages C++ Ramana Isukapalli

CPSC 427: Object-Oriented Programming

C++ For Science and Engineering Lecture 15

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

Templates and Vectors

C++ Namespaces, Exceptions

Recharge (int, int, int); //constructor declared void disply();

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

C++ Exception Handling. Dr. Md. Humayun Kabir CSE Department, BUET

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

C++_ MARKS 40 MIN

Midterm Review. PIC 10B Spring 2018

CS 247: Software Engineering Principles. ADT Design

Introduction Of Classes ( OOPS )

Lecture 8. Exceptions, Constructor, Templates TDDD86: DALP. Content. Contents Exceptions

Computer Programming

Chapter 15 - C++ As A "Better C"

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Intermediate Programming, Spring 2017*


Exceptions, Templates, and the STL

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

TEMPLATE IN C++ Function Templates

Lab Instructor : Jean Lai

CSCE 110 PROGRAMMING FUNDAMENTALS

CMPS 221 Sample Final

The format for declaring function templates with type parameters

C++ Exception Handling 1

CS3157: Advanced Programming. Outline

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Interview Questions of C++

CPSC 427: Object-Oriented Programming

Short Notes of CS201

Homework 4. Any questions?

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

CS201 - Introduction to Programming Glossary By

Your first C++ program

Function Overloading

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

Constructor - example

CS

Generics Concurrency Exceptions (examples in C++, Java, & Ada)

Object Oriented Design

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

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Object-Oriented Principles and Practice / C++

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Variables. Data Types.

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

l Operators such as =, +, <, can be defined to l The function names are operator followed by the l Otherwise they are like normal member functions:

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

05-01 Discussion Notes

Module 7 b. -Namespaces -Exceptions handling

COEN244: Class & function templates

Week 3: Pointers (Part 2)

CS 11 C++ track: lecture 1

! Operators such as =, +, <, can be defined to. ! The function names are operator followed by the. ! Otherwise they are like normal member functions:

The University of Nottingham

Lecture 10. To use try, throw, and catch Constructors and destructors Standard exception hierarchy new failures

Data Structures using OOP C++ Lecture 3

CSCE 206: Structured Programming in C++

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Polymorphism. Zimmer CSCI 330

2 nd Week Lecture Notes

Getting started with C++ (Part 2)

Fundamentals of Programming. Lecture 19 Hamed Rasifard

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

COMP322 - Introduction to C++

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

G52CPP C++ Programming Lecture 20

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

Fast Introduction to Object Oriented Programming and C++

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Scientific Computing

Introduction to C++ Systems Programming

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips

Introduction to Programming using C++

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

GEA 2017, Week 4. February 21, 2017

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Transcription:

Programming in C++: Assignment Week 8 Total Marks : 20 September 9, 2017 Question 1 Consider the following code segment. Mark 2 void myfunction(int test) { try { if (test) throw test; else throw "Value is zero"; catch (int i) { cout << "CaughtOne "; catch (const char *str) { cout << "CaughtString "; What will be displayed for the following function calls in main(). myfunction(-3); myfunction(0); a) CaughtOne Value is zero b) CaughtOne CaughtString c) CaughtOne CaughtOne d) CaughtString Value is zero Answer: b) Explanation: A non-zero integer value is considered as true in C++. Hence, for the function call myfunction(-3), CaughtOne will be displayed. CaughtString will be displayed for myfunction(0). For a detailed explanation, visit the course video lectures for exception handling in C++. Question 2 Consider the following Template. Mark 2 1

template <class T> T Comp(T x, T y) { return x + y; If a = 3.1, b = 5, c = 2, d = 3.7 and if a, b, ic are declared as int and c, d, dc as double. What will be stored in ic and dc for the following calls ic = Comp(a, b); dc = Comp(c, d); in main(). a) 8 and 5 b) 8.1 and 5 c) 8.1 and 5.7 d) 8 and 5.7 Answer: d) Explanation: a is declared as int hence 3.1 will gets converted to 3 implicitly. Further, ic is declared as int, the result will be integer (8). The result will be double (5.7) for the call dc = Comp(c, d) since all variables are declared as double. Question 3 Consider the following code segment. Assume that the sizeof(double)= 8. What will be the size of the object derived? Mark 2 class base { double arr[5]; ; class base1 : public base { ; class base2: public base { ; class derived: public base1, public base2 {; a) 40 b) 20 c) 80 d) 88 Answer: c) Explanation: Class derived ISA a base1 and base2 and hence inherits all its data and function members. But, memory will be allocated only for data members. Hence, the total memory will be 80 (5 (array elements) * 8 (double size) + 5 (array elements) * 8 (double size)) 2

Question 4 Consider the following Class Template. Mark 2 template<class T=int, class U=int> class Test { T x; U y; public: Test(T t, U u): x(t), y(u) { void display() { cout << x << "," << y << endl; ; What will be the output for the following instantiations in main()? Test<char, char>b( a, b ); Test<>c( a, 12.9); a) a,b a,12.9 b) a,b 97,12 c) a,b a,12 d) 97,b 97,12 Answer: b) Explanation: Default arguments to a class template. Since by default, arguments are int to the template. When we make a statement Test<>c( a, 12.9); without any explicit data types within <>, all arguments will be converted to int type. Question 5 What will be the output of the following program? Mark 2 #include <iostream> using namespace std; class Test { public: Test() { cout << "Created" << endl; ~Test() { cout << "Destroyed " << endl; ; int main() { try { Test t1; throw 98; catch(char i) { 3

cout << "Caught Char " << i << endl; catch(double i) { cout << "Caught Double " << i << endl; catch(...) { cout << "Default" << endl; return 0; a) Created Destroyed Caught Char b Caught Double 98 b) Created Caught Char b Caught Double 98 c) Created Caught Double 98 d) Created Destroyed Default Answer: d) Explanation: Object instantiation calls the constructor and then calls the destructor when the scope ends. Since 98 is neither char nor double, catch(...) will be called. Refer course materials for more details on exception handling. Programming Assignment Question 1 Fill up the missing code segment by following the comments below such that output matches the test cases. Marks 3 #include <iostream> using namespace std; /* Write the function header and body of display() which takes single generic parameter and prints it s value */ // Provide suitable template signature void display( x) { // Make x as a generic parameter cout << x << " "; 4

/* Write overladed display() which takes two generic parameters and prints the values */ // Provide suitable template signature void display( x, y) { // Make x and y as generic parameters cout << x << " " << y << endl; int main() { double d; int i; char c; cin >> i; cin >> d; cin >> c; display(c); display(i, d); display(c, d); return 0; Answer: template <class T> void display(t x) { cout << x << " "; template <class T1, class T2> void display(t1 x, T2 y) { cout << x << " " << y << endl; Explanation: Concept of overloading. Here, we are overloading the function display() with generic parameters. a. Input: 10 12.2 c c 10 12.2 c 12.2 b. Input: 5

8 8 a a 8 8 a 8 c. Input: 2 10.8 g g 2 10.8 g 10.8 Question 2 Consider the following code. Write the correct swap function to match the test cases. Marks 3 #include <iostream> #include <string> using namespace std; // ----------------- Write the Swap function here --------------------- // --------- You cannot write more than one Swap function ------------- // -------------------------------------------------------------------- int main() { int a, b; double s, t; string mr, ms; cin >> a >> b; cin >> s >> t; cin >> mr >> ms; Swap(a, b); Swap(s, t); Swap(mr, ms); cout << a << " " << b << endl; cout << s << " " << t << endl; cout << mr << " " << ms; return 0; 6

Answer: template <typename T> void Swap(T& x, T& y) { T tmp = x; x = y; y = tmp; Explanation: Use template to swap generic parameters. a. Input: 5 2 11.66 3.3 come wel 2 5 3.3 11.66 wel come b. Input: 20 30 2.2 3.3 hello world 30 20 3.3 2.2 world hello c. Input: 9 15 77.7 88.8 program c++ 15 9 88.8 77.7 c++ program Question 3 Consider the following code. Write the correct Min function which returns the minimum of two values to match the test cases. Marks 2 #include <iostream> using namespace std; // ----------------- Write the Min() function here --------------------- // --------- You cannot write more than one Min() function ------------- // -------------------------------------------------------------------- int main() { int a, b, imin; cin >> a >> b; double c, d, dmin; cin >> c >> d; 7

imin = Min(a, b); cout << "Min(" << a << ", " << b << ") = " << imin << endl; dmin = Min(c, d); cout << "Min(" << c << ", " << d << ") = " << dmin << endl; return 0; Answer: template<class T> T Min(T x, T y) { return x < y? x : y; Explanation: Use template to find the min. of two values that are of two different type. a. Input: 2 3-2.2 3.3 Min(2, 3) = 2 Min(-2.2, 3.3) = -2.2 b. Input: 8 0 0.0 3.0 Min(8, 0) = 0 Min(0, 3) = 0 c. Input: -2-10 -8 8 Min(-2, -10) = -10 Min(-8, 8) = -8 8

Question 4 Consider the following code. Insert the code for error handling using exception, in editable section to match the test cases. Marks 2 #include <iostream> #include <exception> using namespace std; class myexception : { // Inherit exception with appropriate visibility virtual const char* what() const throw() { return "DivideByZero"; ; class DivideByZero { public: int numerator, denominator; DivideByZero(int a = 0, int b = 0) : numerator(a), denominator(b) { int divide(int numerator, int denominator){ if (denominator == 0) { // Call exception suitably to handle divide by zero error return (numerator / denominator); ; int main() { DivideByZero d; int a, b; cin >> a >> b; try { d.divide(a, b); catch (exception& e) { cout << e.what() << endl; return 0; Answer public exception // throw myexception(); Input: 9

20 0 DivideByZero Input: 3.3 0 DivideByZero Input: 10 0 DivideByZero 10