C++

From Colettapedia
Revision as of 17:50, 21 December 2009 by Ccoletta (talk | contribs)
Jump to navigation Jump to search

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

Function Pointers

  1. Define a function pointer and initialize to NULL
    1. int (*pt2Function)(float, char, char) = NULL;
      • C-style
    • int (TMyClass::*pt2Member)(float, char, char) = NULL;
      • C++-style
    • int (TMyClass::*pt2ConstMember)(float, char, char) const = NULL;
      • C++-style, constant