+ All Categories
Home > Documents > 1 Intro to C/C++ #include using namespace std; int main() { cout

1 Intro to C/C++ #include using namespace std; int main() { cout

Date post: 22-Dec-2015
Category:
View: 218 times
Download: 2 times
Share this document with a friend
32
1 Intro to C/C++ #include <iostream> using namespace std; int main() { cout << “Hello world!” << endl; return 0; } #include <stdio.h> int main() { printf(“Hello world!\n”); return 0; } C++ C
Transcript
Page 1: 1 Intro to C/C++ #include using namespace std; int main() { cout

1

Intro to C/C++

#include <iostream>using namespace std; int main() {

cout << “Hello world!” << endl;return 0;

}

#include <stdio.h>int main() {

printf(“Hello world!\n”);return 0;

}

C++

C

Page 2: 1 Intro to C/C++ #include using namespace std; int main() { cout

2

Compile & run

Compile: g++ –g -o hello hello.cpp -g: allow debugging

Run: hello or ./hello

Page 3: 1 Intro to C/C++ #include using namespace std; int main() { cout

3

Intrinsic types

byteshortintlongfloatdoublechar boolean

Java C/C++

charshortintlongfloatdoublechar boolean

Page 4: 1 Intro to C/C++ #include using namespace std; int main() { cout

4

Conditionals

bool temp = true;int i = 2;

if(temp) cout << “Hello world!” << endl;

if(temp==true) cout << “Hello world!” << endl;

if(i) cout << “Hello world!” << endl;

An non-zero value is true (better to use 1).A zero value is false.

Page 5: 1 Intro to C/C++ #include using namespace std; int main() { cout

5

Control flow

for loop: for(i=1; i<=9; i++){…}

while loop: while(cond){…}

if, else if(cond){…} else{…}

do, switch

Page 6: 1 Intro to C/C++ #include using namespace std; int main() { cout

6

File I/O

Standard I/O cout and cin are special files cout << “hello.” << endl; (print hello on

screen) cin >> a; (read a from the console)

File I/O Open file Read/write Close file

Page 7: 1 Intro to C/C++ #include using namespace std; int main() { cout

7

Command-line arguments#include <iostream>#include <fstream>using namespace std;

int main(int argc, char * argv[]){ int num; ifstream input; if(argc != 3){ cout << "Usage: " << argv[0] << " <number> <file

name>" << endl; exit(1); }

num = atoi(argv[1]); input.open(argv[2]); input.close(); return 0;}

Page 8: 1 Intro to C/C++ #include using namespace std; int main() { cout

8

Key differences between C/C++ and Java

Pointers Assignment Parameter passing Heap & Stack Arrays

Page 9: 1 Intro to C/C++ #include using namespace std; int main() { cout

9

Pointers

C/C++ Pointer is just an address

Address: location of a piece of data in memory

&x: the address of x; *ptr: dereference ptr

Dereference pointer to a classSomeclass *ptr; //declare a pointercout << ptr->a << endl;cout << (*ptr).a << endl;

Page 10: 1 Intro to C/C++ #include using namespace std; int main() { cout

10

Pointers II

#include <iostream>using namespace std;int main(int argc, char * argv[]){ int x; int * ptr; x = 5; ptr = &x; cout << "ptr " << *ptr << endl; //print 5 x=10; cout << "ptr " << *ptr << endl; //print 10 return 0;}

Page 11: 1 Intro to C/C++ #include using namespace std; int main() { cout

11

Pointer exercises

Data structure for an itemstruct myItem{ int val; myItem * ptr;};

Create a linked list with two items Print the value of items in the list Remove the last item in the list

Page 12: 1 Intro to C/C++ #include using namespace std; int main() { cout

12

One way to do it

#include <stdio.h>struct myItem{ int val; myItem * ptr;};int List_Items(myItem * head){ myItem *cur_item; cur_item = head; while(cur_item != NULL){ printf("cur_item value %d\n", cur_item->val); cur_item = cur_item -> ptr; } return 1;}

Page 13: 1 Intro to C/C++ #include using namespace std; int main() { cout

13

One way to do it (cont’d I)int main(){ myItem *head; myItem *item1, *item2; item1 = new(myItem); item1->val = 1; item1->ptr = NULL; item2 = new(myItem); item2->val = 2; item2->ptr = NULL; //now link the items head = item1; item1->ptr = item2;

//list the items List_Items(head);

Page 14: 1 Intro to C/C++ #include using namespace std; int main() { cout

14

One way to do it (cont’d II) //now delete the last item in the list (last item

ptr==NULL) myItem *cur_item, *next_item; cur_item = head; next_item = cur_item->ptr; while(next_item->ptr != NULL){ cur_item = next_item; next_item = next_item->ptr; } delete(next_item); cur_item->ptr = NULL; printf("after remove the last item:\n"); List_Items(head); return 1;}

Page 15: 1 Intro to C/C++ #include using namespace std; int main() { cout

15

Assignment

Java assignment: makes reference

C++ assignment: makes copySomeClass x, y;Someclass *a;

x=y; //this copies y to x; changing x does not change y

a = &y; //copies the reference (address) of y to a;

a->val += 10; //this changes y too

Page 16: 1 Intro to C/C++ #include using namespace std; int main() { cout

16

Parameter passing: default by value#include <iostream>using namespace std;

void foo(int para){ // para += 10;}int main(int argc, char * argv[]) { int i=5; foo(i); //all parameters copied by default cout << "i " << i << endl;}

Page 17: 1 Intro to C/C++ #include using namespace std; int main() { cout

17

Parameter passing: how to change value? (pass pointer or call by references)#include <iostream>using namespace std;void foo(int * para){ *para += 10;}void bar(int & para){ para += 10;}int main(int argc, char * argv[]){ int i=5; foo(&i); cout << “after foo: i " << i << endl; bar(i); cout << “after bar: i " << i << endl; return 0;}

Page 18: 1 Intro to C/C++ #include using namespace std; int main() { cout

18

Stack & heap

Stack: regions of memory for temporaries Stack pointer pushed in on function

entry Stack pointer pushed out on function

exit Heap: regions of memory for

persistent data C/C++: explicitly managed

Page 19: 1 Intro to C/C++ #include using namespace std; int main() { cout

19

Big stack mistake (never return pointer to a stack!)#include <iostream>using namespace std;int * foo( ) { int b=10; return &b;}int main(int argc, char * argv[]) { int *a; a = foo(); cout << "a: " << *a << endl; //print 10? return 1; }

Page 20: 1 Intro to C/C++ #include using namespace std; int main() { cout

20

Return value from heap (Good!)#include <iostream>using namespace std;int * foo() { int *b = new (int); *b = 10; return b;}int main(int argc, char * argv[]) { int *a; a = foo(); cout << "a: " << *a << endl; //print 10? return 1;}

Page 21: 1 Intro to C/C++ #include using namespace std; int main() { cout

21

Explicit memory management

You must delete items (or memory leak)

Deleting items too soon – crash Deleting items twice – crash Good practice:

int * a = new(int);After deleting a, do a=NULL; if(a!=NULL) delete a;

Page 22: 1 Intro to C/C++ #include using namespace std; int main() { cout

22

Struct: class with everything public#include <iostream>using namespace std;struct foo { int mem1, mem2;};int main(int argc, char * argv[]) { foo a, *b; a.mem1 = 1; a.mem2 = 2; b = &a; b->mem1 += 10; cout << b->mem1 << endl; //print ? return 1;}

Page 23: 1 Intro to C/C++ #include using namespace std; int main() { cout

23

Class declaration: similar to Java

class IntCell{public: IntCell (int initVal = 0){ storedVal = initVal; } int getVal() {return storedVal;} int setVal(int val) {storedVal = val;}

private: int storedVal;

};

Page 24: 1 Intro to C/C++ #include using namespace std; int main() { cout

24

Arrays

Arrays do not have to be allocated with new

Array bounds not checked (careful!) Avoid pointer arithmetic

item[12]=0; Don’t use: v=12, *(item+v)=0;

Multiple dimensional array int item[10][20];

Page 25: 1 Intro to C/C++ #include using namespace std; int main() { cout

25

Other features - I

Assert (good style)int *ptr = NULL; …assert(ptr != NULL);

Typedeftypedef int * p_int;p_int a; //a is a pointer to an integer

commentsSame syntax as in java

Page 26: 1 Intro to C/C++ #include using namespace std; int main() { cout

26

Other features II

#define#define MAX 100

int myArray[MAX]; Static

May not mean the same thing as in java

Declaring a global variable or function as static means that it is only visible in this file.

Page 27: 1 Intro to C/C++ #include using namespace std; int main() { cout

27

Basics of Linux

On linux machine: Login at your home directory Open a “shell” or “terminal” or “xterm” workspace (4)

On windows machine Install linux terminals (xmanager, putty,

cygwin) Login

Page 28: 1 Intro to C/C++ #include using namespace std; int main() { cout

28

Basic commands of Linux I

Command format: [command] [arguments] Search information on a command: man

[command] e.g., man pwd

Print current directory: pwd List files and directories in a directory: ls

ls –l Only list directories: ls -ld Current directory: . upper-level directory: ..

Page 29: 1 Intro to C/C++ #include using namespace std; int main() { cout

29

Basics Commands of Linux II

File/directory operations: Copy file x to y: cp x y Remove file x: rm x

be very careful, rm –i x Move file x to y (rename): mv x y Create new directory x: mkdir x Change directory:

Go to upper level: cd .. Go to directory x: cd x

Remove directory: rm –r x Rename directory x to y: mv x y

Page 30: 1 Intro to C/C++ #include using namespace std; int main() { cout

30

Basics Commands of Linux III

State of processes: ps (process state)

ps –aux ps –aux | grep bing

Kill First use ps to find the ID of a process kill -9 -1 pid

Pipeline command: e.g., ps -aux | more

Page 31: 1 Intro to C/C++ #include using namespace std; int main() { cout

31

Basics Commands of Linux IV

Mode of a program: list mode of f: ls –l f 3 components: for owner, group

member, others 3 bits in each component: r, w, x e.g., mode of 755 means?

Change the mode of a file: chmod E.g., chmod 755 f

Change owner of a program: chown

Page 32: 1 Intro to C/C++ #include using namespace std; int main() { cout

32

Programming on Linux

Write your code (using e.g., emacs, xemacs, vi, or eclipse)

Compile: e.g., g++ –g -o hello hello.cpp

Run: e.g.,hello or ./hello

Debug: print messages using gdb( locate segmentation fault) or

xxgdb (better interface)


Recommended