Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 39 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
39
Dung lượng
145 KB
Nội dung
C++ o Why use C++? ANSI standard Compilers available on every platform All libraries (to my knowledge) have C and/or C++ API o Is C++ better than Fortran? Structure Object orientation Reusable code and library creation Excellent error checking at compile time Fortran becomes more like C++ with every release o Microsoft Visual C++ Development environment Create console (DOS) applications Can develop full-blown Windows applications Introduction C++ Course layout Week 1 Learn about C, the subset of C++ (not all C covered, some C++ stuff used) Week 2 C++ proper; classes and objects Quick question and answer session after each section C++ Sections 1) Form of a C++ program 2) Data types 3) Variables and scope 4) Operators 5) Statements 6) Arrays 7) Pointers 8) Functions 9) Structures 10) Console I/O 11) File I/O 12) Pre-processor instructions 13) Comments 14) Compiling examples under Unix Lecturer may pop out for a smoke after some sections C++ Form of a C++ program #include <iostream.h> int main(int argc, char* argv[]) { cout << “Hello World\n”; return 0; } pre-processor statement semi-colons braces enclosing function All C++ programs must have one, and only one, main function arguments function Example: helloworld C++ Some differences with Fortran o C++ is case sensitive: x != X o You can write it all on one line - statements separated by ‘;’ o There are NO built-in functions but lots of standard libraries o There is NO built-in I/O but there are I/O libraries o There are about 60 keywords - need to know about 20? o C++ supports dynamic memory allocation o All C++ compilers are accompanied by the C pre-processor o You can potentially screw up the operating system with C++ C++ Header files declare the functions to be linked in from libraries… #include <iostream.h> #include <math.h> #include “D:\andy.h” Including header files and library functions C++ programs try to be ‘lean and mean’; programmer chooses what to include in the executable read & write functions sqrt, log, sin, abs, etc My stuff that I use frequently and don’t want to recreate If header is in “” the filename must be full (or relative); if in <> pre-processor searches in pre-defined folders C++ Microsoft Visual C++ o Each program is created within a ‘project’ o A project can only contain ONE ‘main’ function o Basically each individual project is a program o Each project is stored in a folder o To open a project, double-click the ‘.dsw’ file in the folder o Click Help>Index and type a query - massive documentation o Or select (highlight) a keyword and press F1 on keyboard build run VC++ is a big application: a separate course! C++ Example programs and VC++ Look at http://www.liv.ac.uk/~aeh o Get VC++ installation disks from Alan (sign license for Mr Gates) o Projects can be downloaded - they are zipped (PowerZip/WinZip) o You might need to go to Project>Settings and click Debug tab and set your own path to where the project is stored (I suggest M:\C++\ as the folder to unzip the examples into) o Experiment with the examples - compilation errors appear in the window below the source window(s)… double-click on the error message to go to the line containing the error. o You can highlight a keyword (or whatever) and press F1 to get help o The documentation is huge - and not just about VC++; be selective! C++ Data types There are 6 atomic data types: 1) char - character (1 byte) 2) int - integer (usually 4 bytes) 3) float - floating point (usually 4 bytes) 4) double - double precision floating point (usually 8 bytes) 5) bool - true or false (usually 4 bytes) 6) void - explicitly says function does not return a value and can represent a pointer to any data type Size of the data types depends on machine architecture e.g. 16 bit, 32 bit or 64 bit words Other data types are derived from atomic types e.g. long int Can use ‘typedef’ to alias your own data type names; defining C++ classes creates new types C++ is a programmer’s language - need to know the basics… C++ Variables and scope int a, b, c; a = 1; b = c = 0x3F; float iAmAFloat = 1.234; double iAmADouble = 1.2e34; { int i, a; for (i=0; i<10; i++) { a = i; int b = i; } b = 2; } ‘a’ declared outside loop braces ‘a’ used inside braces, OK ‘b’ declared inside braces; ‘b’ is in scope inside braces ‘b’ is unknown outside braces: ‘b’ is out of scope, ERROR [...]... menuchoice C++ Iteration: ‘for’ loop Huge amount of variation allowed, very flexible: generally for (initialisation; condition; increment) { statements } Initially i is set to zero int i, a[10]; int sum = 0; Keeps looping while i is less than 10 (i.e condition is ‘true’) for (i=0; i c; The loop body will execute } forever - until “q” is pressed cout . (i<3 || i>6) { continue; } count++; } infinite loop jump out of loop (if loops nested, only jump out of inner) Jump to beginning of loop and begin next iteration C++ Expressions An expression. i; } Initially i is set to zero Keeps looping while i is less than 10 (i.e. condition is ‘true’) i increments by one on each iteration C++ Iteration: ‘while’ loop General form is while (condition). after braces This is a label, and so has a ‘:’ after it Example: menuchoice C++ Iteration: ‘for’ loop Huge amount of variation allowed, very flexible: generally for (initialisation; condition; increment)