Hank Childs, University of Oregon June 3 rd, 2015 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: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Plab 2003 Lecture 41 C++ - Classes. Plab 2003 Lecture 42 Motivating Example Goal: Graphics package Handle drawing of different shapes Maintain list of.
Polymorphism, Virtual Methods and Abstract Classes.
Computer Science II Exam I Review Monday, February 6, 2006.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Hank Childs, University of Oregon April 24 th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Hank Childs, University of Oregon May 8th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Defining New Types Lecture 21 Hartmut Kaiser
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
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: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Hank Childs, University of Oregon May 19 th & 21st, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ /
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
 140 Total Points ◦ 100 Points Writing Programs ◦ 24 Points Tracing Algorithms and determining results ◦ 16 Points Short Answer  Similar to quizzes.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
Copyright © 2005 Elsevier Object-Oriented Programming Control or PROCESS abstraction is a very old idea (subroutines!), though few languages provide it.
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
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: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Hank Childs, University of Oregon May 13th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Hank Childs, University of Oregon May 11th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Hank Childs, University of Oregon April 22 nd, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Hank Childs, University of Oregon May 27 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
Hank Childs, University of Oregon May 18th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Hank Childs, University of Oregon
CMSC201 Computer Science I for Majors Lecture 25 – Classes
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Hank Childs, University of Oregon
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
C ++ MULTIPLE CHOICE QUESTION
Hank Childs, University of Oregon
Hank Childs, University of Oregon
Programming with ANSI C ++
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
Review: Two Programming Paradigms
Hank Childs, University of Oregon
Hank Childs, University of Oregon
Programming with ANSI C ++
Hank Childs, University of Oregon
Hank Childs, University of Oregon
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Parameter Passing Actual vs formal parameters
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
CISC/CMPE320 - Prof. McLeod
Object Oriented Programming in java
ICOM 4029 Fall 2003 Lecture 2 (Adapted from Prof. Necula UCB CS 164)
Presentation transcript:

Hank Childs, University of Oregon June 3 rd, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/ Lecture 19: templates and the standard template library

Outline Announcements Special Topics Review Potpourri from Lec 17 Templates Standard Template Library C++ Strings Bonus Slides

Outline Announcements Special Topics Review Potpourri from Lec 17 Templates Standard Template Library C++ Strings Bonus Slides

Announcements: Projects 3G: due Monday 3H: due “Friday” 4B: due “Friday” 4C: due “Friday” “Friday”  not late until June 11 th And: all work must be turned by June 11 th And: Project 3 grading may be simplified

Announcements: Logistics Office Hours: right here after class Lab: no Lab this week! Final: Monday June 8 2:45pm in this room Grading…

Outline Announcements Special Topics Review Potpourri from Lec 17 Templates Standard Template Library C++ Strings Bonus Slides

Special Topics Virtual functions in constructors const Image with Update

Outline Announcements Special Topics Review Potpourri from Lec 17 Templates Standard Template Library C++ Strings Bonus Slides

“this”: pointer to current object From within any struct’s method, you can refer to the current object using “this”

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.

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.

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”.

More notes on virtual function tables There is one instance of a virtual function table for each class – Each instance of a class shares the same virtual function table Easy to overwrite (i.e., with a memory error) – And then all your virtual function calls will be corrected – Don’t do this! ;)

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. : public B

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

Upcasting and Downcasting Upcast: treat an object as the base type – We do this all the time! – Treat a Rectangle as a Shape Downcast: treat a base type as its derived type – We don’t do this one often – Treat a Shape as a Rectangle You better know that Shape really is a Rectangle!!

Upcasting and Downcasting what do we get?

Upcasting and Downcasting C++ has a built in facility to assist with downcasting: dynamic_cast I personally haven’t used it a lot, but it is used in practice Ties in to std::exception

Default Arguments default arguments: compiler pushes values on the stack for you if you choose not to enter them

Booleans New simple data type: bool (Boolean) New keywords: true and false

gettimeofday example

Outline Announcements Special Topics Review Potpourri from Lec 17 Templates Standard Template Library C++ Strings Bonus Slides

Inline function inlined functions: – hint to a compiler that can improve performance – basic idea: don’t actually make this be a separate function that is called Instead, just pull the code out of it and place it inside the current function – new keyword: inline The compiler sometimes refuses your inline request (when it thinks inlining won’t improve performance), but it does it silently.

Inlines can be automatically done within class definitions Even though you don’t declare this as inline, the compiler treats it as an inline

You can do inlines within header files, but not regular functions Left: function is inlined in every.C that includes it … no problem Right: function is defined in every.C that includes it … duplicate symbols Left: function is inlined in every.C that includes it … no problem Right: function is defined in every.C that includes it … duplicate symbols

Outline Announcements Special Topics Review Potpourri from Lec 17 Templates Standard Template Library C++ Strings Bonus Slides

Motivation

First Template

Will now do an example to compare templates and virtual functions Will take some buildup…

Money Class

License Plate Class

Sorting With Templates

Doing the same with inheritance

Templates vs Virtual Functions Virtual functions: – Had to affect inheritance hierarchy – Overhead in function call (virtual function table) Templates: – Did not need to affect inheritance hierarchy, although function names had to coincide – No additional overhead (resolved at compile time)

Outline Announcements Special Topics Review Potpourri from Lec 17 Templates Standard Template Library C++ Strings Bonus Slides

Standard Template Library Standard Template Library: STL Many, many templated types Can ease your programming Can also hide significant performance issues – And you use C/C++ for performance My recommendation: use with caution for code that needs to be performant

Vector

std::vector Always has the amount of memory you need Many STL algorithms work on it Memory allocation: – If you don’t have enough, double it (can be a big overestimation) Overhead in access – Maybe not a big deal if data-intensive?

STL: map

Outline Announcements Special Topics Review Potpourri from Lec 17 Templates Standard Template Library C++ Strings Bonus Slides

(not a template thing): String C++ string class is very useful Great implementation of a class that encapsulates a string

String methods

Outline Announcements Special Topics Review Potpourri from Lec 17 Templates Standard Template Library C++ Strings Bonus Slides

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!!

printf Print to stdout – printf(“hello world\n”); – printf(“Integers are like this %d\n”, 6); – printf(“Two floats: %f, %f”, 3.5, 7.0); Have you ever wondered how printf takes a variable number of arguments?

Variable-Length Argument Lists Adapted from Kernigan & Ritchie C book