ECE 449 OOP and Computer Simulation Lecture 14 Final Exam Review

Similar documents
ECE 449 OOP and Computer Simulation Lecture 12 Resource Management II

ECE 449 OOP and Computer Simulation Lecture 08 Resource Management I

ECE 449 OOP and Computer Simulation Lecture 11 Design Patterns

Homework 4. Any questions?

CSE 333 Lecture smart pointers

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

CPSC 427: Object-Oriented Programming

CSE 333 Lecture smart pointers

CS250 Final Review Questions

G Programming Languages - Fall 2012

COMP6771 Advanced C++ Programming


Programming, numerics and optimization

Instantiation of Template class

6 Architecture of C++ programs

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

CSE 303: Concepts and Tools for Software Development

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

COMP6771 Advanced C++ Programming

ECE 449 OOP and Computer Simulation Lecture 07 Class Design for Circuit Netlist

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

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Today s lecture. CS 314 fall 01 C++ 1, page 1

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

STRUCTURING OF PROGRAM

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

VALLIAMMAI ENGINEERING COLLEGE

CGS 2405 Advanced Programming with C++ Course Justification

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

C++ Smart Pointers. CSE 333 Autumn 2018

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

2 ADT Programming User-defined abstract data types

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

C++ Important Questions with Answers

04-24/26 Discussion Notes

Object-Oriented Programming for Scientific Computing

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

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

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

IS 0020 Program Design and Software Tools

Object-Oriented Programming

Object-Oriented Programming

Object Oriented Programming. Solved MCQs - Part 2

C++ for System Developers with Design Pattern

Value Semantics and Concept Based Polymorphism Sean Parent Principal Scientist Adobe Systems Incorporated. All Rights Reserved.

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Introduction to Linked Lists. Introduction to Recursion Search Algorithms CS 311 Data Structures and Algorithms

G52CPP C++ Programming Lecture 13

QUIZ. What is wrong with this code that uses default arguments?

PHY4321 Summary Notes

EL2310 Scientific Programming

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012

AN OVERVIEW OF C++ 1

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

use static size for this buffer

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

Short Notes of CS201

Object-Oriented Principles and Practice / C++

05-01 Discussion Notes

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

Part VII. Object-Oriented Programming. Philip Blakely (LSC) C++ Introduction 194 / 370

Exception Safe Coding

ECE Fall 2017, Third Exam

Advanced C++ Topics. Alexander Warg, 2017

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

CPS 506 Comparative Programming Languages. Programming Language

CS201 - Introduction to Programming Glossary By

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

Programming Project 4: COOL Code Generation

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

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

C++11: 10 Features You Should be Using. Gordon R&D Runtime Engineer Codeplay Software Ltd.

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

Introduce C# as Object Oriented programming language. Explain, tokens,

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Ch. 11: References & the Copy-Constructor. - continued -

Increases Program Structure which results in greater reliability. Polymorphism

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

D Programming Language

UNIT 3

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Assertions and Exceptions

C++ TEMPLATES. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

CS304 Object Oriented Programming

Programming II (CS300)

Intermediate C++ 1/83

COMP6771 Advanced C++ Programming

AIMS Embedded Systems Programming MT 2017

Part X. Advanced C ++

What are the characteristics of Object Oriented programming language?

CSE 307: Principles of Programming Languages

C++\CLI. Jim Fawcett CSE687-OnLine Object Oriented Design Summer 2017

Inheritance: Develop solutions by abstracting real-world object and their interaction into code to develop software solutions. Layering: Organization

Object Oriented Programming in C++

Midterm Review. PIC 10B Spring 2018

CS250 Final Review Questions

COMP322 - Introduction to C++ Lecture 10 - Overloading Operators and Exceptions

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

Data Abstraction. Hwansoo Han

Transcription:

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 1/35 ECE 449 OOP and Computer Simulation Lecture 14 Final Exam Review Professor Jia Wang Department of Electrical and Computer Engineering Illinois Institute of Technology December 1, 2017

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 2/35 Outline Types and Operators Class Design and Object Composition Exception and Exception Safety Polymorphism Smart Pointers

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 3/35 Final Exam Time: Mon. Dec. 4, 2:00 PM 4:00 PM Closed book/notes, cheat sheet allowed Main campus students and Internet students staying at main campus should take exams in this room. Check http://www.iit.edu/registrar/important_ dates/final_exam_schedule.shtml for other issues. Makeup exams will NOT be given.

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 4/35 Outline Types and Operators Class Design and Object Composition Exception and Exception Safety Polymorphism Smart Pointers

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 5/35 Types and Operators Every object has a type. Built-in types: numbers(integer/real), pointers, arrays Class types: defined by class or struct To manipulate an object, we apply operators associated with its type. Either an operator is applied directly in an expression, Or the object is passed to a function where an operator is applied eventually. Operator overloading allows us to define operators for class types. Implemented as functions with special names, where operands become parameters Make class types more intuitive for use

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 6/35 More about Operator Overloading Most operators in C++ can be overloaded. Except ::(scope) and.(member) It is wise NOT to overload key operators like &(address). Limitations One operand must be of an class type. You can only overload operators that are already in C++. Operator precedences and associativities cannot be changed. The placement and the number of the operands cannot be changed. Except (), which can take any number of operands

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 7/35 Example I const std::string hello = "Hello"; const std::string message = hello+", world"+"!"; + is left-associative: (hello+", world")+"!" hello+", world" is interpreted as hello.operator+(", world") or operator+(hello, ", world"). The compiler will call the available one. If both are available, the compiler will complain as it don t know which one to call. The return value +"!" will be handled similarly.

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 8/35 Example II const std::string exclam = "!"; const std::string message = "Hello"+", world"+exclam; + is left-associative: ("Hello"+", world")+exclam Both "Hello" and ", world" are string literals, which is a built-in type. + is not defined and cannot be redefined.

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 9/35 Example III const std::string world = "world"; const std::string message = "Hello, "+world+"!"; + is left-associative: ("Hello, "+world)+"!" "Hello, "+world is interpreted as operator+("hello, ", world). Note that since "Hello, " is of a built-in type, the compiler won t look for "Hello, ".operator+(world). The return value +"!" will be handled as in Example I.

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 10/35 Example IV double d = f()[n]; What s the type of f for the above code to be valid? f is not necessary a function. () must be defined/overloaded for the type of f. A function taking no argument [] must be defined/overloaded for the return type of (). It should take n as an argument note that n is not necessary an integer. The return type should convert to double implicitly.

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 11/35 Outline Types and Operators Class Design and Object Composition Exception and Exception Safety Polymorphism Smart Pointers

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 12/35 Class Design Class types can be defined with struct or class. The only difference is the default protection: struct is public and class is private. Each class type has a class invariant. Constructors establish the class invariant. Public member functions maintain the class invariant. An object is needed to access members. Use -> if the object is referred through a pointer, or. otherwise The object becomes an implicit argument to the member functions, though you can refer to it by this. The compiler verifies constness and protection labels at compile-time.

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 13/35 Object Composition Compose objects from others by Include child objects as data members of parent objects Keep pointers to objects as data members Inherit from a base type and access the base object as an anonymous data member Ctors and dtors ensure all the objects are constructed and destroyed properly. The compiler will synthesize default ctor, copy ctor, dtor, and operator=. The semantics are member-wise. We can implement them if the ones synthesized by the compiler are not correct, as well as other ctors. In such case, the compiler will still generate some code to make our life easier.

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 14/35 Data Member Construction and Destruction Members are constructed before the body of a ctor. They are constructed in the order they appear in the class definition. Base objects are constructed before all explicit members. We can specify how the members are constructed using the initializer list. The compiler will default initialize all the members whose constructions are not specified. Members are destroyed after the body of the dtor. In the reverse order as they are constructed The compiler will destroy them automatically.

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 15/35 Example I class B { B(const B &); ; class A { B member; ; void some_function() { A a; // call synthesized default ctor A b = a; // cannot synthesize copy ctor for A You can make it work by defining a copy ctor for A. class A { B member; public: A(const A &) { ;

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 16/35 Example II class B { ~B(); ; class A { B member; ; void some_function() { A a; // cannot synthesize dtor for A There is no way to make the above code working by changing A (without removing member).

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 17/35 Outline Types and Operators Class Design and Object Composition Exception and Exception Safety Polymorphism Smart Pointers

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 18/35 Exception and Exception Safety Exception allows us to indicate errors during construction. Exception safety: ensure proper program behavior when exception happens There are many levels of exception safety. We are concerned of the minimum level no leakage All the objects that are constructed successfully should be destroyed eventually. The compiler will take care of most cases by Stack unwinding: local objects are guaranteed to be destroyed All or None construction: if a member construction fails, all the members that are already constructed will be destroyed, and the construction of the parent object fails Assumption: dtors NEVER throw

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 19/35 Example I template <class T> class unique_ptr { unique_ptr(const unique_ptr<t> &) = delete; unique_ptr &operator=(const unique_ptr<t> &) = delete; T *p_; public: explicit unique_ptr(t *p) : p_(p) { ~unique_ptr() {delete p_; T *get() const {return p_; ; // class unique_ptr<t> void may_throw(int *p); void test() { unique_ptr<int> p(new int(10)); may_throw(p.get()); Stack unwinding ensures p to be destroyed when may_throw throws an exception. So the int object created on the heap will be deleted.

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 20/35 Example II: the Classes struct will_not_throw { will_not_throw() {std::cout << "ctor of will_not_throw" << std::endl; ~will_not_throw() {std::cout << "dtor of will_not_throw" << std::endl; ; // struct will_not_throw class ctor_throw { will_not_throw wnt_; public: ctor_throw() { std::cout << "ctor of ctor_throw" << std::endl; throw std::runtime_error("from ctor of ctor_throw"); ~ctor_throw() {std::cout << "dtor of ctor_throw" << std::endl; ; // class ctor_throw

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 21/35 Example II: the Output class ctor_throw { will_not_throw wnt_;... ; // class ctor_throw void test() { try { ctor_throw ct; catch (std::exception &e) { std::cout << e.what() << std::endl; The output ctor of will_not_throw ctor of ctor_throw dtor of will_not_throw from ctor of ctor_throw Dtor of ctor_throw is not called. That s a desired behavior since the object ct is not constructed successfully.

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 22/35 Example III: the Classes struct will_throw { will_throw() { std::cout << "ctor of will_throw" << std::endl; throw std::runtime_error("from ctor of will_throw"); ~will_throw() {std::cout << "dtor of will_throw" << std::endl; ; // struct will_throw class member_throw { will_not_throw wnt_; will_throw wt_; public: member_throw() {std::cout << "ctor of member_throw" << std::endl; ~member_throw() {std::cout << "dtor of member_throw" << std::endl; ; // class member_throw

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 23/35 Example III: the Output class member_throw { will_not_throw wnt_; will_throw wt_;... ; // class member_throw void test() { try { member_throw mt; catch (std::exception &e) { std::cout << e.what() << std::endl; The output ctor of will_not_throw ctor of will_throw dtor of will_not_throw from ctor of will_throw Body of ctor of member_throw is not called. Neither dtor of will_throw nor dtor of member_throw is called.

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 24/35 Outline Types and Operators Class Design and Object Composition Exception and Exception Safety Polymorphism Smart Pointers

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 25/35 Polymorphism A single piece of code behaves differently under different circumstances. Enable code reusing without modification Object interfaces: constraints objects should satisfy when used for the code Programming against interface: the code depends on the object interfaces but not their exact types Runtime polymorphism Interfaces are defined as virtual functions in base classes. Good for decoupling classes as exact types are hidden

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 26/35 Dynamic Binding Calling a virtual function through a pointer or a reference Not through a fully-qualified name Not in ctors or dtor The virtual function could also be called through the implicit this pointer, i.e. in member functions.

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 27/35 Example I struct base { virtual void vfun(std::string info) { std::cout << "calling base::vfun from " << info << std::endl; void usual_member() {vfun("base::usual_member"); ; // struct base struct derived : public base { void vfun(std::string info) { std::cout << "calling derived::vfun from " << info << std::endl; ; // struct derived void test_obj() { derived d; d.usual_member(); calling derived::vfun from base::usual_member

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 28/35 Example II void test_ptr() { base *p = new derived; p->usual_member(); p->vfun("test_ptr"); delete p; calling derived::vfun from base::usual_member calling derived::vfun from test_ptr

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 29/35 Outline Types and Operators Class Design and Object Composition Exception and Exception Safety Polymorphism Smart Pointers

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 30/35 Garbage Collection There are cases where it is not possible or not convenient for one to decide the exact place objects allocated from heap should be deleted garbage collection (GC) is a must. Programmers decide when objects should be created on the heap. The compiler/runtime library decide when they should be deleted. Typical garbage collection algorithms Reachability analysis: reclaim memory when necessary Reference counting: delete an object immediately when it s no longer in use Both have their advantages and disadvantages.

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 31/35 Smart Pointers Smart pointers: class types that can be used as pointers but are smarter than built-in pointers. They will delete an object when it is no longer in use. Provide GC to C++ programs std::unique_ptr: keep a non-copyable pointer to an object It is straight-forward to determine when the object should be deleted in its dtor. std::shared_ptr: allow to share a pointer to an object Reference counting is used to determine when the object should be deleted. Limitation: at runtime, if the smart pointers form a cycle, resources won t be released properly.

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 32/35 Example I struct A { shared_ptr<b> b_of_a; A() {std::cout << "ctor of A" << std::endl; ~A() {std::cout << "dtor of A" << std::endl; ; // struct A struct B { shared_ptr<a> a_of_b; B() {std::cout << "ctor of B" << std::endl; ~B() {std::cout << "dtor of B" << std::endl; ; // struct B void test1() { shared_ptr<a> pa(new A); // (A, 1) output: ctor of A shared_ptr<b> pb(new B); // (A, 1), (B, 1) output: ctor of B pa->b_of_a = pb; // (A, 1), (B, 2) pb->a_of_b = pa; // (A, 2), (B, 2) // pb is destroyed: (A, 2), (B, 1) // pa is destroyed: (A, 1), (B, 1)

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 33/35 Example II void test2() { shared_ptr<a> pa(new A); // (A, 1) output: ctor of A shared_ptr<b> pb(new B); // (A, 1), (B, 1) output: ctor of B pa->b_of_a = pb; // (A, 1), (B, 2) // pb is destroyed: (A, 1), (B, 1) // pa is destroyed: (A, 0), (B, 1) // A is destroyed output: dtor of A // b_of_a is destroyed: (B, 0) // B is destroyed output: dtor of B // a_of_b is destroyed

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 34/35 Example III void test3() { shared_ptr<a> pa(new A); // (A, 1) output: ctor of A shared_ptr<b> pb(new B); // (A, 1), (B, 1) output: ctor of B pb->a_of_b = pa; // (A, 2), (B, 1) // pb is destroyed: (A, 2), (B, 0) // B is destroyed output: dtor of B // a_of_b is destroyed (A, 1) // pa is destroyed: (A, 0) // A is destroyed output: dtor of A // b_of_a is destroyed

ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 35/35 Example IV void test4() { shared_ptr<a> pa(new A); // (A, 1) output: ctor of A shared_ptr<b> pb(new B); // (A, 1), (B, 1) output: ctor of B // pb is destroyed: (A, 1), (B, 0) // B is destroyed output: dtor of B // a_of_b is destroyed // pa is destroyed: (A, 0) // A is destroyed output: dtor of A // b_of_a is destroyed