C++ and C Programming Brief Tutorial

Wednesday, March 17, 2010

This pocket reference was created to help the author to refresh C++- programming, presumably aiming at an intermediate programmer or advanced. This pocket reference is mainly based on Deitel & Deitel’s C++: How To Program, 4th ED., and, thus, it can be considered a brief summary of the text. It’s free to distribute/modify. Any inquiry should be mailed to joumon@cs.unm.edu. o C++ – C++ is a, loosely speaking, OOP language with some data types such as char, short, int, long, float, double, i.e. a combination of C and OOP – C++ has strengthened type-checking – namespace is introduced o Compile/Execute 1. edit a source file(s); i.e. at least one file should have main() function somewhere 2. compile it(them), generating machine code, link, and load 3. execute a binary ex) > c++ main.cc > ./a.out o Syntax/Semantics/Algorithms – syntax: grammar – semantics: meaning – algorithms: an efficient way to solve a problem cf. syntax error/semantic error/run-time error or execution error/ debugging o Basic C++ Program // preprocessor #include // namespace using namespace std; // typedef s and/or struct typedef int bool; // define s #define PI 3.14 // function declarations come here … // class declarations come here … // method implementations come here … int main(int argc, char** argv) { // variables come here … // statements come here … } // function implementations come here … o Name Space – namespace is used to define scope, i.e. it is an abstract container defining context for itentifiers ex) using namespace std; o Constants – See C-Pocket book for more details – enum : used to enumerate some predefined data ex) enum Status {ON, OFF}; Status s; // cf. enum Status s; in C o Function – functions allow to modularize a program – variables defined inside a function are local – why functions? 1. divide-and-conquer makes development more manageable 2. software reusability 3. avoiding repeating code in a program – requires the main function in a program – use void if a function doesn’t receive any parameters or return any data – when a function implementation is made after a caller function, function prototype, which tells the compiler the name of function, should be declared ex) void foo(int, double); // function prototype int main() {… foo(); … } void foo(int i, double d) { … } // function implementation o Storage Class – auto , register , extern , mutable , static – auto : local variable for the automatic storage class – register : advise the compiler to use a register, if available, and is used for the automatic storage class – automatic storage class: variables are created when program execution enters the block in which they are defined, they exist while the block is active, and they are destroyed when the program exits the block – local variables shadow global variables – static : can retain the value after the function returns to its caller and used for the static storage class – extern : global variables belong to this by default and used for the static storage class – static storage class…

Download C++ and C Programming Brief Tutorial.Pdf

0 comments:

Post a Comment

 
 
 

locate all visitor site