Midterm Review. PIC 10B Spring 2018

Size: px
Start display at page:

Download "Midterm Review. PIC 10B Spring 2018"

Transcription

1 Midterm Review PIC 10B Spring 2018 Q1 What is size t and when should it be used? A1 size t is an unsigned integer type used for indexing containers and holding the size of a container. It is guarenteed to be large enough to hold the size, and thus any index, of any container on the system on which the code was compiled. Note that the. size () function of containers in the standard library return a size t, so to avoid loss of precision, one should always use a size t when storing the size of a container. Q2 In what situation would one use a struct rather than a class? A2 structs are the same as classes, except structs are by default public, whereas classes are by default private. Normally, structs consist solely of related data, without member functions. Q3 What is const correctness? A3 const correctness refers to the following practices: 1. Making variables that do not change const 2. Making member functions of a class that do not modify the state of the class const. Q4 How can we make the following class const correct? 1 // This c l a s s d i v i d e s a passed in double by a member double 2 class DivideBy { 3 public : 4 DivideBy ( double by ) : by ( by ) {} 5 double operator ( ) ( double x ) { 6 return x/ y ; 7 } 8 void r e s e t ( ) { 9 by = 1 ; 10 } 11 double change ( double x ) { 12 double old = by ; 13 by = x ; 14 return by ; 15 } 16 private : 17 double by ; 18 } ; A4

2 1 // This c l a s s d i v i d e s a passed in double by a member double 2 class DivideBy { 3 public : 4 DivideBy ( const double by ) : by ( by ) {} 5 double operator ( ) ( const double x ) const { 6 return x/ y ; 7 } 8 void r e s e t ( ) { 9 by = 1 ; 10 } 11 double change ( double x ) { 12 const double o l d = by ; 13 by = x ; 14 return by ; 15 } 16 private : 17 double by ; 18 } ; Q5 What is a situation when it is better to pass by value than by const reference? A5 One situation is when we need to create a copy of the passed parameter. For example, when implementing the binary operator+, operator, operator\, and operator we passed the first parameter by value. Another situation is when we are passing fundamental data types, because a reference takes up about the same amount of member as data of fundamental type. Q6 What is the purpose of separating header files and cpp files? A6 By placing the definitions of normal functions inside of.cpp files and placing the declarations inside of.h files, one is able to avoid redundant compiling of the function definitions for each translation unit that uses the functions. Specifically, instead of recompiling the entire function wherever it is used, we compile the function once, and then depend on the linker to find the correct function definition for a given function declaration. Q7 Find memory leak(s) in the following code: 1 struct Example { 2 Example ( const double x ) ptr ( x ) {} 3 Example ( const Example& e ) { 4 ptr = new double ( e. ptr ) ; 5 } 6 void changeval ( const double x ) { 7 double p = new double ( x ) ; 8 std : : swap ( ptr, p ) ; 9 } 10 double ptr ; 11 } ; 2

3 A7 There are three memory leaks. First, in the copy constructor, which will have a memory leak because the old data assigned to ptr is not deleted. Secondly, in the changeval function, since we do not delete p. Lastly, we do not provide a destructor, so the memory assigned to ptr will not be deallocated. Q8 Consider the following code. Does it have a memory leak? 1 struct Base { 2 Base ( const int x = 0) : x ( x ) {} 3 virtual void p r i n t ( ) { std : : cout << x ; } 4 int x ; 5 } ; 6 struct De riv ed : Base { 7 Derived ( const int x, const double y ) : Base ( x ), ptr (new double ( y ) ) {} 8 void p r i n t ( ) o v e r r i d e { std : : cout << x <<, << y << std : : endl ; } 9 Derived ( ) { delete ptr ; } 10 int ptr ; 11 } A8 Maybe. It is undefined behavior to call non-virtual destructors in a polymorphic class. It is possible that the following code 1 Base ptr = new Derived ( 1, 1. 5 ) ; 2 delete ptr ; will only call the destructor of Base, which will leak the data pointed to by ptr in Derived. Q9 What is RAII? A9 RAII stands for resource acquisition is initialization. It describes the practice of having memory acquisition occur when an object is created and memory deallocation occur when an object is destroyed. By consequence, code that abides by RAII should not require the programmer to use new, new[], delete, or delete[]; instead all allocation and deallocated should occur during the constructor or destruction of objects. Smart pointers allow the programmer to abide by RAII even when handling dynamically allocated memory. Q10 Create a simple integer array class that contains as member variables a raw integer pointer, a size t for the size, and a size t for the capacity. Create a default constructor, a constructor taking in a size, create copy and move constructors, and create copy and move assignment operators. Further, create member functions pop back, push back, and operator[], implemented as in the std::vector int class. Overload operator[] on const. Upon construction, set capacity to be twice the size. Keep your class free of memory leaks. A10 1 class SimpleArray { 2 public : 3 4 SimpleArray ( ) : cap ( 1 ), sz ( 0 ), a r r (new double [ cap ] ( ) ) {} 5 3

4 6 SimpleArray ( const s i z e t s z ) : cap ( s z 2), sz ( s z ), a r r (new double [ s z ] ) {} 7 8 SimpleArray ( const SimpleArray& e ) : cap ( e. cap ), sz ( e. sz ), 9 ptr (new double [ e. cap ] ) { 10 for ( s i z e t i = 0 ; i < sz ; ++i ) { 11 a r r [ i ] = e. a r r [ i ] ; 12 } 13 } // one option : unnecessary a l l o c a t i o n but puts moved from o b j e c t 16 // in a nice ( v a l i d ) s t a t e 17 SimpleArray ( Simple&& e ) : SimpleArray ( ) { 18 std : : swap ( cap, e. cap ) ; 19 std : : swap ( sz, e. sz ) ; 20 std : : swap ( arr, e. a r r ) ; 21 } // copy swap idiom : both move and copy assignment 24 // no need to check f o r s e l f assignment s i n c e a copy i s made. 25 SimpleArray& operator=( SimpleArray e ) { 26 std : : swap ( cap, e. cap ) ; 27 std : : swap ( sz, e. sz ) ; 28 std : : swap ( arr, e. a r r ) ; 29 } double& operator [ ] ( const s i z e t i ) { 32 i f ( i >= sz ) { 33 throw std : : o u t o f r a n g e ( index too l a r g e ) ; 34 } 35 return a r r [ i ] ; 36 } double operator [ ] ( const s i z e t i ) const { 39 i f ( i >= sz ) { 40 throw std : : o u t o f r a n g e ( index too l a r g e ) ; 41 } 42 return a r r [ i ] ; 43 } void push back ( const double i ) { 46 i f ( sz == cap ) { 47 cap = 2 ; 48 double tmp = new double [ cap ] ; 49 try { 50 for ( s i z e t i = 0 ; i < sz ; ++i ) { 51 tmp [ i ] = a r r [ i ] ; 52 } 53 std : : swap (tmp, a r r ) ; 4

5 54 } catch (... ) { 55 delete [ ] tmp ; 56 throw ; 57 } 58 delete [ ] tmp ; 59 } 60 a r r [ sz++] = i ; 61 } void pop back ( ) { 64 sz ; 65 } private : 68 s i z e t cap ; 69 s i z e t sz ; 70 double a r r ; 71 } ; Q11 Add the specifiers final, virtual, const, override, constexpr, and static to the following class where appropriate. 1 class Animal { 2 public : 3 Animal ( std : : s t r i n g& name ) : name( name ) {++number of animals ; } 4 std : : s t r i n g get name ( ) 5 std : : s t r i n g t a l k ( ) = 0 ; 6 unsigned get number of animals ( ) ; 7 private : 8 std : : s t r i n g name ; 9 unsigned number of animals ; 10 } ; 11 std : : s t r i n g Animal : : get name ( ) { 12 return name ; 13 } 14 unsigned Animal : : number of animals = 0 ; 15 unsigned Animal : : get number of animals ( ) { 16 return number of animals ; 17 } 18 class Dog : public Animal { 19 public : 20 Dog ( std : : s t r i n g& name ) : Animal ( name ) {++number of dogs ; } 21 unsigned get number of dogs ( ) ; 22 std : : s t r i n g t a l k ( ) ; 23 private : 24 unsigned number of dogs ; 25 std : : s t r i n g n o i s e ; 26 } ; 5

6 27 std : : s t r i n g Dog : : t a l k ( ) { 28 return n o i s e ; 29 } ; 30 unsigned Dog : : number of dogs = 0 ; 31 std : : s t r i n g Dog : : n o i s e = Woof ; A11 1 class Animal { 2 public : 3 // cannot be c o n s t e x p r because i t has a body 4 Animal ( const std : : s t r i n g& name ) : name( name ) {++number of animals ; } 5 // cannot be c o n s t e x p r because s t d : : s t r i n g i s not a l i t e r a l type 6 std : : s t r i n g get name ( ) const ; 7 // cannot be c o n s t e x p r because v i r t u a l 8 virtual std : : s t r i n g t a l k ( ) const = 0 ; 9 // cannot be c o n s t e x p r because number of animals cannot be c o n s t e x p r because 10 // i t changes 11 static unsigned get number of animals ( ) ; 12 private : 13 // cannot be c o n s t e x p r because s t d : : s t r i n g i s not a l i t e r a l type 14 std : : s t r i n g name ; 15 // cannot be c o n s t e x p r because number of animals changes 16 static unsigned number of animals ; 17 } ; 18 std : : s t r i n g Animal : : get name ( ) const { 19 return name ; 20 } 21 unsigned Animal : : number of animals = 0 ; 22 unsigned Animal : : get number of animals ( ) { 23 return number of animals ; 24 } 25 class Dog f i n a l : public Animal { 26 public : 27 Dog ( const std : : s t r i n g& name ) : Animal ( name ) {++number of dogs ; } 28 static unsigned get number of dogs ( ) ; 29 virtual std : : s t r i n g t a l k ( ) const o v e r r i d e ; 30 private : 31 static unsigned number of dogs ; 32 static const std : : s t r i n g n o i s e ; 33 } ; 34 std : : s t r i n g Dog : : t a l k ( ) const { 35 return n o i s e ; 36 } ; 37 const std : : s t r i n g Dog : : n o i s e = Woof ; Q12 Explain the value categories in C++. Give examples of each. 6

7 A12 Expressions can be either pr-values, x-values, or l-values. These three categories are further placed into the categories of gl-values and r-values: pr-values and x-values are r-values, and x- values adn l-values are gl-values. These categories are defined via two properties: having identity and being able to be moved-from. An expression has identity if it has a memory address and an expression can be moved-from if it is allowed to leave the moved-from object in an indeterminate, but valid, state. r-values do not have identity but can be moved-from. x-values have identity and can be movedfrom. l-values have identity and cannot be moved-from. The last option, does not have identity and cannot be moved-from, is not used in C++. Examples: 1 std : : s t r i n g x = h e l l o ; // h e l l o i s an l v a l u e 2 std : : s t r i n g y ( std : : move( x ) ) // s t d : : move ( x ) i s an x v a l u e 3 std : : s t r i n g z = y + world ; // y + world i s a pr v a l u e Q13 Explain declarations and definitions. Will the code below compile? If not, indicate where the errors occur and/or how to fix them. 1 class A { 2 B b ; 3 } ; 4 class B { 5 A a ; 6 } 7 extern int x ; 8 class A; 9 std : : s t r i n g s ; 10 int x = 1 0 ; 11 std : : s t r i n g s = H e l l o world! ; A13 Declarations tell the compiler that a symbol is valid. Definitions give symbols meaning by associating symbols with code. 1 class B; // need to d e c l a r e B b e f o r e using 2 class A { 3 B b ; 4 } ; 5 class B { 6 A a ; 7 } 8 extern int x ; 9 class A; 10 std : : s t r i n g s ; 11 int x = 1 0 ; 12 std : : s t r i n g s = H e l l o world! ; // ERROR! Redefining s Q14 Suppose we have two.cpp files as follows: 1.cpp 7

8 1 int fun ( ) ; 2 int x ; 3 int main ( ) { 4 fun ( ) ; 5 } 2.cpp 1 int x = 1 0 ; 2 inline int fun ( ) { 3 return 1 0 ; 4 } Will the code compile? If not, where do the error(s) occur? A14 It is not allowed to have multiple definitions of the variable x. To fix this, one can use the extern keyword to declare int x in 1.cpp. Secondly, inline functions have internal linkage; thus, the linker will be unable to find the definition in 2.cpp for the declaration int fun(). To fix this, one can either remove the inline keyword, or place the full definition of fun in 2.cpp into 1.cpp. Q15 What is the output of the following code? Will the code finish normally or be terminated prematurely? 1 void fun1 ( ) { 2 throw std : : r u n t i m e e r r o r ( stack unwind ) ; 3 std : : cout << 1 << std : : endl ; 4 } 5 void fun2 ( ) { 6 try { 7 fun1 ( ) ; 8 } catch ( const int x ) { 9 std : : c e r r << 2 << std : : endl ; 10 } catch ( const std : : invalid argument& e ) { 11 std : : c e r r << 3 << e. what ( ) << std : : endl ; 12 throw ; 13 } catch ( const std : : r u n t i m e e r r o r& e ) { 14 std : : c e r r << 4 << e. what ( ) << std : : endl ; 15 throw ; 16 } catch (.. ) { 17 std : : c e r r << 5 << std : : endl ; 18 throw ; 19 } 20 std : : cout << 6 << std : : endl ; 21 } 22 int main ( ) { 23 try { 24 fun2 ( ) ; 25 } catch ( const std : : e x c e p t i o n ()& e ) { 26 std : : c e r r << 7 << std : : endl ; 8

9 27 throw ; 28 } 29 std : : cout << 8 << std : : endl ; 30 } A15 Output: 1 4 Stack unwind The code will terminate early because the exception is uncaught. 9

04-17 Discussion Notes

04-17 Discussion Notes 04-17 Discussion Notes PIC 10B Spring 2018 1 RAII RAII is an acronym for the idiom Resource Acquisition is Initialization. What is meant by resource acquisition is initialization is that a resource should

More information

05-01 Discussion Notes

05-01 Discussion Notes 05-01 Discussion Notes PIC 10B Spring 2018 1 Exceptions 1.1 Introduction Exceptions are used to signify that a function is being used incorrectly. Once an exception is thrown, it is up to the programmer

More information

04-19 Discussion Notes

04-19 Discussion Notes 04-19 Discussion Notes PIC 10B Spring 2018 1 Constructors and Destructors 1.1 Copy Constructor The copy constructor should copy data. However, it s not this simple, and we need to make a distinction here

More information

04-24/26 Discussion Notes

04-24/26 Discussion Notes 04-24/26 Discussion Notes PIC 10B Spring 2018 1 When const references should be used and should not be used 1.1 Parameters to constructors We ve already seen code like the following 1 int add10 ( int x

More information

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

CS 162, Lecture 25: Exam II Review. 30 May 2018 CS 162, Lecture 25: Exam II Review 30 May 2018 True or False Pointers to a base class may be assigned the address of a derived class object. In C++ polymorphism is very difficult to achieve unless you

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1.... COMP6771 Advanced C++ Programming Week 5 Part One: Exception Handling 2016 www.cse.unsw.edu.au/ cs6771 2.... Memory Management & Exception Handling.1 Part I: Exception Handling Exception objects

More information

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

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OPERATOR OVERLOADING KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 Dynamic Memory Management

More information

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

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING 2011 C ++ Basics Review part 2 Auto pointer, templates, STL algorithms AUTO POINTER (AUTO_PTR) //Example showing a bad situation with naked pointers void MyFunction()

More information

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

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak! //Example showing a bad situation with naked pointers CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING 2011 C ++ Basics Review part 2 Auto pointer, templates, STL algorithms void MyFunction() MyClass* ptr( new

More information

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

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am CMSC 202 Section 010x Spring 2007 Computer Science II Final Exam Name: Username: Score Max Section: (check one) 0101 - Justin Martineau, Tuesday 11:30am 0102 - Sandeep Balijepalli, Thursday 11:30am 0103

More information

Advanced Systems Programming

Advanced Systems Programming Advanced Systems Programming Introduction to C++ Martin Küttler September 19, 2017 1 / 18 About this presentation This presentation is not about learning programming or every C++ feature. It is a short

More information

6 Architecture of C++ programs

6 Architecture of C++ programs 6 Architecture of C++ programs 1 Preview managing memory and other resources "resource acquisition is initialization" (RAII) using std::auto_ptr and other smart pointers safe construction of an object

More information

GEA 2017, Week 4. February 21, 2017

GEA 2017, Week 4. February 21, 2017 GEA 2017, Week 4 February 21, 2017 1. Problem 1 After debugging the program through GDB, we can see that an allocated memory buffer has been freed twice. At the time foo(...) gets called in the main function,

More information

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

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15 C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15 This chapter introduces the notion of dynamic memory allocation of variables and objects in a C++ program.

More information

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011 More C++ David Chisnall March 17, 2011 Exceptions A more fashionable goto Provides a second way of sending an error condition up the stack until it can be handled Lets intervening stack frames ignore errors

More information

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

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi Modern C++ for Computer Vision and Image Processing Igor Bogoslavskyi Outline Using pointers Pointers are polymorphic Pointer this Using const with pointers Stack and Heap Memory leaks and dangling pointers

More information

G52CPP C++ Programming Lecture 16

G52CPP C++ Programming Lecture 16 G52CPP C++ Programming Lecture 16 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last Lecture Casting static cast dynamic cast const cast reinterpret cast Implicit type conversion 2 How

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

COMP 2355 Introduction to Systems Programming

COMP 2355 Introduction to Systems Programming COMP 2355 Introduction to Systems Programming Christian Grothoff christian@grothoff.org http://grothoff.org/christian/ 1 Today Class syntax, Constructors, Destructors Static methods Inheritance, Abstract

More information

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Cpt S 122 Data Structures. Course Review Midterm Exam # 2 Cpt S 122 Data Structures Course Review Midterm Exam # 2 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 2 When: Monday (11/05) 12:10 pm -1pm

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

A brief introduction to C++

A brief introduction to C++ A brief introduction to C++ Rupert Nash r.nash@epcc.ed.ac.uk 13 June 2018 1 References Bjarne Stroustrup, Programming: Principles and Practice Using C++ (2nd Ed.). Assumes very little but it s long Bjarne

More information

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive) Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

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

Intermediate Programming & Design (C++) Classes in C++ Classes in C++ A class is a data type similar to a C structure. It includes various local data (called data members) together with constructors, destructors and member functions. All of them are called

More information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

More information

Pointers. Developed By Ms. K.M.Sanghavi

Pointers. Developed By Ms. K.M.Sanghavi Pointers Developed By Ms. K.M.Sanghavi Memory Management : Dynamic Pointers Linked List Example Smart Pointers Auto Pointer Unique Pointer Shared Pointer Weak Pointer Memory Management In order to create

More information

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive) Final Exam Exercises Chapters 1-7 + 11 Write C++ code to:! Determine if a number is odd or even CS 2308 Fall 2018 Jill Seaman! Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

Assertions and Exceptions

Assertions and Exceptions CS 247: Software Engineering Principles Assertions and Exceptions Reading: Eckel, Vol. 2 Ch. 1 Exception Handling U Waterloo CS247 (Spring 2017) p.1/32 Defensive Programming The question isn t whether

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer October 10, 2016 OOPP / C++ Lecture 7... 1/15 Construction and Destruction Kinds of Constructors Move Semantics OOPP / C++ Lecture 7... 2/15

More information

Lesson 13 - Vectors Dynamic Data Storage

Lesson 13 - Vectors Dynamic Data Storage Lesson 13 - Vectors Dynamic Data Storage Summary In this lesson we introduce the Standard Template Library by demonstrating the use of Vectors to provide dynamic storage of data elements. New Concepts

More information

CS201- Introduction to Programming Current Quizzes

CS201- Introduction to Programming Current Quizzes CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements

More information

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

Quiz Start Time: 09:34 PM Time Left 82 sec(s) Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information

CS201 Latest Solved MCQs

CS201 Latest Solved MCQs Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information

Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini

Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini Laboratorio di Tecnologie dell'informazione Ing. Marco Bertini bertini@dsi.unifi.it http://www.dsi.unifi.it/~bertini/ Resource Management Memory, auto_ptr and RAII The most commonly used resource in

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 20 November 12, 2018 CPSC 427, Lecture 20, November 12, 2018 1/26 Rethrowing Exceptions Uncaught Exceptions Singleton Design Pattern Smart

More information

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

double d0, d1, d2, d3; double * dp = new double[4]; double da[4]; All multiple choice questions are equally weighted. You can generally assume that code shown in the questions is intended to be syntactically correct, unless something in the question or one of the answers

More information

2 ADT Programming User-defined abstract data types

2 ADT Programming User-defined abstract data types Preview 2 ADT Programming User-defined abstract data types user-defined data types in C++: classes constructors and destructors const accessor functions, and inline functions special initialization construct

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer May 13, 2013 OOPP / C++ Lecture 7... 1/27 Construction and Destruction Allocation and Deallocation Move Semantics Template Classes Example:

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010 CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 19 Introduction ti to C++ C++ C++ is an enormous language: g All of C Classes and objects (kind of like Java, some crucial differences)

More information

What does it mean by information hiding? What are the advantages of it? {5 Marks}

What does it mean by information hiding? What are the advantages of it? {5 Marks} SECTION ONE (COMPULSORY) Question #1 [30 Marks] a) Describe the main characteristics of object-oriented programming. {5 Marks Encapsulation the ability to define a new type and a set of operations on that

More information

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

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4 CS32 - Week 4 Umut Oztok Jul 15, 2016 Inheritance Process of deriving a new class using another class as a base. Base/Parent/Super Class Derived/Child/Sub Class Inheritance class Animal{ Animal(); ~Animal();

More information

CS250 Final Review Questions

CS250 Final Review Questions CS250 Final Review Questions The following is a list of review questions that you can use to study for the final. I would first make sure you review all previous exams and make sure you fully understand

More information

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator C++ Addendum: Inheritance of Special Member Functions Constructors Destructor Construction and Destruction Order Assignment Operator What s s Not Inherited? The following methods are not inherited: Constructors

More information

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 Data Structures Course Review FINAL Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Final When: Wednesday (12/12) 1:00 pm -3:00 pm Where: In Class

More information

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Classes and Objects. Class scope: - private members are only accessible by the class methods. Class Declaration Classes and Objects class class-tag //data members & function members ; Information hiding in C++: Private Used to hide class member data and methods (implementation details). Public

More information

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive) Final Exam Exercises CS 2308 Spring 2014 Jill Seaman Chapters 1-7 + 11 Write C++ code to: Determine if a number is odd or even Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

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

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1 Lab 2: Pointers 1. Goals Further understanding of pointer variables Passing parameters to functions by address (pointers) and by references Creating and using dynamic arrays Combing pointers, structures

More information

C++_ MARKS 40 MIN

C++_ MARKS 40 MIN C++_16.9.2018 40 MARKS 40 MIN https://tinyurl.com/ya62ayzs 1) Declaration of a pointer more than once may cause A. Error B. Abort C. Trap D. Null 2Whice is not a correct variable type in C++? A. float

More information

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

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

Function Overloading

Function Overloading Function Overloading C++ supports writing more than one function with the same name but different argument lists How does the compiler know which one the programmer is calling? They have different signatures

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 10 October 1, 2018 CPSC 427, Lecture 10, October 1, 2018 1/20 Brackets Example (continued from lecture 8) Stack class Brackets class Main

More information

Object-Oriented Programming for Scientific Computing

Object-Oriented Programming for Scientific Computing Object-Oriented Programming for Scientific Computing Dynamic Memory Management Ole Klein Interdisciplinary Center for Scientific Computing Heidelberg University ole.klein@iwr.uni-heidelberg.de 2. Mai 2017

More information

CSE 303: Concepts and Tools for Software Development

CSE 303: Concepts and Tools for Software Development CSE 303: Concepts and Tools for Software Development Hal Perkins Autumn 2008 Lecture 24 Introduction to C++ CSE303 Autumn 2008, Lecture 24 1 C++ C++ is an enormous language: All of C Classes and objects

More information

Introduction to C++ Part II. Søren Debois. Department of Theoretical Computer Science IT University of Copenhagen. September 12th, 2005

Introduction to C++ Part II. Søren Debois. Department of Theoretical Computer Science IT University of Copenhagen. September 12th, 2005 Introduction to C++ Part II Søren Debois Department of Theoretical Computer Science IT University of Copenhagen September 12th, 2005 Søren Debois (Theory, ITU) Introduction to C++ September 12th, 2005

More information

Week 3: Pointers (Part 2)

Week 3: Pointers (Part 2) Advanced Programming (BETC 1353) Week 3: Pointers (Part 2) Dr. Abdul Kadir abdulkadir@utem.edu.my Learning Outcomes: Able to describe the concept of pointer expression and pointer arithmetic Able to explain

More information

Polymorphism Part 1 1

Polymorphism Part 1 1 Polymorphism Part 1 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid

More information

04-05 Discussion Notes

04-05 Discussion Notes 04-0 Discussion Notes PIC 10B Spring 018 1 The Build phase 1.1 Declaration vs Definition It is often the case when writing.cpp code that we wish to say a symbol is valid without actually giving the symbol

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 22 November 28, 2016 CPSC 427, Lecture 22 1/43 Exceptions (continued) Code Reuse Linear Containers Ordered Containers Multiple Inheritance

More information

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity. OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

More information

Section - Computer Science

Section - Computer Science Section - Computer Science 1. With respect to the C++ programming language, which is the parameter that is added to every non-static member function when it is called? (i) this pointer (ii) that pointer

More information

C++ Coding Standards. 101 Rules, Guidelines, and Best Practices. Herb Sutter Andrei Alexandrescu. Boston. 'Y.'YAddison-Wesley

C++ Coding Standards. 101 Rules, Guidelines, and Best Practices. Herb Sutter Andrei Alexandrescu. Boston. 'Y.'YAddison-Wesley C++ Coding Standards 101 Rules, Guidelines, and Best Practices Herb Sutter Andrei Alexandrescu 'Y.'YAddison-Wesley Boston Contents Prefaee xi Organizational and Poliey Issues 1 o. Don't sweat the small

More information

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

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor Outline EDAF50 C++ Programming 4. Classes Sven Gestegård Robertz Computer Science, LTH 2018 1 Classes the pointer this const for objects and members Copying objects friend inline 4. Classes 2/1 User-dened

More information

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

C++ (Non for C Programmer) (BT307) 40 Hours C++ (Non for C Programmer) (BT307) 40 Hours Overview C++ is undoubtedly one of the most widely used programming language for implementing object-oriented systems. The C++ language is based on the popular

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer September 26, 2016 OOPP / C++ Lecture 4... 1/33 Global vs. Class Static Parameters Move Semantics OOPP / C++ Lecture 4... 2/33 Global Functions

More information

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed) Final Exam Exercises Chapters 1-7 + 11 Write C++ code to:! Determine if a number is odd or even CS 2308 Spring 2013 Jill Seaman! Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

III. Classes (Chap. 3)

III. Classes (Chap. 3) III. Classes III-1 III. Classes (Chap. 3) As we have seen, C++ data types can be classified as: Fundamental (or simple or scalar): A data object of one of these types is a single object. int, double, char,

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1.. COMP6771 Advanced C++ Programming Week 5 Part Two: Dynamic Memory Management 2016 www.cse.unsw.edu.au/ cs6771 2.. Revisited 1 #include 2 3 struct X { 4 X() { std::cout

More information

Programming in C++: Assignment Week 8

Programming in C++: Assignment Week 8 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

More information

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management Session 8 Memory Management The issues Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) Programs manipulate data, which must be stored

More information

Midterm Exam #2 Spring (1:00-3:00pm, Friday, March 15)

Midterm Exam #2 Spring (1:00-3:00pm, Friday, March 15) Print Your Name: Signature: USC email address: CSCI 101L Fundamentals of Computer Programming Midterm Exam #2 Spring 2013 (1:00-3:00pm, Friday, March 15) Instructor: Prof Tejada Problem #1 (20 points):

More information

Smart Pointers. Some slides from Internet

Smart Pointers. Some slides from Internet Smart Pointers Some slides from Internet 1 Part I: Concept Reference: Using C++11 s Smart Pointers, David Kieras, EECS Department, University of Michigan C++ Primer, Stanley B. Lippman, Jesee Lajoie, Barbara

More information

AIMS Embedded Systems Programming MT 2017

AIMS Embedded Systems Programming MT 2017 AIMS Embedded Systems Programming MT 2017 Object-Oriented Programming with C++ Daniel Kroening University of Oxford, Computer Science Department Version 1.0, 2014 Outline Classes and Objects Constructors

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

CGS 2405 Advanced Programming with C++ Course Justification

CGS 2405 Advanced Programming with C++ Course Justification Course Justification This course is the second C++ computer programming course in the Computer Science Associate in Arts degree program. This course is required for an Associate in Arts Computer Science

More information

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I Polynomial Class Polynomial(); Polynomial(const string& N, const vector& C); Polynomial operator+(const Polynomial& RHS) const; Polynomial operator-(const Polynomial& RHS) const; Polynomial operator*(const

More information

C++ Primer for CS175

C++ Primer for CS175 C++ Primer for CS175 Yuanchen Zhu September 10, 2014 This primer is pretty long and might scare you. Don t worry! To do the assignments you don t need to understand every point made here. However this

More information

Allocation & Efficiency Generic Containers Notes on Assignment 5

Allocation & Efficiency Generic Containers Notes on Assignment 5 Allocation & Efficiency Generic Containers Notes on Assignment 5 CS 311 Data Structures and Algorithms Lecture Slides Friday, October 30, 2009 Glenn G. Chappell Department of Computer Science University

More information

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University (5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University Key Concepts Function templates Defining classes with member functions The Rule

More information

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

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011 Print Your Name: Page 1 of 8 Signature: Aludra Loginname: CSCI 102L - Data Structures Midterm Exam #1 Fall 2011 (10:00am - 11:12am, Wednesday, October 5) Instructor: Bill Cheng Problems Problem #1 (24

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 7 September 21, 2016 CPSC 427, Lecture 7 1/21 Brackets Example (continued) Storage Management CPSC 427, Lecture 7 2/21 Brackets Example

More information

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

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section

More information

IS0020 Program Design and Software Tools Midterm, Fall, 2004

IS0020 Program Design and Software Tools Midterm, Fall, 2004 IS0020 Program Design and Software Tools Midterm, Fall, 2004 Name: Instruction There are two parts in this test. The first part contains 22 questions worth 40 points you need to get 20 right to get the

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 Introduction 2 IS 0020 Program Design and Software Tools Exception Handling Lecture 12 November 23, 200 Exceptions Indicates problem occurred in program Not common An "exception" to a program that usually

More information

CS93SI Handout 04 Spring 2006 Apr Review Answers

CS93SI Handout 04 Spring 2006 Apr Review Answers CS93SI Handout 04 Spring 2006 Apr 6 2006 Review Answers I promised answers to these questions, so here they are. I'll probably readdress the most important of these over and over again, so it's not terribly

More information

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

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers and Arrays CS 201 This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers Powerful but difficult to master Used to simulate pass-by-reference

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

More information

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

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name: Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name: Section 1 Multiple Choice Questions (40 pts total, 2 pts each): Q1: Employee is a base class and HourlyWorker is a derived class, with

More information

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1 Polymorphism Part 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid adult

More information

Part X. Advanced C ++

Part X. Advanced C ++ Part X Advanced C ++ topics Philip Blakely (LSC) Advanced C++ 158 / 217 References The following are highly regarded books. They are fairly in-depth, and I haven t read them in their entirity. However,

More information

Programming in C and C++

Programming in C and C++ Programming in C and C++ 5. C++: Overloading, Namespaces, and Classes Dr. Neel Krishnaswami University of Cambridge (based on notes from and with thanks to Anil Madhavapeddy, Alan Mycroft, Alastair Beresford

More information

G52CPP C++ Programming Lecture 13

G52CPP C++ Programming Lecture 13 G52CPP C++ Programming Lecture 13 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture Function pointers Arrays of function pointers Virtual and non-virtual functions vtable and

More information

1 Short Answer (5 Points Each)

1 Short Answer (5 Points Each) 1 Short Answer ( Points Each) 1. Find and correct the errors in the following segment of code. int x, *ptr = nullptr; *ptr = &x; int x, *ptr = nullptr; ptr = &x; 2. Find and correct the errors in the following

More information

Implementing Abstractions

Implementing Abstractions Implementing Abstractions Pointers A pointer is a C++ variable that stores the address of an object. Given a pointer to an object, we can get back the original object. Can then read the object's value.

More information

C How to Program, 6/e, 7/e

C How to Program, 6/e, 7/e C How to Program, 6/e, 7/e prepared by SENEM KUMOVA METİN modified by UFUK ÇELİKKAN and ILKER KORKMAZ The textbook s contents are also used 1992-2010 by Pearson Education, Inc. All Rights Reserved. Two

More information

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

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O Outline EDAF30 Programming in C++ 2. Introduction. More on function calls and types. Sven Gestegård Robertz Computer Science, LTH 2018 1 Function calls and parameter passing 2 Pointers, arrays, and references

More information

Basic Array Implementation Exception Safety

Basic Array Implementation Exception Safety Basic Array Implementation Exception Safety CS 311 Data Structures and Algorithms Lecture Slides Monday, October 26, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks

More information

An Object Oriented Programming with C

An Object Oriented Programming with C An Object Oriented Programming with C By Tanmay Kasbe Dr. Ravi Singh Pippal IDEA PUBLISHING WWW.ideapublishing.in i Publishing-in-support-of, IDEA PUBLISHING Block- 9b, Transit Flats, Hudco Place Extension

More information

Pointers, Dynamic Data, and Reference Types

Pointers, Dynamic Data, and Reference Types Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple

More information

A Generic Non-intrusive Smart Pointer Implementation

A Generic Non-intrusive Smart Pointer Implementation A Generic Non-intrusive Smart Pointer Implementation Anthony Williams 13th March 2001 1 Background I started writing an article about resource management using Resource Acquisition Is Initialisation (RAII),

More information

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review)

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review) Announcements Lecture 05a Sept. 19 th, 2017 9/19/17 CS253 Fall 2017 Bruce Draper 1 Quiz #4 due today (before class) PA1/2 Grading: If something is wrong with your code, you get sympathy PA3 is due today

More information