E N D
CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/ Lecture 10: C strings Changes in C++ for structs Hank Childs, University of Oregon April 30th, 2014
Outline • Announcements • Review • Project 3A • C-strings • Misc. C++ • Structs in C++ • Additional topics (if time)
Outline • Announcements • Review • Project 3A • C-strings • Misc. C++ • Structs in C++ • Additional topics (if time)
Announcements: Grades • Grades for project 1A, 1B, 1C all available on Blackboard • Emails sent out for missing projects, late projects • Please check and make sure everything you submitted is graded • Grades for projects 2A and 2D will be available soon • Suggestions for reviewing HWs? • Do it in class and point out common mistakes?
Announcements: Late Passes • All projects on Blackboard have full score, even if late • I propose you all keep track of your own late passes, and tell me how you want to apply them at the end of the quarter. • Alternate: you can tell me now if you want to apply. • Not considered: I figure out optimal solution for you. So what should we do?
Outline • Announcements • Review • Project 3A • C-strings • Misc. C++ • Structs in C++ • Additional topics (if time)
Review on compilation • gcc –c: build an object file (.o), i.e., binary code that can directly run on the architecture • Then the binary can be generated from the object files. • Libraries are a mechanism for grouping up a bunch of related object files • They are assembled together using a program called an archiver (ar) • You can also just use object files directly when linking.
Makefiles • Consists of rules • Rule syntax: target: dependency1 dep2 … depN <tab>command1 <tab>command2 Quiz: write down a Makefile for a program called proj2B. Again, the file names are prototypes.h, driver.c, rectangle.c
Preprocessor Phases • Resolve #includes • (we understand #include phase already) • Conditional compilation • #define / #ifdef / #ifndef / #endif • Macro replacement • Special macros • __FILE__, __LINE__, __FUNCTION__
#ifndef/ #define to the rescue struct.h #ifndef RECTANGLE_330 #define RECTANGLE_330 struct Rectangle { double minX, maxX, minY, maxY; }; #endif Why does this work? This problem comes up a lot with big projects, and especially with C++.
Problem with C… No checking of type…
Mangling • Mangling refers to combing information about arguments and “mangling” it with function name. • Way of ensuring that you don’t mix up functions. • Why not return type too? • Causes problems with compiler mismatches • C++ compilers haven’t standardized. • Can’t take library from icpc and combine it with g++.
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.
Outline • Announcements • Review • Project 3A • C-strings • Misc. C++ • Structs in C++ • Additional topics (if time)
Project #3A: background • Definitions: • Image: 2D array of pixels • Pixel: A minute area of illumination on a display screen, one of many from which an image is composed. • Pixels are made up of three colors: Red, Green, Blue (RGB) • Amount of each color scored from 0 to 1 • 100% Red + 100% Green + 0% Blue = Yellow • 100% Red + 0% Green + 100 %Blue = Purple • 0% Red + 100% Green + 0% Blue = Cyan • 100% Red + 100% Blue + 100% Green = White
Project #3A: background • Colors are 0->1, but how much resolution is needed? How many bits should you use to represent the color? • Can your eye tell the difference between 8 bits and 32 bits? • No. Human eye can distinguish ~10M colors. • 8bits * 3 colors = 24 bits = ~16M colors. • Red = (255,0,0) • Green = (0,255,0) • Blue = (0,0,255)
Project 3A • Prompt was posted on Sunday • Due on Saturday, May 3rd • Tasks: • Struct to contain image • Read image from file (simplified format) • Write image to file (simplified format) • Function to modify image (yellow diagonal) • Program that puts it all together We will be modifying this code throughout the quarter… (expect you will have to retrofit what you are writing now)
Outline • Announcements • Review • Project 3A • C-strings • Misc. C++ • Structs in C++ • Additional topics (if time)
ASCII Character Set There have been various extensions to ASCII … now more than 128 characters Many special characters are handled outside this convention image source: granneman.com
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
signed vs unsigned chars • signed char (“char”): • valid values: -128 to 127 • size: 1 byte • used to represent characters with ASCII • values -128 to -1 are not valid • unsigned char: • valid values: 0 to 255 • size: 1 byte • used to represent data
character strings • A character “string” is: • an array of type “char” • that is terminated by the NULL character • Example: char str[12] = “hello world”; • str[11] = ‘\0’ (the compiler did this automatically) • The C library has multiple functions for handling strings
Useful C library string functions • strcpy: string copy • strncpy: string copy, but just first N characters • strlen: length of a string
Useful C library string functions • strcpy: string copy • strncpy: string copy, but just first N characters • strlen: length of a string What happened here?
More useful C library string functions source: cplusplus.com
memcpy I mostly use C++, and I still use memcpy all the time
sscanf • like printf, but it parses from a string sscanf(str, "%s\n%d %d\n%d\n", magicNum, &width, &height, &maxval); on: str=“P6\n1000 1000\n255\n”; gives: magicNum = “P6”, width = 1000, height = 1000, maxval = 255
Outline • Announcements • Review • Project 3A • C-strings • Misc. C++ • Structs in C++ • Additional topics (if time)
C++ • Reminder: almost every C program is a valid C++ program. • We now know C pretty well • So we are going to focus on new things in C++ • Classes • Templates • Operator overloading • Standard library • (will also talk about other changes)
References • A references is a simplified version of a pointer. • Key differences: • You cannot do pointer manipulations • A reference is always valid • a pointer is not always valid • Accomplished with & (ampersand) • &: address of variable (C-style, still valid) • &: reference to a variable (C++-style, also now valid) You have to figure out how ‘&’ is being used based on context.
References vs Pointers vs Call-By-Value ref_doubler and ptr_doubler are both examples of call-by-reference. val_doubler is an example of call-by-value.
References • Simplified version of a pointer. • Key differences: • You cannot manipulate it • Meaning: you are given a reference to exactly one instance … you can’t do pointer arithmetic to skip forward in an array to find another object • A reference is always valid • No equivalent of a NULL pointer … must be a valid instance
Different Misc C++ Topic: initialization during declaration using parentheses This isn’t that useful for simple types, but it will be useful when we start dealing with objects.
Outline • Announcements • Review • Project 3A • C-strings • Misc. C++ • Structs in C++ • Additional topics (if time)
Learning classes via structs • structs and classes are closely related in C++ • I will lecture today on changes on how “structs in C++” are different than “structs in C” • … at the end of the lecture, I will describe how classes and structs in C++ differ.
3 Big changes to structs in C++ • You can associate “methods” (functions) with structs
Methods vs Functions • Methods and Functions are both regions of code that are called by name (“routines”) • With functions: • the data it operates on (i.e., arguments) are explicitly passed • the data it generates (i.e., return value) is explicitly passed • stand-alone / no association with an object • With methods: • associated with an object & can work on object’s data • still opportunity for explicit arguments and return value
Function vs Method (left) arguments and return value are explicit (right) arguments and return value are not necessary, since they are associated with the object (left) function is separate from struct (right) function (method) is part of struct
Tally Counter 3 Methods: Increment Count Get Count Reset
Methods & Tally Counter • Methods and Functions are both regions of code that are called by name (“routines”) • With functions: • the data it operates on (i.e., arguments) are explicitly passed • the data it generates (i.e., return value) is explicitly passed • stand-alone / no association with an object • With methods: • associated with an object & can work on object’s data • still opportunity for explicit arguments and return value