C++ (intro)
Hwansoo Han
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 <iostream>
using namespace std;
// main function
int main()
{
cout << “hello, world!” << endl;
return 0;
}
console output
C++ IDE
Download Eclipse CDT (C/C++ development tooling)
www.eclipse.org/downloads
Download “Eclipse IDE for C/C++ Developers”
Unzip the downloaded file into a directory
E.g. D:\bin\
Download JRE 6.0 or above
Google “JRE 64bit windows 7”
E.g. Downloads.cnet.com
Run the downloaded file to install JRE
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; }
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
Program Images, Variables, Objects
10
Address space
Local variables : stack
Global variables : data
Dynamically allocated objects : heap
code
data
heap
stack
kernel 0xC0000000
0xFFFFFFFF
0x080482d0
int a = 1; // ?
int main() {
int b = 1; // ?
int *c; // ?
c = (int*)malloc(sizeof(int)); // ?
}
C++ vs. C vs. Java: Major Differences
11
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;
}
C++ vs. C vs. Java: Major Differences
12
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
}
/* 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
}
Coding Style – Managing Large Code
13
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
14
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
10 + 8.2 ⇒ 18.2
Functions, objects (C++/Java), arithmetic operators, …
Makefile - Example
15
target: dependency_list
[TAB]command_list
$@ : name of target
$^ : dependecy_list
First target is a default target
> make
> make all
> make random
> make clean
Comment
Variable definition
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
$(CXX) $(CXXFLAGS) –o $@ $^
random: random.cpp
$(CXX) $(CXXFLAGS) –o $@ $^
clean:
rm –f $(PROGRAMS)
@echo “--binaries are removed”