cse 1341 honors note set 2 1. overview java vs. c++ functions in c++ first programming packet ...

20

Click here to load reader

Upload: camilla-stevens

Post on 06-Jan-2018

214 views

Category:

Documents


0 download

DESCRIPTION

Review 3

TRANSCRIPT

Page 1: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

1

CSE 1341 Honors

Note Set 2

Page 2: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

2

Overview

Java vs. C++ Functions in C++ First Programming Packet Development Environment

Page 3: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

3

Review

Page 4: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

4

Main Function Free floating main function in C++.

Doesn’t have to be inside a class

public class JavaProgram { public static void main(String [] args) { System.out.println(“Hello World!”); }}

int main(){ cout << “Hello World!”;}

Page 5: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

5

Compiling

java compiles to bytecode that is later interpreted by the JVM Should theoretically be able to transfer even compiled code to

a different machine – Platform Independence C++ compiles to machine code that is directly executable

on the machine Compiled code will only run on one OS/Machine type Executable is not necessarily universally portable

Page 6: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

6

Syntax Formatting No real major difference – More of coding conventions to

deal with Where do we put braces?

Usually on the next line Does it really matter?

No

int main(){ for (int j = 0; j < 10; j++) {

cout << “Hello World!” << endl; }}

Page 7: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

7

Using Libraries What’s this #include stuff??

#include <iostream> Anything with a # is handled by the C++ Preprocessor iostream is a library of io functionality there is actually a file in the file system named iostream the contents of the iostream file are effectively copied into the place of the

#include <iostream> other things can appear between the <> such as fstream, iomanip, etc. similar to an import in Java

using namespace std; everything that is in the iostream file is part of the std namespace Namespaces allow grouping of entities like classes, objects and functions

using one name Allows global scope to be subdivided, with each subdivision having its own

name

Page 8: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

8

Functions

Page 9: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

9

Functions

int myfunction (int x, int y){ int sum = 0; for (int j = x; j < y; j++) sum += j; return sum;}

int main(){ int sum = myfunction(5, 10); cout << sum << endl; return 0;} arguments parameters

Page 10: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

10

Pass By Value

int myfunction (int x, int y){ x = 60; y = 600; return x + y;}

int main(){ int a = 5, b = 10; int sum = myfunction(a, b); cout << a << “ “ << b << endl; return 0;}

5

10

a

b

5

10

x

y

• x and y have copies of a and b• changing x or y does not affect a or b

Page 11: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

11

Pass By Reference - &

int myfunction (int& x, int& y){ x = 60; y = 600; return x + y;}

int main(){ int a = 5, b = 10; int sum = myfunction(a, b); cout << a << “ “ << b << endl; return 0;}

5

10

a

b

x

y

• x and y refer back to their arguments, a and b• changing x or y DOES affect a or b

Page 12: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

12

Pass By Reference - &

int myfunction (int& x, int& y){ x = 60; y = 600; return x + y;}

int main(){ int a = 5, b = 10; int sum = myfunction(a, b); cout << a << “ “ << b << endl; return 0;}

60

10

a

b

x

y

• x and y refer back to their arguments, a and b• changing x or y DOES affect a or b

Page 13: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

13

Pass By Reference - &

int myfunction (int& x, int& y){ x = 60; y = 600; return x + y;}

int main(){ int a = 5, b = 10; int sum = myfunction(a, b); cout << a << “ “ << b << endl; return 0;}

60

600

a

b

x

y

• x and y refer back to their arguments, a and b• changing x or y DOES affect a or bWhat would main display?

Page 14: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

14

Function Prototypeint myfunction(int&, int&);

int main(){ int a = 5, b = 10; int sum = myfunction(a, b); cout << a << “ “ << b << endl; return 0;}

int myfunction (int& x, int& y){ x = 60; y = 600; return x + y;}

• Before a function can be called, the compiler must be told about its legal format• Function definition can

be placed before the call, or we can use a prototype

• Function Prototype allows the compiler to determine if a function call is legal• Sort-of like a function declaration (like a variable declaration)

Prototype

Page 15: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

15

Default Function Arguments

Default Arguments are arguments that are passed to parameters automatically if no argument is provided in

the function call

void displayStars(int = 10, int = 1);

Page 16: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

16

Default Argumentsvoid displayStars(int=10, int=1);int main(){displayStars();displayStars(5);

displayStars(7,3);return 0;

}void displayStars(int cols, int rows){for (int down=0; down<rows; down++){

for (int across=0; across<cols; across++)cout << “*”;

cout << endl;}

}

Page 17: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

17

Default Argumentsvoid displayStars(int=10, int=1);int main(){displayStars();displayStars(5);

displayStars(7,3);return 0;

}void displayStars(int cols, int rows){for (int down=0; down<rows; down++){

for (int across=0; across<cols; across++)cout << “*”;

cout << endl;}

}

Page 18: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

18

Default Argumentsvoid displayStars(int=10, int=1);int main(){displayStars();displayStars(5);

displayStars(7,3);return 0;

}void displayStars(int cols, int rows){for (int down=0; down<rows; down++){

for (int across=0; across<cols; across++)cout << “*”;

cout << endl;}

}

Page 19: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

19

Default Function Arguments Rule

When an argument is left out of a function call, all arguments that come after it must also be left out.

displayStars(,3)//Illegal

someFunc(1,,3)//Illegal

Page 20: CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2

20

?