Cpp
Published: 13:14, Thursday 14 July 2011
Notes
What's this? See my article about Notes.
View as plain text file.
asdfasdf
hello world:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}
compile: g++
comments: /* */ //
variable initialization:
int a = 0;
int a (0);
constants:
decimal: 75, 75u, 75l, 75ul
octal: 0113
hexa: 0x4b
floating point
3.12, 5.34e223, 1.3e-23, 3.0
3.13L, 6.04e34f
char: 'e'
string
"asd"
of wchar_t
L"asd"
for other character sets
bool: true, false
defined
#define ASD val
preprocessed
declared
const int a = 334
like normal vars, but cannot be modified
operators:
comma operator: a=(b=3,b+2)
int a = sizeof(char)
strings:
#include <string>
string s = "asd";
char s[] = "asd";
char* s = "asd"
input/output/i/o/stdio
iostream:
#include <iostream>
#include <cstdlib>
cout << "hello" << endl;
new line: endl
int age;
cin >> age;
cin >> mystring
stops on blank
getline(cin, mystr)
stringstream:
#include <sstream>
getline(cin,mystr)
stringstream(mystr) >> quantity;
separate obtaining input with interpretation of input
control structures:
if, else, else if, while, do-while, for, break, continue, goto
exit: cstdlib, switch
functions
void
by value: asd (int a, int b)
by reference: asd (int& a, int& b)
overloading with different signature
inline type name (args) { instructions }
prototype declaration: type name (args)
arrays:
type name[elements];
as argument: (int arg[], int arg2[][depth][depth])
special case:
const int n = 1000;
int a[n]
pointers:
reference operator:
address of
pointer = &var
dereference operator
value pointed by
var = *pointer
decl:
int* p;
set directly:
int n
int* p = &n;
means NOT *p = &n, but means p = &n
multiple:
int* p1,* p2 !
difference between pointers and arrays:
arrays are CONSTANT pointers to the first element
a[5] is equivalent to *(a+5)
int n[20];
int* p;
p = n; is valid
n = p; is not
pointer arithmetic/precedence
p++ increases the value of sizeof(typeof(p))
*p++ === *(p++) === *p; p++ because postfix
pointers to pointers
char** c
void pointers
pointers to a value with not type
can point to anything
data can not be dereferenced
null pointer
int* p = 0;
points to nowhere
pointers to functions
rettype (* fp)(argtypes) = fname
rettype res = (* fp)(args)
dynamic memory:
p = new type
p = new type[n]
n doesn't have to be constant here
int* bobby = new int[5];
allocated in heap
exceptions:
default: bad_alloc exc
p = new (nothrow) int[5];
if error just returns a nullpointer
delete pointer
delete[] pointer
in c: malloc, calloc, realloc, free also available <cstdlib>
data structures:
struct sname { mtype1 membername;...} objnames;
sname objnames;
obj.member
pointers to structures:
sname* obj;
obj->member === (*obj).member
*obj.member === *(obj.member)
additional data types:
aliases/synonyms
typedef existingtype newtypename;
unions:
union uname {members}
anonymous unions inline
struct { type el; union {type el1; type2 el2 }}
enumeration:
enum enumeration_name {membe[=startval],member...} objnames;
compatible with integers
classes:
holds data and functions
class cname {access_specifier_1
member1;...}objnames;
note: different syntax than java
access specifiers:
private: same class or friends (default)
protected: same or derived class
public: all
instantiate
Cname v;
Cname v(args);
NOT Cname v();
methods:
can be overloaded (with other args)
Cname::mname (args) {}
scope operator ::
varname directly
constructor:
Cname::Cname (args) {}
java's super: SubCname::SubCname (args) :Cname(args) {}
destructor:
Cname::~Cname() {}
pointers to classes:
Cname* o = new Cname;
o->mname();
struct and union classes
is possible, difference: struct public by default
this: pointer to current object
static members
class variables
static int n
static function
can only refer to static data
no this
operator overloading:
operator function:
member function: CVector CVector::operator+ (CVector param) {}
global function
CVector operator+ (CVector param, CVector param)
disadvantage: cannot access private
friendship
friend function:
class C {...;friend type function (args);}
friend class:
friend class Cname
if cross ref needs empty definition first
not transitive
inheritance:
class Cname
public Pname1, public Pname2, ...
described minimum access level for members inherited
inherits everythin except constr,destr, operator=(), friends
polymorphism:
classes conform to base classes
virtual members/polymorphic class
virtual type mname() {}
can be redefined in lower classes: int mname()
pure virtual function/abstract class:
virtual int mname() =0;
genericity:
function templates:
declare
template <class/type identifier> fdecl;
use the identifier as a type inside
use: fname <type> (params);
multiple: <class id1, class id2,...>
class templates:
typetype: class or typename
template <typetype T> class Cname {
member functions:
template <typetype T> rettype Cname<T>::mname(...) {...}
instantiate:
Cname<type> var;
template specialization:
existing: template <typetype T> class Cname {
specialization
template <> class Cname <type> {
define all its members: no inheritance
non type parameters!
template<typetype T, int N>
defaults
t<int N=10>
use with t<>
compiler: compiled on demand
bound genericity?
namespaces:
define:
namespace identifier {entities}
include:
#include <lib>
using namespace identifier;
use:
using identifier::entity
identifier::entity
or using namespace identifier;
avoid name clashes with {}
aliases:
namespace nname = name;
exceptions:
try {} catch (int e) {}
ellipsis: catch(...)
throw 20;
limit exception type of function:
rtype fname (args) throw (type);
no exc allowed: throw()
all exc allowed: empty
standard:
in namespace std, <exception> header exceptiono
bad_alloc, bad_cast, bad_exception, byd_typeid, ios_base::failure
type casting:
@ The C++ language tutorial, p. 127
sdl:
gui
Comments
Write a Comment
* These fields are mandatory.
Carissimo! decido di postare qui perché sono particolarmente affezionato al caro vecchio C++ :-D
sono contento che tu abbia riattivato il tuo sito... cominciavo a sentirmi solo nel mondo dei Bloggers! Ora copio il feed del tuo sito ;-) I'll stay tuned :-P