18
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

Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 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

Lecture 01a: C++ reviewTopics:• 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

Page 2: Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output

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!

Page 3: Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output

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 <iostream>using namespace std;

int main(){ cout << "Hello, World!" << endl; return 0;}

Page 4: Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output

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]

Page 5: Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output

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.

Page 6: Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output

General layout0

… 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.

Page 7: Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output

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)

Page 8: Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output

Variable Declaration

• C/C++ uses explicit declarationint x = 6; // declaration (and init)cout << x; // use

• Compare that to Python/MathPiper/etc which use implicit declaration

x = 6 # implicit declarationprint(x) # use

Page 9: Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output

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

Page 10: Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output

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

Page 11: Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output

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 << and/or >> operators (most built-in types have this)

Page 12: Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output

Example: cin/cout#include <iostream>#include <string>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;

}

Page 13: Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output

Example: fstream#include <fstream>#include <iostream>#include <cstdlib>#include <ctime>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 << rand() % 100 << endl; fp << rand() % 100 << endl; fp.close(); }

Page 14: Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output

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 >> temp; avg += temp;

fp.close(); cout << "The average is " << avg / 3.0 << endl; }}

Page 15: Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output

Part VI: Operators, Expressions

• No surprises here (high precedence => low):

• Reference: http://cplus.about.com/od/learning1/ss/cppexpressionsr_7.htm

Level Operator

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 ,

Page 16: Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output

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

Page 17: Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output

Simple example#include <iostream>using namesapce std;

int main(){char * cptr;char c = 'a'; // 97double * 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]