330 likes | 460 Views
COMP2004. Programming Practice. Sam Holden Department of Computer Science University of Sydney. About Me. Room: Madsen G61 Email: sholden_pp@cs.usyd.edu.au Consultation: 20 mins before each lecture Email to arrange an appointment during consultation times. Textbook.
E N D
COMP2004 Programming Practice Sam Holden Department of Computer Science University of Sydney
About Me • Room: Madsen G61 • Email: sholden_pp@cs.usyd.edu.au • Consultation: 20 mins before each lecture • Email to arrange an appointment during consultation times
Textbook • C++ Primer by Lippman 3rd Edition • The C++ Programming Language by Stroustrup 3rd Edition • Thinking in C++ by Eckel 2nd Edition
Course Information • 'Practice of Programming' so expect a some programming • Development Tools • Library Usage
Lecture Schedule • The Basics of C++ • Basics of development under Unix • The less basic Basics of C++
Assessment • Assignment Zero 0% • Assignment One 10% • Assignment Two 10% • Assignment Three 20% • Final Exam 60%
Assignment Policy • Machine and Hand marked • Individual work, no groups or copying • Follow output instructions exactly • Must work on department software • No late assignments accepted without a medical certificate
The C++ Language • C++ is what we will be using • C will not be covered (except the parts which are part of C++)
C++ at Basser • Using the GNU C++ compiler • Usage: • g++ -Wall -g -o hello hello.cc • hello.cc is the C++ file • The executable will be hello • -g adds debugging information • -Wall turns on all warnings
A Simple C++ Program • C++ starts in a special function called main #include <iostream> int main() { std::cout << "They killed Kenny\n"; } • The above is a complete C++ program
Variables • C++ is strongly typed • Every variable has a type and must be declared #include <iostream> int main() { int result = 5 + 4; std::cout<<result<<std::endl; }
Constants • Constants are declared with the keyword const const int life = 42; const double pi = 3.1415926536; • Constants are just like variables (except not variable)
Operators • C++ has a lot of operators • There are math operators like +, - , *, /, % • Comparison operators like ==, >=, <, >, != • Logical operators like ||, &&, ! • Bitwise operators like &, |, ~
More Operators • a++ and ++a mean a =a+1 (sort of, see your text book for the real story) • a-- and --a are similar • a+=10 means a=a+10 • There are a?=b versions for all the math operators
Enumerated Types • An enumeration is a type that can hold a set of values • They act lie integer types • They are very useful for anything with a restricted domain enum day_of_week { SUN, MON, TUE, WED, THU, FRI, SAT }; day_of_week day; day = WED; day = day_of_week(5); //set to FRI
Writing Functions • A function looks like this: double half(int num) { double result; result = num/2.0; return result; } • half returns a double • It takes a single int argument
Writing Functions II • If a function doesn’t return anything it is declared as returning void void display_square(int num) { std::cout << num*num; }
If Statements • C++ uses an if statement for selection int main() { char c; std::cin >> c; if (c == 'y') { std::cout << "You typed a y"; } }
If Statements II • There is also an else if (c == 'y') std::cout << "You typed a y"; else std::cout "You didn't type a y"; • Can join an else and an if: if (c == 'y') std::cout <<"You typed a y"; else if (c == 'n') std::cout <<"You typed an n"; else std::cout <<"Only y/n are accepted";
Switch Statements • A replacement for multiple ifs: char c; … switch (c) { case 'y' : std::cout << ”A y"; break; case 'n' : std::cout << ”A n"; break; default : std::cout << "y/n only"; } • Works with any integral type
Switch Statements II • Cases fall thorugh by default char c; … switch (c) { case 'y' : std::cout << ”A y"; case 'n' : std::cout << ”A n"; default : std::cout << "y/n only"; } • The above wrong
For Statements for(int i=0;i<10;i++) std::cout << "i is " << i << std::endl; • Initialisation : int i=0 • Test : i<10 • Increment : i++ • Body :std::cout << ...
While Statements int i=0; while(i<10) { std::cout << "i is " << i << std::endl; i++; } • Test : i<10 • Body :between {}s
Do Statements int i=0; do { std::cout << "i is " << i << std::endl; i++; } while (i<10); • Similar to while • Always executes one • Not often used
Arrays • Combine well with for int scores[5] = { 1,2,3,4,5}; for (int i=0;i<5;i++) std::cout "Score ” << i << " : ” <<scores[i]; • First element has index zero • Arrays do not keep track of their length • Do not automatically grow
Pointers • A hard concept int i = 27; int* p = &i; *p=26; std::cout << i << endl; std::cout << p << endl; std::cout << *p << endl; • & takes the address of a variable • *p dereferences p • source of many bugs
Pointers and Arrays • closely related in C++ • Can traverse an array with pointers char s[] ={'h','e','l','l','o','\0'}; for (int i=0;s[i]<5;i++) cout << s[i] << endl; for (char *p = s;*p!=0;p++) cout << *p << end; • A nul terminated array of chars is a C-style string • Note we assigned an array to a pointer and incremented a pointer
C-Style Strings • Double quoted strings are C-Style strings • We can initialise arrays with them • We can assign them to pointers to char • We can print them with std::cout char s1[] = "hello"; const char *s2 = "hello”; std::cout << s1 << s2;
Pointers and Const • A pointer has two parts • the pointer • what it points to • Which one does const apply to? • Both are allowed
Pointers and Const Example char s[] = "hello"; char s2[] = "bye-bye"; const char* pc = s; pc[1] = 'a'; //error pc = s2; //ok char* const cp = s; cp[1] = 'a'; //ok cp = s2; //error const char* const cpc = s; cpc[1] = 'a'; //error cpc = s2; //error
References • An alternative name for an item • Must always reference a valid item • Can not switch items • Must be initialised when declared • Like a pointer that automatically gets dereferenced int i = 42; int &r = i; r++; std::cout << i << std::endl;
Pointers, References and Const • References are often used as arguments • Pointers are as well • If the argument is not modified it should be const char* strcpy(char* dest, const char* src) { char *result = dest; while(*dest++ = *src++) /*nothing*/; return dest; }