1 / 26

Lecture 19

Lecture 19. CIS 208 Wednesday, April 06, 2005. Welcome to C++. Basic program style and I/O Class Creation Templates. C++ OOP. Allows for more complex programs C starts to break down Better data protection Much more programmer control. some C++ features. Exception Handling

wschubert
Download Presentation

Lecture 19

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. Lecture 19 CIS 208 Wednesday, April 06, 2005

  2. Welcome to C++ • Basic program style and I/O • Class Creation • Templates.

  3. C++ OOP • Allows for more complex programs • C starts to break down • Better data protection • Much more programmer control

  4. some C++ features • Exception Handling • an actual string type and bool type • Function Overloading/Constructors • Operator overloading. • redefine what the + operator can do

  5. OOP basics • Encapsulation • Polymorphism • Inheritance.

  6. Encapsulation • Binding Data and functions together in nice, neat packages. • Private and public data and functions • Implemented as Classes.

  7. Polymorphism • ‘One interface, multiple methods’ • Multiple functions with the same name. • Don’t care how something works on the interior, just that it does something

  8. Polymorphism Example • In c, abs(), fabs(), labs(): Absolute value functions. • Each takes different type of parameter. • Annoying to remember which one does which.

  9. Function Overloading • in C++, define a single function name that can handle multiple types of data. • in C++, there is just a abs() function.

  10. Inheritance • An object may acquire properties of another. • Example. Red Declicious apple is a type of apple, which is a type of fruit, which is a type of food.

  11. Our first C++ program #include <iostream> using namespace std; int main() { cout << "this is output. \n"; cout << "Enter a number: "; int i; cin >> i; cout << i << " squared is " << i*i <<"\n"; for (int j =0; j < i; j++) cout << "iteration " << j << '\n'; return 0; }

  12. Compiling • best to use g++ • Same commands as gcc. (-o for name of executable)

  13. #include #include <iostream> using namespace std; • iostream == stdio. • Don’t need .h if using namespace

  14. C libraries • Can use old C headers (math.h, string.h) • math.h == cmath • string.h == cstring

  15. namespace • ignore for right now. #include <iostream.h> == #include <iostream> using namespace std;

  16. function declaration • nearly same as C int main() {… Don’t need to put void as a parameter, still need void if no return. Must still have function

  17. Local Variables • Can be declared anywhere. • Normal scope rules. Only know in current code segment and only after declaration. • Declaring at beginning might make better code though

  18. Basic output cout << “This is output\n”; • prints ‘This is output’ to console. • May still use any of the old functions • printf, puts, putch, etc

  19. cout << • << is the output symbol • cout is the destination (console) • Arrows point towards destination • cout << “hello” << “ world”;

  20. cout << • works on any basic data type. • No format specifiers. • Can chain multiple types together int j; char k; float m; cout << j << k << m;

  21. cout << • use casting to alter appearance float j = 1.5; cout << (int) j;

  22. cout << • can use expressions or function calls int j = -2; cout << j *j << “ “ << abs(j);

  23. Basic input int j; cin >> j; Reads an int from the keyboard and stores it in variable j. No & necessary. cin >> knows what to do.

  24. cin >> • again, arrows point to destination. • Will still want to flush buffer. • Works on all basic types, and strings

  25. cin >> • multiple reads. char x, y, z; cin >> x >> y >> z; • takes first 3 chars and stores in x,y,z; • blank spaces don’t count

  26. cin >> • use multiple types int x; char y; float z; cin >> x >> y >> z; • be careful, this can get tricky.

More Related