Difference between revisions of "C++"

From Colettapedia
Jump to navigation Jump to search
Line 21: Line 21:
 
}</pre>
 
}</pre>
 
* [http://www.netalive.org/codersguild/posts/1753.shtml The skinny on typedef struct blah syntax and what they mean for c and c++]
 
* [http://www.netalive.org/codersguild/posts/1753.shtml The skinny on typedef struct blah syntax and what they mean for c and c++]
 +
* Functions must be declared static if they are to be passed via function pointers.
 +
 
==stdlib functions==
 
==stdlib functions==
 
* strchr() vs strrchr() - returns pointer to first and last instance of a specified character, respectively.
 
* strchr() vs strrchr() - returns pointer to first and last instance of a specified character, respectively.

Revision as of 05:40, 20 December 2009

General

  • Copy one file into another:
#include <fstream>
#include <string>

int main(){
     
     //Open file for reading
     std::ifstream in("input.dat");
     
     //Open file for writing
     std::ofstream out("output.dat");
     
     //Temporary buffer for line read from file
     std::string line;
     
     while(getline(in,line)){//getline removes the newline char
          out<<line<<'\n';   // Appending back newline char  
     }
     return 0;
}

stdlib functions

  • strchr() vs strrchr() - returns pointer to first and last instance of a specified character, respectively.

Preprocessor

  • #pragma directives - offer a way for each compiler to offer machine- and operating system-specific features while retaining overall compatibility with the C and C++ languages.
    • Pragmas are machine- or operating system-specific by definition, and are usually different for every compiler.
    • Pragmas can be used in conditional statements, to provide new preprocessor functionality, or to provide implementation-defined information to the compiler.

Polymorphism and Inheritance

  • You must explicitly specify the accessibility of the inherited class when declaring the inheriting class
    • class inheriting : public inherited { ... };
      • NOTE THE PLACEMENT OF PUBLIC ABOVE