*******CLASSES*******
<iostream>
cin, cout, flush, '\a', '\b', '\t', '\n', endl, >> (search ignoring whitespace, fetch until unmatched.), <<
<iomanip>
setw(2), setprecision(16), setiosflags(ios::left/right/fixed), resetiosflags(ios::left/right/fixed)
<fstream>
ofstream ofsname("filename"), ofsname<<"stream to write", ifstream ifsname("filename"), getline(ifsname,linestringread)
, eof(), is_open();
<cmath>
fabs(double d), sqrt(double d)
<ctime>
time_t, time(NULL) => time_t, ctime(&t) => string formatted time, difftime(t1, t2) => integer seconds,
<string>
st.find('\n') => index, string::npos, st.substr(0,5) => string, stringstream
<exception>
class exception, exception.what()
*******DATA TYPES*******
int, float, long, double, char '\0', unsigned int
array type arg[] = {,}; constatnt pointer of type.
#define arg val
const type arg = val;
pointer: type * ref; address of (the first element that it points to): & arg; value pointed by: * ref; ref[2] == *(ref+2)
dynamic allocation: type * pt; pt = new (nothrow) type [int]; delete [] pt; <cstdlib>: malloc, calloc, realloc, free;
call by reference fucntion(type & arg)
function pointer:
int function(int a, int b) {return a*b;}
int call_function(int x, int y, int (*funcp)(int,int)) { return (*funcp)(x,y); }
int test(){ int (*funcp) (int, int); funcp = function; return call_function(3,5,funcp); } //or
int test(){ int (*funcp) (int, int); funcp = function; return (*funcp)(3,5); }
macro: #define macroname(variable) macroscript #variable as label, (variable) as value
struct typename{membertype1 member1;...} objectname1,...;
typename * ptype; ptype->member == (*ptype).member
typedef variable_declaration; variable_declaration == int alias_name [20]; alias_name arg; =>> arg is an int[20];
union union_name{ long l; struct{short hi;short lo}s; char c[4]; } union_object_name; usage: union_object_name.l, .s.hi, .s.lo, .c[0]
anonymous union: struct movie {string title; uion{long twdollar; double usdollar;};}; =>> usage: movie.twdollar;
enumeration enum enum_name{value1,value2}enum_object;
namespace
declaration:
namespace spacename{functions, variables}
usage:
using spacename::elementname; elementname = elementvalue;
using namespace spacename; elementname = elementvalue;
class:
class class_name {specifier1: datatype1 data1; specifier2: datatype2 function2 (paratype); class_name (paratype);};
datatype2 class_name::function2 (paratype para){}
class_name::classname(paratype para){data1 = para;}
see also: classes.cpp for deep copy which involves constructor, copy constructor, default constructor, destructor;
static member, firend function, friend class, this;
a friend of one class can access directly private and protected members of that class.
static function can only access static member or global member.
static member has global scope
operator overloading: class classname{public: datatypeR operator + (datatypeI);}; datatypeR classname::operator+ (datatypeI param){}
inheritance
declaration:
class child : access_right1 mother1, access_right2 mother2 {}; access_right is the maximum exposure level.
access_right: public < protected < private
child's minimum access: protected
constructor inheritance:
child (type para) : mother (para){}
polymorphism:
mother *pm = & child; pm->mother_public_members();
virtual member:
declaration:
virtual dataTypeRe virtFunc (dataTypeIn param) { implementations;};
virtual dataTypeRe virtFunc (dataTypeIn) = 0; => pure virtual function
abstract class: a class containing pure virtual function
declare destructor of extended class as virtual.
pure virtual function can be called within base class function.
mother_class *pm = new child_class();
virtual member in mother for child to override and used in polymorphism
to call child specific function from mother pointer.
template:
function template
template <class T1, class T2, class R>
R function (T1 t1, T2 t2){ R r; r = t1+t2; return r;}
class template
template <class T, int N = 10>
class ClassName {T funcName (T);};
template <class T>
T ClassName<T>::funcName(T t){return t+t;}
int t;
ClassName<int> classObj;
classObj.funcName(t);
class template specialization
template<>
class ClassName<int, int N = 10>{int funcName(int);};
template <>
int ClassName<int,int N = 10>::funcname(int t){return t*t;}
exception:
try{try{throw object1;}catch(){throw; } throw object2;}catch(typeObject1 o1){}catch(...){}
function exception:
typeR funcName(typeI param) throw (typeThr){} // can only throw typeThr in {} otherwise not catchable.
typeR funcName(typeI param) throw () {} // cannot throw anything in {}
typeR funcName(typeI param) {} // anything can be thrown in {}
exception class:
delcare:
#include <exception>
using namespace std;
class MyExcept : public exception {
virtual const Char * what() const throw (){ return "MyExcept happened."; };
};
usage:
MyExcept myExcept;
try{throw myExcept;}catch(exception & e){ cout << e.what() << endl;}
standard library exceptions:
bad_alloc, bad_typeid, bad_cast, bad_exception, ios_base::failure
typecasting:
object cast:
operator type cast
type1 obj1; type2 obj2; obj2 = (type2) obj1; // omit "(type2)" as implicit typecasting which may produce warning.
constructor type cast
type1 obj1; type2 obj2 = type2 (obj1); // omit type2() as implicit typecasting which may produce warning.
pointer cast:
class1 obj1; class2 *pobj2; pobj2 = (class2 *) &obj1;
dynamic_cast, reinterpret_cast, static_cast, const_cast <new_type> (expression)
dynamic_cast: child pointer to mother pointer. abstract mother to child with null pointer casted. other case: throw bad_cast
check for cast completeness between pointers.
casting not possible: throw bad_cast; casting not complete: return null pointer.
require RTTI compiler support
static_cast: no RTTI check. cast between child pointer and mother pointer. implicit cast.
reinterpret_cast: pointer between any type. implicit cast. simply copy pointer address value.
const_cast: set or remove constness of a pointer.
typeid:
#include <typeinfo>
typeid(object_pointer) is a typeinfo.
typeid(object_pointer).name() is the name of the type id of the object_pointer.
typeid(*object_pointer).name() is the name of the type id of the derived object of the object_pointer.
if *object_pointer is null, typeid throws bad_typeid exception.
preprocessor:
directives are digested by the preprocessor before any statement is compiled.
format:
begin with '#', end with newline, continue with backslash '\'
usage:
# derictives here \
# continue the derictives
#define, #undefine
usage:
#define identifier(arg1,arg2,arg3) replacement (arg1) #(arg2) ##(arg3)
(arg1) == value of arg1, #(arg2) == "arg2", ##(arg3) == concatenate "arg3"
#undefine identifier
#ifdef, #ifndef, #endif
usage:
#ifdef \\ or #ifndef (i.e. if not defined by #define)
statements here
#endif
#if, #elseif, #else, #endif
usage:
#if macro expressions
statements here
#elseif macro expressions
statements here
#else
statements here
#endif
macro expressions: >, <, defined, !defined
#line
usage:
#line line_number "file_name"
meaning:
define line number to be line_number, which is increased by one from here on.
define source code file name to be "file_name"
#error
usage:
#error error message here
meaning:
if found, print error message and abort compilation.
#include
usage1:
#include "filename"
meaning:
replace this line by the entire content of that file
that file is searched first in the current directory of the includer file, then standard directories
usage2:
#include <libname>
meaning:
same as usage 1 except that current directory is skipped.
#pragma
meaning:
specify compiler options which is compiler and platform dependent
predefined macros:
__LINE__, __FILE__, __DATE__, __TIME__, __cplusplus
current line, current sorce file, compilation date, time, compiler version long value.
fileio:
usage:
#include <fstream>
fstream myfile ("filename.txt", ios::flags);
string mystring = "mystring here.";
myfile << mystring; // output to myfile
myfile >> mystring; // input from myfile
ios::flags:
ios::in | ios::out | ios::ate | ios::app | ios::trunc | ios::binary
eof append truncate
fstream, ofstream, ifstream default flags:
ios::in|ios::out, ios::out, ios::in
.eof(), .is_open()
state check:
bad(), fail(), eof(), good()
io failed, io failed including format mismatch, end of file, non of the previous three is true.
each report its state flag.
state clear:
clear() state flags of the previous four state flags.
get and put pointers:
tellg(), tellp() : returns pos_type (which is a long) of the get, put pointer.
seekg(pos_type), seekp(pos_type) : set the get, put pointer to pos_type
seekg(off_type, seekdir), seekp(off_type, seekdir) : set the ... to seekdir plus off_type,
where off_type is long, and
seekdir is enumerated long including ios::beg, ios::cur, ios::end
compiler functions:
stat(), fstat()
file number, file number of a file stream // to be studied.
buffer sync:
conditions:
1. sync() called.
2. manipulator called: flush, endl
3. file closed
4. buffer is full
inline
1e-2
limits.h
*******CONTROLS*******
for(){}
if(){} else if() {}
switch(var){case:arg1 {break;} case: arg2 {break;}}
do{}while();
沒有留言:
張貼留言