Download presentation
Presentation is loading. Please wait.
Published byNoel Benson Modified over 9 years ago
1
Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output (console and file) Operators / Expressions Basic pointers
2
Preface Recall: C++ is made of – ANSI-C ("older") – Preprocessor directives (#xxx) – C++ extensions (classes, STL, …) – Templates In this class, I'm going to mainly use – C++ (e.g. cout instead of printf) – I won't use an C++11 (C++0X) Most compilers don't fully support it…yet – C++ is intentionally multi-paradigm I'm assuming you understood ~90% of the material in ETEC1101 – This should all be review…so we'll move quickly – If not, it's your responsibility to get up to speed…fast!
3
Part I: Setting-Up [Set up a project in VC++ and Netbeans and Code::Blocks] Skeleton code Note: this is what a console application looks like. – Other types of applications (e.g..DLL,.LIB, Windows- EXE) will look different #include using namespace std; int main() { cout << "Hello, World!" << endl; return 0; }
4
Part II: Memory Diagrams A tool I use frequently to model current state of RAM Mimics most executable formats, but isn't modeled after a specific architecture / OS Four main areas: – Stack: Contains static variables, saved registers, call stack – Heap: Contains dynamically allocated memory chunks – Globals: Contains global and static variables. – Machine Code [not usually shown]
5
Overview, cont. In reality, the physical addresses of variables are determined by: – the architecture (x86, ARM, …) – the OS (OSX, Windows, …) the other processes currently running – the compiler (MinGW, VC++, …) …I'll just use relative addresses – The CPU and OS together resolve this to a physical address at run-time.
6
General layout 0 …HEAP ? ?+1 …STACK 10,000 10,001 …GLOBALS 19,999 20,000 CODE The HEAP and STACK share the same memory. Items are added to the stack in reverse order.
7
Part III: C++ variables / types C++ variables can be made from these types: – integer types (char, short, int, long int) – floating-point types (float, double) – booleans (hold a true or false – really just an integer) – pointers (to ___; an address) – [homogeneous] arrays – classes user-defined (e.g. Spaceship) STL-defined (standard template library) (e.g. vector, string, etc) From a library (e.g. SDL_Surface)
8
Variable Declaration C/C++ uses explicit declaration int x = 6;// declaration (and init) cout << x;// use Compare that to Python/MathPiper/etc which use implicit declaration x = 6# implicit declaration print(x)# use
9
Variables and memory When you include a declaration – compiler generates code which will allocate memory at the appropriate time – The architecture / OS target determines the amount allocated e.g. on a 32-bit system, int's might be 4 bytes, but on a 64-bit system they could be 8. sizeof function / operator
10
Part IV: const One of the most confusing syntaxes in C/C++ Performs exactly like the non-const version… …but the compiler won't allow changes. Good for: – list sizes – things like pi –…–… int x = 5; const int y = 5; x = 6; y = 6;// compile error
11
Part V: Input / Output Just focusing on cin / cout here (although you can still use printf / scanf) Both are considered C++ streams – as are fstream's (for files), and stringstream's (for building up strings) – Input streams (cin and file-reading) use the >> operator – Output streams (cout, file-writing, and stringstreams) use the << operator – The data passed can be anything which has overloaded the > operators (most built-in types have this)
12
Example: cin/cout #include using namespace std; int main() { int age; string name; cout << "Enter your name: "; cin >> name;// no spaces allowed! cout << "Enter your age: "; cin >> age; cout << "\"" << name << "\", in 5 years, you'll be"; cout << age + 5 << endl; }
13
Example: fstream #include using namespace std; int main() { fstream fp; float avg, temp; // Seed the random generator srand(time(NULL)); // Create (or replace) the contents of test.txt fp.open("test.txt", ios::out); if (fp.is_open()) { fp << rand() % 100 << endl; fp.close(); }
14
Example: fstream // Read data in from the file and compute the average fp.open("test.txt", ios::in); if (fp.is_open()) { fp >> avg; fp >> temp; avg += temp; fp.close(); cout << "The average is " << avg / 3.0 << endl; } }
15
Part VI: Operators, Expressions No surprises here (high precedence => low): Reference: http://cplus.about.com/od/learning1/ss/cp pexpressionsr_7.htm http://cplus.about.com/od/learning1/ss/cp pexpressionsr_7.htm LevelOperator 1() grouping 2++ -- post-fix 3! ~ 3+ - unary 3& address-of 3* de-reference 4* / % 5+ - 6 > bit-shift 7 = == != 8& ^ | bit-wise 9&& || 10= *= += /= %= ~= &= |= >= 11?: ternary conditional 12,
16
Part VII: Basic Pointers Pointers themselves are just integers – Regardless of what they point to! – They hold an address – The type of the pointer is important When working with arrays (later) When working with pointers to objects (later) When de-referencing a pointer
17
Simple example #include using namesapce std; int main() { char * cptr; char c = 'a'; // 97 double * dptr = NULL; double d = 3.17, e = 9.3; int x; x = sizeof(c); x = sizeof(cptr); x = sizeof(dptr); cptr = &c; *cptr = 'b'; dptr = &d; *d = 4.2; dptr = &e; *dptr = *dptr * 2.0; } [Do the memory diagram on the board]
18
const and pointers From: http://www.codeguru.com/cpp /cpp/cpp_mfc/general/article.p hp/c6967/Constant-Pointers- and-Pointers-to-Constants.htm http://www.codeguru.com/cpp /cpp/cpp_mfc/general/article.p hp/c6967/Constant-Pointers- and-Pointers-to-Constants.htm char a = 'A', b = 'B'; char * ptr1 = &a;// Non-const const char * ptr2 = &a;// Can't change char pointed to char * const ptr3 = &a;// Can't change address of ptr const char * const ptr4 = &a;// Can't change either *ptrX = 'c';ptrX = &b ptr1 ptr2 ptr3 ptr4
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.