11. Primitive Data Types
Java C/C++ (32 bits) Range
void void n/a
char unsigned short / unsigned char 2 bytes / 1 byte
byte char / signed char 1 byte
-128 … 127
short short / short int
signed short
signed short int
2 bytes
-215 … (215 – 1)
int int / signed int
long / long int / signed long / signed long int
4 bytes
-231 … (231 – 1)
long long long / signed long long 8 bytes
-263 … (263 – 1)
boolean bool 1 byte
True/false
float / double float / double 4 bytes/ 8 bytes
12. Primitive Data Types
• NO “unsigned” (excepted char is an unsigned type)
• Set to “default value” when declare
• Separated to signed & unsigned
• NO “default value”
• No standard string, use char*
Type Range
char • (signed) char: -128 … 127
• unsigned char: 0 … 255
short • (signed) short: -215 … (215 – 1)
• unsigned short: 0 … (216 – 1)
int / long • (signed) int/long: -231 … (231 – 1)
• unsigned int/long: 0 … (232 – 1)
long long • (signed) long long: -263 … (263 – 1)
• usigned long long: 0 … (264 – 1)
C/C++
Use sizeof() to
get size of a type:
char c = 0;
sizeof(c) 1
13. New type definition
0 C/C++ only
0 Use typedef
typedef int Mytype;
typedef int MyArr[5];
Mytype var1;
MyArr arr;
15. Storage class specifiers
Key-work Desc. Example
auto Go out of scope once the program exits
from the current block
auto int var1;
int var2;
register Stored in a machine register if possible register int var;
static Allocated when the program begin and
deallocated when the program ends
static int var;
extern Specify that the variable is declared in a
different file. Compiler will not allocate
memory for the variable
extern int DefineElseWhere;
Static / non-static
int m_iVar;
static int s_StaticVar;
16. Why extern?
0 No need to notify compiler where is the
classes/ variable/ functions
0 MUST notify compiler where is the classes /
variables/ functions were declared
0 Use extern to notify that they were declared
somewhere
19. Variable
int todo()
{
unsigned int Age;
float a = 10;
System.out.println(Age);
}
Fail, Age is not set
value before using
int todo()
{
unsigned int Age;
float a = 10;
printf("%d", Age);
}
Age is not set value,
but still OK in C/C++
No default value caused
many mysterious bug
20. Variable
0 Static duration
static int var2;
struct C
{
void Test(int value)
{
static int var;
cout <<var <<endl;
var = value;
}
};
class MyClass
{
public:
static int s_Var;
};
int MyClass::s_Var = 0;
int main() {
C c1, c2;
c1.Test(100); c2.Test(100);
var2 = 10000;
MyClass::s_Var = 100;
}
Local static
Global static
Set default to zero
var = 0
Value must be set
outside class
definition
Static Class
member
21. Variable
0 C/C++ only
0 Stored in a machine register if
possible
0 Improve performance
register int index = 0;
26. Data structure
0 Used to set up collections of
named integer constants
enum MyEnumType { ALPHA, BETA, GAMMA };
enum MyEnumType x; /* legal in both C and C++ */
MyEnumType y; // legal only in C++
public enum Day {
SUNDAY, MONDAY, TUESDAY,
WEDNESDAY, THURSDAY,
FRIDAY, SATURDAY
}
27. Data structure
class JavaClass
{
public JavaClass() {…}
private void Toso() {…}
}
class CClass
{
public:
CClass() {…}
~CClass();
private:
void Toso() ;
};
#include "Any_name.h"
void CClass::Todo()
{
…
}
CClass::~CClass()
{
…
}
JavaClass.java
Any_name.h Other_name.cpp
28. Data structure
0 C/C++ only
0 A user-defined type that uses same
block of memory for every its list
member.
union NumericType
{
int iValue;
long longValue;
double dValue;
};
int main()
{
union NumericType Values = { 10 };
// iValue = 10
printf_s("%dn", Values.iValue);
Values.dValue = 3.1416;
printf_s("%fn", Values.dValue);
}
0 4 8
iValue
longValue
dValue
29. Data structure
0 C/C++ only
0 The C++ class is an extension of the C
language structure
0 Default access:
0 Class: private
0 Struct: public
class X {
// private by default
int a;
public:
// public member function
int f() { return a = 5; };
};
struct Y {
// public by default
int f() { return a = 5; };
private:
// private data member
int a;
};
Should be used for storing only.
See more: structure alignment
31. Pointer
0 No explicit pointer in Java
0 Array and object are implicit pointer
0 Explicit null value for array/object
Bicycle bike1 = new Bicycle();
int[] arr = new int [100]
bike1
arr
Two type of variable:
o Data type
o Pointer type
Implicit null value, aka zero value (0)
int i;
int *p = &i;
char *st = new char[10];
char *p2 = st;
char st2[] = "ABC“;
char p3[] = st2;
i p st2 = ABC st p2
null
0x0 NULL
sizeof(p) = ?
sizeof(st2) = ?
p = ?
*p = ?
&p = ?
32. String
0 No standard string in C/C++
0 Use char*, or char[] instead
0 String in C/C++ is array of byte, end with ‘0’
char *st = "String";
S t r i n g 0st
char st2[] = "String";
S t r i n g 0
st2
st = ?
&st = ?
*st = ?
st2 = ?
&st2 = ?
*st2 = ?
33. Function pointer
0 C/C++ only
0 Is variable store address of a function
// C
void DoIt (float a, char b, char c){……}
void (*pt2Function)(float, char, char) = DoIt;
// using
pt2Function(0, 0, 0);
// C++
class TMyClass
{
public:
void DoIt(float a, char b, char c){……};
};
void (TMyClass::*pt2Member)(float, char, char) = &TMyClass::DoIt;
// using
TMyClass *c = new TMyClass();
(c->*pt2Member)(0, 0, 0);
34. Object
0 Always use “new”
0 Object variable hold reference
0 Support two kinds:
0 Object variable hold values
0 Object variable hold reference
(through pointer)
Bicycle bike1 = new Bicycle();
bike1.changeCadence(50);
Bicycle bike1;
bike1.changeCadence(50);
Bicycle *bike2 = new Bicycle();
bike2->changeCadence(50);
(*bike2).changeCadence(100);
bike1 vs. bike2?
36. Function
• Declare inside class
• Pass-by-value only
• No need prototype
• Declare anywhere
• Pass-by-value / pass-by-reference optional
• Need prototype
How about
array and
object?
37. Function
0 Function Prototype
void todo();
void main()
{
todo();
}
void todo()
{
……
}
0 Pass by value / Pass by reference
void todo(int pass_by_value, int &pass_by_ref)
“&” is for pass
by reference
prototype
38. Pass-by-value
void swap1(int i, int j) void swap1(int i, int j)
void swap1(MyClass obj1, MyClass obj2)
swap
A
A’
B’
A
tmp = A;
A = B;
B = tmp
43. OOP - Inheritance
public class MyDog extends Dog implement ITalkable, ISmileble
{
public:
MyDog(){}
//ITalkable implement
void DoTalk();
//ISmileble
void DoSmile();
void Todo() { super.Todo();}
}
public class MyDog:public Dog,public ITalkable, protected ISmileble
{
public:
MyDog(){}
~MyDog() {}
//ITalkable implement
void DoTalk();
//ISmileble
void DoSmile();
void Todo() { Dog::Todo();}
}
Destructor, called when object is deallocated
44. OOP
Abstract class vs. Pure virtual class
public abstract class GraphicObject
{
// declare fields
// declare non-abstract methods
abstract void draw();
}
class classname
{
public:
virtual void virtualfunctioname() = 0;
};
Pure virtual function
45. OOP - virtual
• Function are virtual by default
• Use final to prevent override
• Use key word virtual
virtual int foo()
class ClassA
{
public:
ClassA() {}
virtual void Todo();
};
class ClassB: public ClassA
{
public:
ClassB():ClassA() {}
void Todo();
}
47. OOP – virtual destructor
class ClassA
{
public:
ClassA() {}
virtual ~ClassA() {printf("DeleteAn");}
};
class ClassB: public ClassA
{
public:
ClassB():ClassA() {}
~ClassB() {printf("DeleteBn");}
};
void main()
{
ClassA *Obj = new ClassB();
delete Obj;
}
Without virtual, only destructor
of ClassA is called
49. #define NULL 0
int main() {
// Allocate memory for the array
char* pCharArray = new char[100];
// Deallocate memory for the array
delete [] pCharArray;
pCharArray = NULL;
// Allocate memory for the object
CName* pName = new CName;
pName->SetName("John");
// Deallocate memory for the object
delete pName;
pName = NULL;
}
Memory management
Use GC to collect garbage data
• No GC mechanism, handle manually
• Use delete / free to clean up
unused heap
• Memory leak problem
55. Appendix A: preprocessor
#include
0 Include another file.
#include <stdio.h>
int main (void)
{
printf("Hello, world!n");
return 0;
}
The preprocessor replaces the line #include <stdio.h> with the system header file of
that name
56. Appendix A: preprocessor
#define
0 Define a macro, object-like and function-like
#define Min(x, y) ((x)<(y))?(x):(y)
Object-like #define PI 3.14
Function - like
#define PI 3.14
#define Min(x, y)((x)<(y))?(x):(y)
void main()
{
int pi = PI;
int num = Min(pi, 2);
}
#undef
0 Un-defined a macro
57. Appendix A: preprocessor
#if, #ifdef, #ifndef, #if defined(…)
#elif, #else,
#endif
0 Directives can be used for conditional compilation.
#ifdef _WIN32
# include <windows.h>
#else
# include <unistd.h>
#endif
#if VERBOSE >= 2
print("trace message");
#endif
#if !defined(WIN32) || defined(__MINGW32__)
...
#endif
59. Prevent duplicate declaration
0 Use #ifndef
#ifndef __H1_H__
#define __H1_H__
void Todo();
//something else
#endif
H1.h
#pragma once
void Todo();
//something else
H1.h
0 Use #pragma once
Hinweis der Redaktion
1. Pre-process: First the C pre-processor (cpp.exe) is run on each source (.cpp) file. This interprets pre-processor directives, which are indicated by the # symbol. The output of the preprocessor will be files containing C++ code.
2. Compile: The expanded source code file resulting from pre-processing is compiled to produce an object file (suffix .obj or .o) containing the raw machine instructions.
Linking: The Linker (Loader) combines one or more object files together with standard libraries (which contain collections of object files), solving all the external references to produce an executable (suffix .exe or no suffix) are solved. This is the program that one actually runs.
The objects files are program modules containing machine code and information for the linker
Storage class specifiers tell the compiler the duration and visibility of the object or function they declared as well as where an object be stored
Static duration means that the object or variable is allocated when the program starts and is deallocated when the program ends