C++ (intro) Created by Hwansoo Han Edited by Ikjun Yeom
Why New Languages? Already know C, Java, Python, Matlab, … C++ Basically extension to C All C code compiled with C++ compilers But many libraries and “OO” concepts added 2
Many Use C++ Most recent open source projects 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) 3
Hello, World! 4 /* * First program in C++ : hello world */ #include using namespace std; // main function int main() { cout << “hello, world!” << endl; return 0; } /* * First program in C++ : hello world */ #include using namespace std; // main function int main() { cout << “hello, world!” << endl; return 0; } console output
C++ IDE Download Cygwin 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\ 5
Genealogy of Programming Languages 6
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!”); 7
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; } 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; } 8
C++ vs. C vs. Java: Major Differences 9 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
C++ vs. C vs. Java: Major Differences 10 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 /* Java */ void AddOne(int x) { x++; } void run() { int x = 7; AddOne(x); println(x); // output 7 } /* Java */ void AddOne(int x) { x++; } void run() { int x = 7; AddOne(x); println(x); // output 7 } /* C++ */ void AddOne(int &x) { x++; } int main() { int x = 7; AddOne(x); cout << x << endl; // output 8 } /* C++ */ void AddOne(int &x) { x++; } int main() { int x = 7; AddOne(x); cout << x << endl; // output 8 } /* 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); printf(“%d\n”, x); // output 8 }
Coding Style – Managing Large Code 11 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.
C++ vs. C vs. Java: Similarities 12 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, …