Presentation is loading. Please wait.

Presentation is loading. Please wait.

Created by Hwansoo Han Edited by Ikjun Yeom

Similar presentations


Presentation on theme: "Created by Hwansoo Han Edited by Ikjun Yeom"— Presentation transcript:

1 Created by Hwansoo Han Edited by Ikjun Yeom
C++ (intro) Created by Hwansoo Han Edited by Ikjun Yeom

2 Why New Languages? Already know C++ C, Java, Python, Matlab, …
Basically extension to C All C code compiled with C++ compilers But many libraries and “OO” concepts added

3 Many Use C++ Most recent open source projects Natural extension to C
Written in C++ or Java Easier to understand – “OO” design Natural extension to C C and C++ can be easily mixed (with a little efforts)

4 Hello, World! /* * First program in C++ : hello world */
#include <iostream> using namespace std; // main function int main() { cout << “hello, world!” << endl; return 0; } console output Comment, header include, main, cout

5 C++ IDE Download Cygwin Download JRE 6.0 or above
Set PATH to include g++ or gcc (제어판>시스템및보안>시스템>고급시스템설정>고급tab>환경변수) Download JRE 6.0 or above Google “JRE 64bit windows 7” E.g. Downloads.cnet.com Run the downloaded file to install JRE Download Eclipse CDT (C/C++ development tooling) Download “Eclipse IDE for C/C++ Developers” Unzip the downloaded file into a directory E.g. D:\bin\

6 Genealogy of Programming Languages

7 C++ vs. C vs. Java: Minor Differences
Boolean type C: int myBooleanValue = 1; C++: bool myBooleanValue = true; Java: boolean myBooleanValue = true; Printing C: printf(“Hello!\n”); C++: cout << “Hello!” << endl; Java: println(“Hello!”);

8 C++ vs. C vs. Java: Minor Differences
Calling other functions C/C++ : define before use Java : no such worries void PrintHello(); void PrintTime() { cout << “It’s time to go!” << endl; } int main() { PrintHello(); // OK – Prototype defined earlier PrintTime(); // OK – Function defined earlier SayHi(); // ERROR – no prototype earlier } void PrintHello() { cout << “Hello!” << endl; } void SayHi() { cout << “Hi!” << endl; }

9 C++ vs. C vs. Java: Major Differences
C/C++ has no garbage collector Java: Integer myInt = new Integer(); myInt = NULL; // GC will delete C++ : programmers need to delete C : programmers need to free Objects Java : memory for all objects is put on the heap C++ : Objects can be placed either on the stack or the heap C : no objects, malloc gets the space from the heap

10 Program Images, Variables, Objects
Address space Local variables : stack Global variables : data Dynamically allocated objects : heap code data heap stack kernel 0xC 0xFFFFFFFF 0x080482d0 int a = 1; // ? int main() { int b = 1; // ? int *c; // ? c = (int*)malloc(sizeof(int)); // ? } a: data, b,c: stack, *c(malloc): heap

11 C++ vs. C vs. Java: Major Differences
Stack vs. Heap allocation in C/C++ int main() { int a = 1; // STACK int b(2); // STACK int *c = new int(3); // HEAP cout << “a=“ << a << “ addr=“ << &a << endl; cout << “b=“ << b << “ addr=“ << &b << endl; cout << “c=“ << *c << “ addr=“ << c << endl; delete c; } int fn() { int *parray = new int [10]; // STACK // … delete [] parray; a: stack, b: stack, c: heap

12 C++ vs. C vs. Java: Major Differences
C++ can choose to pass by value, pointer, and reference C can choose to pass by value or pointer Java cannot choose Primitives only pass by value Objects only pass by reference /* C */ void AddOne(int *x) { (*x)++; } int main() { int x = 7; AddOne(&x); printf(“%d\n”, x); // output 8 } /* C++ */ void AddOne(int &x) { x++; } int main() { int x = 7; AddOne(x); cout << x << endl; // output 8 } /* Java */ void AddOne(int x) { x++; } void run() { int x = 7; AddOne(x); println(x); // output 7 }

13 Coding Style – Managing Large Code
Instead putting all into a single file, separate them for easier management Files with extension .cpp/.cc contains implementations of functions Files with extension .h contains function prototype .h files are used to overall shape of source code Other programmers can roughly recognize your programs through header files (.h files) If you don’t care the details on how a function works, you don’t have to look into .cpp files.

14 C++ vs. C vs. Java: Similarities
Short circuit evaluation if (x < 4 || y == 3) // test the first (x<4), if true skip the second Integer division 9/4 ⇒ 2 Primitives char, short, int, long, float, double Type conversion 3/2.0 ⇒ 1.5 ⇒ 18.2 Functions, objects (C++/Java), arithmetic operators, …

15 First Assignment – Guess Number
Print out “Welcome to guess game!” Declare a variable secret (0..100) Print out the range of secret value Set secret value with random() Loop until exit Get a guess from the user If the guess is equal to secret, exit Print if the guess is high or low Print out “Congratulations!” /* C++ random value */ #include <cstdlib> #include <ctime> int main() { srandom(time(NULL)); int x = random(); } /* C++ cin, cout */ int main() { int x = 0, inum; cin >> inum; /* IN */ x += inum; cout << x << endl; }

16 First Assignment – Guess Number
DUE: midnight (23:59:59), Sep. 19 (Wed) Submit to Submit: student_id.tar includes guess.cpp, io.cpp, guess.h, Makefile, Readme.txt guess.cpp: #include “guess.h”, main(), check() io.cpp: getNum(), printCongrats() guess.h: declare types of getNum(), printCongrats();

17 Makefile - Example Comment Variable definition target: dependency_list
List of targets # Makefile for hello, mem, and random CXXFLAGS = -O3 –Wall CXX = g++ PROGRAMS = hello mem random all: $(PROGRAMS) hello: hello.cpp $(CXX) $(CXXFLAGS) –o $^ mem: mem.cpp random: random.cpp clean: rm –f $(PROGRAMS) @echo “--binaries are removed” target: dependency_list [TAB]command_list : name of target $^ : dependecy_list First target is a default target > make > make all > make random > make clean


Download ppt "Created by Hwansoo Han Edited by Ikjun Yeom"

Similar presentations


Ads by Google