26
Lecture 19 CIS 208 Wednesday, April 06, 2005

Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

Embed Size (px)

Citation preview

Page 1: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

Lecture 19

CIS 208Wednesday, April 06, 2005

Page 2: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

Welcome to C++

Basic program style and I/O

Class Creation

Templates.

Page 3: 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

Page 4: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

some C++ features

Exception Handling

an actual string type and bool type

Function Overloading/Constructors

Operator overloading. redefine what the + operator can do

Page 5: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

OOP basics

Encapsulation

Polymorphism

Inheritance.

Page 6: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

Encapsulation

Binding Data and functions together in nice, neat packages.

Private and public data and functions

Implemented as Classes.

Page 7: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

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

Page 8: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

Polymorphism Example

In c, abs(), fabs(), labs(): Absolute value functions.

Each takes different type of parameter.

Annoying to remember which one does which.

Page 9: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

Function Overloading

in C++, define a single function name that can handle multiple types of data.

in C++, there is just a abs() function.

Page 10: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

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.

Page 11: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

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

Page 12: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

Compiling

best to use g++

Same commands as gcc. (-o for name of executable)

Page 13: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

#include

#include <iostream>using namespace std;

iostream == stdio.

Don’t need .h if using namespace

Page 14: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

C libraries

Can use old C headers (math.h, string.h)

math.h == cmathstring.h == cstring

Page 15: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

namespace

ignore for right now.

#include <iostream.h> ==

#include <iostream>using namespace std;

Page 16: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

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

Page 17: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

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

Page 18: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

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

Page 19: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

cout <<

<< is the output symbol

cout is the destination (console)

Arrows point towards destination

cout << “hello” << “ world”;

Page 20: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

cout <<

works on any basic data type. No format specifiers.

Can chain multiple types togetherint j; char k; float m;

cout << j << k << m;

Page 21: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

cout <<

use casting to alter appearance

float j = 1.5;cout << (int) j;

Page 22: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

cout <<

can use expressions or function calls

int j = -2;

cout << j *j << “ “ << abs(j);

Page 23: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

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.

Page 24: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

cin >>

again, arrows point to destination.

Will still want to flush buffer.

Works on all basic types, and strings

Page 25: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

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

Page 26: Lecture 19 CIS 208 Wednesday, April 06, 2005. Welcome to C++ Basic program style and I/O Class Creation Templates

cin >>

use multiple typesint x; char y; float z;

cin >> x >> y >> z;be careful, this can get tricky.