Hank Childs, University of Oregon April 22 nd, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /

Slides:



Advertisements
Similar presentations
CSE 105 Structured Programming Language (C)
Advertisements

C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Hank Childs, University of Oregon April 17 th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
CS16 Week 2 Part 2 Kyle Dewey. Overview Type coercion and casting More on assignment Pre/post increment/decrement scanf Constants Math library Errors.
. Virtual Classes & Polymorphism. Example (revisited) u We want to implement a graphics system u We plan to have lists of shape. Each shape should be.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Structure of a C program
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Guide To UNIX Using Linux Third Edition
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined.
Hank Childs, University of Oregon June 3 rd, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
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: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
CHAPTER 3 Function Overloading. 2 Introduction The polymorphism refers to ‘one name having many forms’ ‘different behaviour of an instance depending upon.
Hank Childs, University of Oregon May 8th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Introduction to Unix – CS 21 Lecture 8. Lecture Overview More detail on emacs and vi Regular expression matching in emacs and vi.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
Hank Childs, University of Oregon May 13th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Hank Childs, University of Oregon May 19 th & 21st, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ /
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
1 CS161 Introduction to Computer Science Topic #9.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Working With Objects Tonga Institute of Higher Education.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.
Classes, Interfaces and Packages
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 Introduction to Object Oriented Programming Chapter 10.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Hank Childs, University of Oregon May 6th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Hank Childs, University of Oregon May 20th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Hank Childs, University of Oregon May 13th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Hank Childs, University of Oregon May 11th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Hank Childs, University of Oregon April 1, 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 May 27 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
Hank Childs, University of Oregon May 18th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Hank Childs, University of Oregon April 15 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Hank Childs, University of Oregon April 13 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Hank Childs, University of Oregon
Hank Childs, University of Oregon
Hank Childs, University of Oregon
Hank Childs, University of Oregon
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
User-Written Functions
Hank Childs, University of Oregon
Hank Childs, University of Oregon
Hank Childs, University of Oregon
A bit of C programming Lecture 3 Uli Raich.
Hank Childs, University of Oregon
Function Overloading.
Hank Childs, University of Oregon
C Basics.
Hank Childs, University of Oregon
Hank Childs, University of Oregon
Hank Childs, University of Oregon
Govt. Polytechnic,Dhangar
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Presentation transcript:

Hank Childs, University of Oregon April 22 nd, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/ Lecture 8: function pointers, subtyping, more Unix

Announcements Projects – 2B prompt available last Saturday AM, due SATURDAY – 2C prompt assigned two days ago, due Tuesday – 2D today, due Friday No class on Weds April 27 – will be a short YouTube replacement lecture No OH on Tues April 26 – can be a bonus OH this weekend if demand

Grading (1/2) code isn’t always portable – Scenario #1 your compiler is smart and glosses over a small problem my compiler isn’t as smart, and gets stuck on the problem (fix: gcc –pedantic) – Scenario #2 memory error doesn’t trip you up memory error does trip me up (fix: run valgrind before submitting)

Grading (2/2) Graders will run your code – If it works for them, then great – If not, they may ding you  how to resolve? – ix.cs.uoregon.edu is my reference machine – if it works there, you have a very strong case for getting credit back

Outline Review Project 2C Function Pointers Subtyping More Unix Project 2D (Bonus Material)

Outline Review Project 2C Function Pointers Subtyping More Unix Project 2D (Bonus Material)

Enums Enums make your own type – Type is “list of key words” Enums are useful for code clarity – Always possible to do the same thing with integers Be careful with enums – … you can “contaminate” a bunch of useful words

enum example C keyword “enum” – means enum definition is coming This enum contains 6 different student types semi-colon!!!

But enums can be easier to maintain than integers If you had used integers, then this is a bigger change and likely to lead to bugs.

Data types float double int char unsigned char All of these are simple data types

Structs: a complex data type Construct that defines a group of variables – Variables must be grouped together in contiguous memory Also makes accessing variables easier … they are all part of the same grouping (the struct)

struct syntax C keyword “struct” – means struct definition is coming This struct contains 6 doubles, meaning it is 48 bytes semi-colon!!! “.” accesses data members for a struct Declaring an instance

Nested structs accesses dir part of Ray accesses directionZ part of Direction (part of Ray)

typedef typedef: tell compiler you want to define a new type saves you from having to type “struct” every time you declare a struct.

Other uses for typedef Declare a new type for code clarity – typedef int MilesPerHour; Makes a new type called MilesPerHour. MilesPerHour works exactly like an int. Also used for enums & unions – same trick as for structs … typedef saves you a word

Unions Union: special data type – store many different memory types in one memory location When dealing with this union, you can treat it as a float, as an int, or as 4 characters. This data structure has 4 bytes

Unions Why are unions useful?

Unions Example

Why are Unions useful? Allows you to represent multiple data types simultaneously – But only if you know you want exactly one of them Benefit is space efficiency, which leads to performance efficiency Unions are also useful for abstracting type. We will re-visit this when we talk about C++’s templates. Unions are also useful for abstracting type. We will re-visit this when we talk about C++’s templates.

Outline Review Project 2C Function Pointers Subtyping More Unix Project 2D (Bonus Material)

Project 2C

Outline Review Project 2C Function Pointers Subtyping More Unix Project 2D (Bonus Material)

Function Pointers Idea: – You have a pointer to a function – This pointer can change based on circumstance – When you call the function pointer, it is like calling a known function

Function Pointer Example

Function Pointers vs Conditionals What are the pros and cons of each approach?

Function Pointer Example #2 Function pointer Part of function signature Don’t be scared of extra ‘*’s … they just come about because of pointers in the arguments or return values.

Simple-to-Exotic Function Pointer Declarations void (*foo)(void); void (*foo)(int **, char ***); char ** (*foo)(int **, void (*)(int)); These sometimes come up on interviews.

Callbacks Callbacks: function that is called when a condition is met – Commonly used when interfacing between modules that were developed separately. – … libraries use callbacks and developers who use the libraries “register” callbacks.

Callback example

Function Pointers We are going to use function pointers to accomplish “sub-typing” in Project 2D.

Outline Review Project 2C Function Pointers Subtyping More Unix Project 2D (Bonus Material)

Subtyping Type: a data type (int, float, structs) Subtype / supertype: – Supertype: the abstraction of a type (not specific) – Subtype: a concrete implementation of the supertype (specific) The fancy term for this is “subtype polymorphism”

Subtyping: example Supertype: Shape Subtypes: – Circle – Rectangle – Triangle

Subtyping works via interfaces Must define an interface for supertype/subtypes – Interfaces are the functions you can call on the supertype/subtypes The set of functions is fixed – Every subtype must define all functions

Subtyping I write my routines to the supertype interface All subtypes can automatically use this code – Don’t have to modify code when new supertypes are added Example: – I wrote code about Shapes. – I don’t care about details of subtypes (Triangle, Rectangle, Circle) – When new subtypes are added (Square), my code doesn’t change

Outline Review Project 2C Function Pointers Subtyping More Unix Project 2D (Bonus Material)

Project 2D You will extend Project 2C You will do Subtyping – You will make a union of all the structs – You will make a struct of function pointers This will enable subtyping Goal: driver program works on “Shape”s and doesn’t need to know if it is a Circle, Triangle, or Rectangle.

Outline Review Project 2C Function Pointers Subtyping More Unix Project 2D (Bonus Material)

“.” and “..” Unix convention: – “.” : the current directory – “..” : the parent directory Quiz: you in /path/to/dir and issue “cd./.././..”. Where do you end up? Quiz: you in /path/to/dir and issue “cd./.././..”. Where do you end up? Answer: “/path”

pwd and $PWD pwd: unix command that returns the “present working directory” $PWD : environment variable that contains the present working directory $OLDPWD : environment variable that contains the previous present working directory “-” : shortcut for the previous PWD

PATH environment variable When the shell wants to invoke a command, it searches for the command in the path “tr”: Unix command for replacing characters (translating characters).

which which: tells you the directory the shell is finding a command in.

Invoking programs in current directory shell works with./prog_name since it views this as a path. Hence $PATH is ignored.

Invoking programs in current directory

Trojan Horse Attack export PATH=.:$PATH – why is this a terrible idea?

Wild Cards ‘*’ (asterisk) serves as a wild card that does pattern matching

Wild Cards You can use multiple asterisks for complex patterns

if / then / else / fi Advanced constructs:

for / do / done

-f and -d -f : does a file exist? -d : does a directory exist? example: if [[ ! -d include ]] ; then mkdir include ; fi

Outline Review Project 2C Function Pointers Subtyping More Unix Project 2D (Bonus Material)

Problem with C…

No checking of type…

Problem is fixed with C++…

Mangling Mangling refers to combing information about the return type and arguments and “mangling” it with function name. – Way of ensuring that you don’t mix up functions. Causes problems with compiler mismatches – C++ compilers haven’t standardized. – Can’t take library from icpc and combine it with g++.

C++ will let you overload functions with different types

C++ also gives you access to mangling via “namespaces” Functions or variables within a namespace are accessed with “::”

C++ also gives you access to mangling via “namespaces” The “using” keyword makes all functions and variables from a namespace available without needing “::”. And you can still access other namespaces. The “using” keyword makes all functions and variables from a namespace available without needing “::”. And you can still access other namespaces.

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

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)

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