1 / 23

C++ overview

C++ overview. CS113. Computer. I/O. CPU. RAM. Memory (HD). From you to computer. a.cpp : … x=5; …. a.obj. Linker. C++ pre-proc + compiler. a.exe. C++ program. What you write. The rest is commentary. Comments Line comment: // … “ Block ” (multi-line) comment: /* … */

warren
Download Presentation

C++ overview

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. C++ overview CS113

  2. Computer I/O CPU RAM Memory (HD) Gene Itkis CS112

  3. From you to computer a.cpp: … x=5; … a.obj Linker C++ pre-proc + compiler a.exe Gene Itkis CS112

  4. C++ program What you write

  5. The rest is commentary • Comments • Line comment: //… • “Block” (multi-line) comment: /*…*/ //this is a comment to the end of the line str = “this line is not a comment”; /*and this too will be ignored by the the computer*/ printf (“%s”, str); Gene Itkis CS112

  6. Wrappers • Headers #include <iostream> • Inserts the text from file iostream.h into the program • Header files can be your own “common blocks” or system dependent (come with the system Gene Itkis CS112

  7. Headers header.h: int x; char c; … a.cpp: //main file int x; char c; … … x = 5; c = ‘a’; … a.cpp: //main file #include <header> … x = 5; c = ‘a’; … Gene Itkis CS112

  8. The main thing int main() { … … // the program … } Gene Itkis CS112

  9. Finally, Objects • Constants • 1, 27, -97 • 3.14, 6.02e23 • ‘a’, ‘1’ • true, false • “hello”, “123)(*^@#, - string” Gene Itkis CS112

  10. Creating Objects • Integers • int x, y = -5; • long i, j; // usually same as int • short k; // -215 to 215–1 • Reals • float p=3.14; • double zp=3.1415926535897932384; • double avogadro=6.02e23; // = 6.02*1023 Gene Itkis CS112

  11. More types of objects • Alphanumericals • char c=‘a’; • string s=“this is a @#& string”, ps; • Boolean • bool there_is_God = true, agnostic; In the above examples, note • multiple variables in one declaration • initialization Gene Itkis CS112

  12. Object names • NO keywords (reserved words) • Names of types • Other C++ constructs • Variable names • Just about any alphanumeric string: x1_Z • Except keywords, some special chars, etc. • Use descriptive names Gene Itkis CS112

  13. Existing objects: Operators • Assignment • x=x+5; // general: object = expr; • Arithmetic operators • The usual: -, -, +, *, / • x-z ; x = -5 • Less usual: % //remainder • 14/3 // quotient = 4 • 14%3 // remainder = 14 mod 3 = 2 Gene Itkis CS112

  14. Who’s first • Precedence (highest first) • -(negation) • * / % • + – Gene Itkis CS112

  15. Shorthand • x++ // x = x+1 • x += y; // x = x+y • x *= y; // x = x*y • x /= --y; // x = x/(y-1) // and y = y-1 • Do NOT over(ab)use shorthand • a source of bugs and confusion • last 2 examples above are bad style Gene Itkis CS112

  16. Conversions • Implicit conversion – to more complex: • int + long // long + long • int + double // double + double • BUT 14/4 –> 3 !!! • Explicit conversion • i = int(3.14); // i=3 • i= static_cast<int>(3.14) // i=3, more C++ Gene Itkis CS112

  17. Conversions • More explicit conversions • int i=14, j=4, r;double x;r=i/j; // r=3x=double(i)/j; // x=3.5 Gene Itkis CS112

  18. Char integrity • Character object represented by integer char c=‘a’;c++; // c=‘b’c -= 32; // c=‘B’int i= c-16; // i=2c=‘5’;int j=c-’0’; // j=5string s, h=“hello”, b=“ “,w=“world”;s=h+b+w; //s=“hello world” Gene Itkis CS112

  19. Being logical but boolish • Operators • and (&&), or (||), not (!) • Binary arithmetic • Bit-wise • And: & • Or: | • Not: ~ • XOR: ^ • Shift: << , >> • We’ll see examples of these later Gene Itkis CS112

  20. Bools & Bins • Examples • bool learn=true, miss_test, fail;int mask=0xF0F0, donts, bits=0x1234;miss_test = !learn;fail = !learn || miss_test;mask = ~mask;donts = bits & (~bits);bits = bits & mask; • Result: fail=false; mask=0x0F0F; donts=0x0; bits=0x0204 Gene Itkis CS112

  21. When to store • string s = “now”; // compile time init • s = “now”;// run time assignment • Watch out for using un-initialized objects! Gene Itkis CS112

  22. Re-cap Objects • “Pre-existing” (atomic): • Constants • Types • Operators • Yours • Variables • Can now write very simple programs • Calculators, converters, etc Gene Itkis CS112

  23. Where to now? • More objects • New kinds – classes • New “operators”– functions • Flow control • Conditionals • if/else • Iteration • while, do … while, for • Iteration with functions: recursion Gene Itkis CS112

More Related