This is a CLOSED-BOOK-CLOSED-NOTES exam consisting of five (5) questions. Write your answer in the answer booklet provided. 1. OO concepts (5 points)

Similar documents
This is a CLOSED-BOOK-CLOSED-NOTES exam consisting of five (5) questions. Write your answer in the answer booklet provided. 1. OO concepts (5 points)

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

G52CPP C++ Programming Lecture 18

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

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

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

Midterm Review. PIC 10B Spring 2018

Fast Introduction to Object Oriented Programming and C++

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

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

ECE 462 Object-Oriented Programming using C++ and Java Design Issues and Multiple Inheritance in C++

This examination has 11 pages. Check that you have a complete paper.

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

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

CSE 333 Lecture 9 - intro to C++

ECE 462 Midterm Exam 1. 10:30-11:20AM, September 21, 2007

Topics. bool and string types input/output library functions comments memory allocation templates classes

PENN STATE UNIVERSITY Department of Economics

Lecture on pointers, references, and arrays and vectors

G52CPP C++ Programming Lecture 17

Intermediate Programming, Spring 2017*

CSE 333 Lecture smart pointers

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Introduction to Programming using C++

Homework 4. Any questions?

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

Programming, numerics and optimization

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

CSE 333 Lecture smart pointers

Programming in C++: Assignment Week 5

Review Questions for Final Exam

AN OVERVIEW OF C++ 1

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

CS197c: Programming in C++

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

EL2310 Scientific Programming

Templates and Vectors

Short Notes of CS201

Chapter 1: Object-Oriented Programming Using C++

CS 211 Winter 2004 Sample Final Exam (Solutions)

C++ Primer for CS175

The Standard Template Library. An introduction

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

CS201 - Introduction to Programming Glossary By

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

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

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

Inheritance, and Polymorphism.

Spring Semester, Dr. Punch. Exam #2 (03/28), form 2 C

Module 9. Templates & STL

CSE 303: Concepts and Tools for Software Development

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

EL2310 Scientific Programming

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

Introduction to C++ (Extensions to C)

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

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

CPSC 427: Object-Oriented Programming

Polymorphism Part 1 1

CMSC 202 Midterm Exam 1 Fall 2015

CS 247: Software Engineering Principles. ADT Design

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

Where do we go from here?

Praktikum: Entwicklung interaktiver eingebetteter Systeme

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Introduction to C++ Systems Programming

Crash Course into. Prof. Dr. Renato Pajarola

IS 0020 Program Design and Software Tools

EL2310 Scientific Programming

use static size for this buffer

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Review Questions for Final Exam KEY

C++: Overview and Features

Midterm Exam 5 April 20, 2015

CSC1322 Object-Oriented Programming Concepts

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

/************************************************ * CSC 309/404 Review for Final -- Solutions * ************************************************/

A Tour of the C++ Programming Language

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

CS201 Some Important Definitions

Inheritance and Polymorphism

Polymorphism. Arizona State University 1

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

C10: Garbage Collection and Constructors

The Ada Standard Generic Library (SGL)

Polymorphism. Zimmer CSCI 330

CS 7B - Spring Final Exam

STL: C++ Standard Library

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Final Examination 8:30 AM, Wednesday, April 18, 2012

1. The term STL stands for?

A Tour of the C++ Programming Language

Exam 3 Chapters 7 & 9

Evolution of Programming Languages

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Transcription:

COMP152H Object Oriented Programming and Data Structures Spring Semester 2011 Midterm Exam March 22, 2011, 9:00-10:20am in Room 3598 Instructor: Chi Keung Tang This is a CLOSED-BOOK-CLOSED-NOTES exam consisting of five (5) questions. Write your answer in the answer booklet provided. 1. OO concepts (5 points) (a) What is polymorphism in object-oriented programming? (b) Express in less than 30 words how the following concepts are related: dynamic polymorphism, dynamic binding, virtual functions, overriding, static polymorphism, template. 2. Abstract Base Class (5 points) The following shows the implementation of three classes derived from Animal. The main program and its output are also shown. Code up the Animal base class. Use member initializers whenever possible when you define constructors. class Cat: public Animal Cat(std::string strname) : Animal(strName) const char* Speak() return "Meow"; ; class Dog: public Animal Dog(std::string strname) : Animal(strName) const char* Speak() return "Woof"; ; class Cow: public Animal Cow(std::string strname) : Animal(strName) const char* Speak() return "Moo"; ; // Animal animal; cannot declare Animal object; compilation error Animal *animal_arr[3]; Cow ccow("betsy"); Dog ddog("lucky"); Cat ccat("tom"); animal_arr[0] = &ccow; animal_arr[1] = &ddog; animal_arr[2] = &ccat; for (int i=0; i<3; i++) std::cout << animal_arr[i]->getname() << " says " << animal_arr[i]->speak() << "... "; The output is Betsy says Moo... Lucky says Woof... Tom says Meow... 1

3. Inheritance (10 points) You are given toy.h and a main program in the following. Your task is to code up toy.cpp so that the main program runs and produces the output on the orders of construction and destruction. Note that a dynamic array is allocated when an object is instantiated in both the base class and derived class. Your implementation must not have memory leak or dangling pointers, and you must use member initializer when defining constructors as much as possible. // toy.h class B B(int n = 10); // initialize a dynamic array iarr of size n virtual ~B(); // should deallocate memory static int bcnt; const int MAX_iSIZE; // maximum size of iarr int *iarr; ; class D : public B D(int n = 10); // initialize a dynamic array carr of size n virtual ~D(); // should deallocate memory static int dcnt; const int MAX_cSIZE; // maximum size of carr char *carr; ; // main program B** b; b = new B*[2]; for (int i=0;i<2;i++) b[i] = new D(10); for (int i=0;i<2;i++) delete b[i]; delete b; The output is B constructed: 1 D constructed: 1 B constructed: 2 D constructed: 2 D destructed: 2 B destructed: 2 D destructed: 1 B destructed: 1 4. Template and operator overloading (10 points) You are given the following class template specification and a main program where its sample output is also given. Your tasks are (a) implement the four member functions; use member initializer as much as possible when defining constructors; 2

(b) overload operator+ as a member function and operator<< as a non-member function; you can assume the types to be instantiated T1 and T2 are primitive types (one example is shown in the main program). You must not implement functions other than the above to make the main program run. You can define all member functions within class template Tuple (which is probably easier with less syntax to bother about), or outside of the class template. class Tuple Tuple (); // assume default values of data members to be 0 Tuple (const T1& t1, const T2& t2); // specify the function prototype for overloading the + operator T1 get_ele1() const; T2 get_ele2() const; T1 ele1; T2 ele2; ; Tuple<int,double> t1 (2, 4.5); Tuple<int,double> t2 (4, 3.8); Tuple<int,double> t3 = t1 + t2; cout << t3 << endl; // output is // (6, 8.3) 5. STL and iterators (5 points) Complete the following program using STL algorithms copy and sort and iterators. A total of only three statements is needed. #include <stdlib.h> #include <vector.h> #include <string> #include <algorithm> #include <iostream> using namespace std; typedef istream_iterator<int> istream_iterator_int; vector<int> v; istream_iterator_int start (cin); istream_iterator_int end; back_insert_iterator<vector<int> > dest (v); // use stl s copy to read integers from cin and copy them to v // use stl s sort to sort v // use stl s to copy the sorted v to cout, each number followed by end of line 3

SOLUTIONS TO MIDTERM 1. OO concepts (5 points) (a) Polymorphism in object-oriented programming means that we can work with objects without knowing their precise type at compile time; in C++, the same function can respond differently depending on the actual object being processed. (b) In dynamic polymorphism, dynamic binding is necessary, which is implemented using virtual functions in an inheritance setting to achieve overriding. To achieve static polymorphism, template is used in C++. (29 words) 2. Abstract Base Class (5 points) class Animal std::string m_strname; Animal(std::string strname) : m_strname(strname) ; std::string GetName() return m_strname; virtual const char* Speak() = 0; 3. Inheritance (10 points) int B::bcnt = 0; int D::dcnt = 0; B::B(int n) : iarr(new int [n]), MAX_iSIZE(n) B::bcnt++; printf ("B constructed: %d\n", B::bcnt); B::~B() delete [] iarr; printf ("B destructed: %d\n", B::bcnt); B::bcnt--; D::D(int n) : B(n), carr(new char [n]), MAX_cSIZE(n) D::dcnt ++; printf ("D constructed: %d\n", D::dcnt); 4

D::~D() delete [] carr; printf("d destructed: %d\n", D::dcnt); D::dcnt --; 4. Template and operator overloading (10 points) class Tuple Tuple () : ele1(0), ele2(0) Tuple (const T1& t1, const T2& t2): ele1(t1), ele2(t2) Tuple operator+ (const Tuple& t) const return Tuple (ele1 + t.ele1, ele2 + t.ele2); T1 get_ele1() const return ele1; T2 get_ele2() const return ele2; T1 ele1; T2 ele2; ; /* // if defined outside of the class Tuple<T1,T2> Tuple<T1,T2>::operator+ (const Tuple<T1,T2> &t) const return Tuple<T1,T2> (ele1 + t.ele1, ele2 + t.ele2); */ ostream& operator<< (ostream& os, const Tuple<T1,T2> &t) os << "(" << t.get_ele1() << ", " << t.get_ele2() << ")" ; return os; 5. STL and iterators (5 points) // use stl s copy to read integers from cin and copy them to v copy (start, end, dest); // use stl s sort to sort v sort(v.begin(), v.end()); // use stl s to copy the sorted v to cout, each number followed by end of line copy (v.begin(), v.end(), ostream_iterator<int>(cout, "\n")); 5