Hank Childs, University of Oregon May 18th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.

Slides:



Advertisements
Similar presentations
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Advertisements

How C++ Compilers Implement Object Orientation Eric Powders (ejp2127)
Hank Childs, University of Oregon April 17 th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Hank Childs, University of Oregon June 3 rd, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Hank Childs, University of Oregon April 24 th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Hank Childs, University of Oregon May 15th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Hank Childs, University of Oregon May 8th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Hank Childs, University of Oregon May 13th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Hank Childs, University of Oregon May 19 th & 21st, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ /
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Copyright © 2005 Elsevier Object-Oriented Programming Control or PROCESS abstraction is a very old idea (subroutines!), though few languages provide it.
Effective C++, 2nd Ed. By Scott Myers. Constructors, Destructors, and Assignment Operators 11.Define a copy constructor and an assignment operator for.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
1 Introduction to Object Oriented Programming Chapter 10.
Module 7: Constructors #1 2000/2001Scientific Computing in OOCourse code 3C59 Module 7: Constructors and Destructors: In this module we will cover: Constructors.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
Hank Childs, University of Oregon May 6th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Hank Childs, University of Oregon May 20th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Hank Childs, University of Oregon May 13th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Hank Childs, University of Oregon May 11th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Hank Childs, University of Oregon April 22 nd, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Hank Childs, University of Oregon April 6 th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
Hank Childs, University of Oregon May 27 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Hank Childs, University of Oregon May 25 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
Hank Childs, University of Oregon
Hank Childs, University of Oregon
Constructors and Destructors
Hank Childs, University of Oregon
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Hank Childs, University of Oregon
Hank Childs, University of Oregon
Hank Childs, University of Oregon
Hank Childs, University of Oregon
Hank Childs, University of Oregon
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Hank Childs, University of Oregon
Hank Childs, University of Oregon
C Basics.
Hank Childs, University of Oregon
Hank Childs, University of Oregon
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Hank Childs, University of Oregon
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Hank Childs, University of Oregon
Constructors and Destructors
Hank Childs, University of Oregon
Presentation transcript:

Hank Childs, University of Oregon May 18th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/ Lecture 15: exceptions

Announcements Weekend OH? Extra Credit

Project 3E You will need to think about how to accomplish the data flow execution pattern and think about how to extend your implementation to make it work. This prompt is vaguer than some previous ones – … not all of the details are there on how to do it

Project 3E

Worth 5% of your grade Assigned May 13, due May 20 th 21st

Example of data flow (image processing) FileReader Crop Transpose Invert Color Concatenate FileWriter

Review: Access Control

Two contexts for access control class A : public B { public: A() { x=0; y=0; }; int foo() { x++; return foo2(); }; private: int x, y; int foo2() { return x+y; }; }; defines how a class inherits from another class defines access controls for data members and methods

Inheritance (“class A : public B”) public  “is a” – (I never used anything but public) private  “implemented using” – (I have never used this, but see how it could be useful) protected  the internet can not think of any useful examples for this

Access Control class Hank { public/private/protected: BankAccount hanksId; }; Access control typeWho can read it privateOnly Hank class publicAnyone protectedThose who inherit from Hank

Class Vs Struct Class: – Default inheritance is private That’s why you add public (class A : public B) – Default access control is private Struct: – Default inheritance is public That’s why you don’t have to add public (struct A : B) – Default access control is public

Review: Lecture 14

How methods work under the covers (4/4) Addr.VariableValue 0x8000MIC/myI nt 12 Addr.VariableValue 0x8000MIC/myI nt 12 0x8004mic0x8000 Addr.VariableValue 0x8000MIC/myI nt 12 Addr.VariableValue 0x8000MIC/myI nt 12 0x8004this0x8000 The compiler secretly slips “this” onto the stack whenever you make a method call. It also automatically changes “myInt” to this->myInt in methods. The compiler secretly slips “this” onto the stack whenever you make a method call. It also automatically changes “myInt” to this->myInt in methods.

Virtual functions Virtual function: function defined in the base type, but can be re-defined in derived type. When you call a virtual function, you get the version defined by the derived type

Picking the right virtual function ?????? So how to does the compiler know? How does it get “B” for “b” and “A” for “a”? So how to does the compiler know? How does it get “B” for “b” and “A” for “a”?

Virtual Function Table Let C be a class and X be an instance of C. Let C have 3 virtual functions & 4 non-virtual functions C has a hidden data member called the “virtual function table” This table has 3 rows – Each row has the correct definition of the virtual function to call for a “C”. When you call a virtual function, this table is consulted to locate the correct definition.

Showing the existence of the virtual function pointer with sizeof() what will this print? empty objects have size of 1? why?!? empty objects have size of 1? why?!? Answer: so every object has a unique address.

Virtual Function Table Let C be a class and X be an instance of C. Let C have 3 virtual functions & 4 non-virtual functions Let D be a class that inherits from C and Y be an instance of D. – Let D add a new virtual function D’s virtual function table has 4 rows – Each row has the correct definition of the virtual function to call for a “D”.

Questions What does the virtual function table look like for a Shape? What goes in Shape’s virtual function table? – Trick question: Shape can’t be instantiated, precisely because you can’t make a virtual function table abstract type due to pure virtual functions

Questions What is the virtual function table for Rectangle? (this is a code fragment from my 2C solution)

Calling a virtual function Let X be an instance of class C. Let the virtual function be the 4 th function Let the arguments to the virtual function be an integer Y and a float Z. Then call: (X.vptr[3])(&X, Y, Z); The pointer to the virtual function pointer (often called a vptr) is a data member of X The 4 th virtual function has index 3 (0-indexing) Secretly pass “this” as first argument to method

Inheritance and Virtual Function Tables A Foo1Location of Foo1 B Foo1Location of Foo1 Foo2Location of Foo2 C Foo1Location of Foo1 Foo2Location of Foo2 Foo3Location of Foo3 Same as B’s This is how you can treat a C as a B This whole scheme gets much harder with virtual inheritance, and you have to carry around multiple virtual function tables.

Virtual Function Table: Summary Virtual functions require machinery to ensure the correct form of a virtual function is called This is implemented through a virtual function table Every instance of a class that has virtual functions has a pointer to its class’s virtual function table The virtual function is called via following pointers – Performance issue

vptr.C

Pitfalls

Pitfall #1 This is using call-by-value, not call-by-reference.

Pitfall #2

Pitfall #3 int *s = new int[6*sizeof(int)];

Pitfall #4 Assume: int *X = new int[100]; What is sizeof(X)? What is sizeof(*X)?

Pitfall #5

(not-a-)Pitfall #6 Top requires memory allocation / deletion, and does extra copy.

Pitfall #7 For objects on the stack, the destructors are called when a function goes out of scope – You may have a perfectly good function, but it seg- faults on return Especially tricky for main – program ran to completion, and crashed at the very end

Pitfall #8

New Stuff: Misc. from OH

Make it easy on yourself to run…

Other ways to make life easier tab from shell: auto-completes Ctrl-R: searches backwards in your shell history

Web pages ssh –l ix.cs.uoregon.edu cd public_html put something in index.html  it will show up as

Web pages You can also exchange files this way – scp – point people to /file.pdf Note that ~/public_html/dir1 shows up as /dir1 /dir1 (“~/dir1” is not accessible via web) Note that ~/public_html/dir1 shows up as /dir1 /dir1 (“~/dir1” is not accessible via web)

make clean ; make making Images in your functions and then copying them…

New Stuff: Exceptions

Exceptions C++ mechanism for handling error conditions Three new keywords for exceptions – try: code that you “try” to execute and hope there is no exception – throw: how you invoke an exception – catch: catch an exception … handle the exception and resume normal execution

Exceptions

Exceptions: catching multiple types

Exceptions: throwing/catching complex types

Exceptions: cleaning up before you return

Exceptions: re-throwing

Exceptions: catch and re-throw anything

Exceptions: declaring the exception types you can throw

Exceptions: declaring the exception types you can throw... not all it is cracked up to be This will compile … compiler can only enforce this as a run-time thing. As a result, this is mostly unused (I had to read up on it) As a result, this is mostly unused (I had to read up on it) But: “standard” exceptions have a “throw” in their declaration.

std::exception c++ provides a base type called “std::exception” It provides a method called “what” Source: cplusplus.com

Exceptions generator by C++ standard library Source: cplusplus.com

3F

Project 3F in a nutshell Logging: – infrastructure for logging – making your data flow code use that infrastructure Exceptions: – infrastructure for exceptions – making your data flow code use that infrastructure The webpage has a head start at the infrastructure pieces for you.

Warning about 3F My driver program only tests a few exception conditions Your stress tests later will test a lot more. – Be thorough, even if I’m not testing it

3F timeline Assigned today, due Weds

const

const: – is a keyword in C and C++ – qualifies variables – is a mechanism for preventing write access to variables

const example const keyword modifies int The compiler enforces const … just like public/private access controls

Efficiency Are any of the three for loops faster than the others? Why or why not? Answer: NumIterations is slowest … overhead for function calls. Answer: X is probably faster than Y … compiler can do optimizations where it doesn’t have to do “i < X“ comparisons (loop unrolling)

const arguments to functions Functions can use const to guarantee to the calling function that they won’t modify the arguments passed in. guarantees function won’t modify the Image read function can’t make the same guarantee

const pointers Assume a pointer named “P” Two distinct ideas: – P points to something that is constant P may change, but you cannot modify what it points to via P – P must always point to the same thing, but the thing P points to may change.

const pointers int X = 4; int *P = &X; Idea #1: violates const: “*P = 3;” OK: “int Y = 5; P = &Y;” Idea #2: violates const: “int Y = 5; P = &Y;” OK: “*P = 3;” pointer can change, but you can’t modify the thing it points to pointer can’t change, but you can modify the thing it points to

const pointers int X = 4; int *P = &X; Idea #3: violates const: “*P = 3;” “int Y = 5; P = &Y;” OK: none pointer can’t change, and you can’t modify the thing it points to

const pointers int X = 4; int *P = &X; Idea #1: violates const: “*P = 3;” OK: “int Y = 5; P = &Y;” pointer can change, but you can’t modify the thing it points to const goes before type

const pointers int X = 4; int *P = &X; Idea #2: violates const: “int Y = 5; P = &Y;” OK: “*P = 3;” pointer can’t change, but you can modify the thing it points to const goes after *

const pointers int X = 4; int *P = &X; Idea #3: violates const: “*P = 3;” “int Y = 5; P = &Y;” OK: none pointer can’t change, and you can’t modify the thing it points to const in both places

const usage class Image; const Image *ptr; – Used a lot: offering the guarantee that the function won’t change the Image ptr points to Image * const ptr; – Helps with efficiency. Rarely need to worry about this. const Image * const ptr; – Interview question!!

Very common issue with const and objects How does compiler know GetNumberOfPixels doesn’t modify an Image? We know, because we can see the implementation. But, in large projects, compiler can’t see implementation for everything. How does compiler know GetNumberOfPixels doesn’t modify an Image? We know, because we can see the implementation. But, in large projects, compiler can’t see implementation for everything.

const functions with objects const after method name If a class method is declared as const, then you can call those methods with pointers.

mutable mutable: special keyword for modifying data members of a class – If a data member is mutable, then it can be modified in a const method of the class. – Comes up rarely in practice.

Bonus Topics

Backgrounding “&”: tell shell to run a job in the background – Background means that the shell acts as normal, but the command you invoke is running at the same time. “sleep 60” vs “sleep 60 &” When would backgrounding be useful?

Suspending Jobs You can suspend a job that is running Press “Ctrl-Z” The OS will then stop job from running and not schedule it to run. You can then: – make the job run in the background. Type “bg” – make the job run in the foreground. Type “fg” – like you never suspended it at all!!

Unix and Windows difference Unix: – “\n”: goes to next line, and sets cursor to far left Windows: – “\n”: goes to next line (cursor does not go to left) – “\m”: sets cursor to far left Text files written in Windows often don’t run well on Unix, and vice-versa – There are more differences than just newlines vi: “set ff=unix” solves this