1 / 33

COMP2004

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.

Download Presentation

COMP2004

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP2004 Programming Practice Sam Holden Department of Computer Science University of Sydney

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

  3. Textbook • C++ Primer by Lippman 3rd Edition • The C++ Programming Language by Stroustrup 3rd Edition • Thinking in C++ by Eckel 2nd Edition

  4. Course Information • 'Practice of Programming' so expect a some programming • Development Tools • Library Usage

  5. Lecture Schedule • The Basics of C++ • Basics of development under Unix • The less basic Basics of C++

  6. Assessment • Assignment Zero 0% • Assignment One 10% • Assignment Two 10% • Assignment Three 20% • Final Exam 60%

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

  8. The C++ Language • C++ is what we will be using • C will not be covered (except the parts which are part of C++)

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

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

  11. 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; }

  12. Some Built-in Types

  13. 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)

  14. Operators • C++ has a lot of operators • There are math operators like +, - , *, /, % • Comparison operators like ==, >=, <, >, != • Logical operators like ||, &&, ! • Bitwise operators like &, |, ~

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

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

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

  18. 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; }

  19. 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"; } }

  20. 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";

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

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

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

  24. While Statements int i=0; while(i<10) { std::cout << "i is " << i << std::endl; i++; } • Test : i<10 • Body :between {}s

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

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

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

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

  29. 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;

  30. Pointers and Const • A pointer has two parts • the pointer • what it points to • Which one does const apply to? • Both are allowed

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

  32. 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;

  33. 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; }

More Related