Beruflich Dokumente
Kultur Dokumente
Hello World !
#include <iostream>
/* Commentaire
sur plusieurs lignes */
int main()
{
//Affiche hello world
std::cout << "Hello World !
<< std:endl;
}
Nouveauts de syntaxe en
C++
Surcharge de fonction
void f(int i) {}
int f(double i) {}
void f(int a,int b) {}
// ERREUR :
// Diffre uniquement par
// le type de retour
double f(int i) {}
Oprateurs
Mathmatiques : +, -, *, /, %, ++, -Logiques : &&, ||, !, ==, !=, <, >, <=, >=
Donnes : *, &, ->
Binaire : &, |, ^, ~, <<, >>
Assignation : =, *=, /=, %=, +=, -=, <<=, >>=,
&=, |=, ^=
Tous surchargeables.
Rfrences
Rfrence
void Double(int x)
{
x*=2;
}
int a=2;
Double(a); //a==2
void Double(int* x)
{ *x*=2; }
Double(&a); //a==4
void Double(int& x)
{ x*=2; }
Double(a); //a==4
Mthodes :
Attaqu par
Mange
Code
(.cpp)
Objet (.o)
Link
Compilation
Executable
Header
(.h)
Code
(.cpp)
Objet (.o)
Les erreurs
Compilation
Linkage
Syntaxe (erreurs)
Warnings
Fonctions et variables dclares, non
implmentes
Excution
Segfault
Joueur.cpp :
void CJoueur::AttaqueDe(CJoueur& J)
class CJoueur
{
{
Vie -= J.Vie;
public:
EnvieManger += 10;
void AttaqueDe(CJoueur& J);
Mort = ((Vie<=0)||
void Mange();
(EnvieManger>=100));
int Vie, EnvieManger;
}
bool Mort;
};
void CJoueur::Mange()
{ EnvieManger=0; }
Pr compilateur
Joueur.h :
#ifndef JOUEUR_H
#define JOUEUR_H
Class CJoueur
{/* */ };
#endif
Joueur.cpp :
#include Joueur.h
void CJoueur::AttaqueDe(CJoueur& J)
Utilisation de lobjet
CJoueur J;
J.Mort=false;
J.Vie=100;
J.Mange();
CJoueur* p=&J;
(*p).Vie=100;
p->Vie=100;
p->Mange();
Constructeur destructeur
Constructeur destructeur
class CJoueur
{
public:
CJoueur()
{
Vie=100; EnvieManger=0;
Mort=false;
}
~CJoueur() { }
};
Drivation
Drivation
class CJoueurZerg
: public CJoueur
{
public:
void SayGrrrgg();
};
CJoueurZerg Zergling;
CJoueur J;
Zergling.Mange();
J.AttaqueDe(Zergling);
Zergling.SayGrrrgg();
CJoueur* p=&Zergling;
p->SayGrrrgg(); //Error
Fonctions virtuelles
CJoueurHumain, CJoueurProtoss et
CJoueurZerg ne mangent pas de la mme
faon. Pourtant on veut que tout CJoueur
puisse manger.
Fonctions virtuelles
class CJoueur
{
public:
virtual void Mange()
{
EnvieManger=0;
}
};
class CJoueurZerg
:public CJoueur
{
public:
void Mange()
{
SayGrrrgg();
CJoueur::Mange();
}
};
Fonctions virtuelles
CJoueurZerg Over;
CJoueur J;
CJoueur* P=&Over;
J.Mange();
Over.Mange();
P->Mange();
CJoueurZerg Hydra;
Hydra.Mange();
CJoueur J; //Erreur
CJoueur* P=&Hydra;
P->Mange(); //OK
CJoueur J; //Erreur
CJoueurHumain J; //Erreur
class CCapitaine
:public CJoueurHumain
{
public:
void Mange()
{ /* */ }
};
CCapitaine Capitaine;
CJoueur* P=&Capitaine;
P->Mange(); //OK
class CMatrix {
public:
CMatrix() {}
CMatrix(const CMatrix& m);
double& operator()(int i,int j) {return Datas[i][j];}
CMatrix& operator=(CMatrix& m);
CMatrix& operator*=(double d);
CMatrix& operator+=(CNmatrix& m);
CMatrix operator+(CMatrix& m);
CMatrix operator-();
friend CMatrix operator*(double r,CMatrix& m);
private:
double Datas[3][3];
};
CMatrix& CMatrix::operator*=(double d)
{
for(int i=0;i<3;++i)
for(int j=0;j<3;++j)
(*this)(i,j)*=d;
return *this;
}
CMatrix CMatrix::operator*(double r,CMatrix& m)
{
CNmatrix R(m);
return R*=r;
}
CMatrix& CMatrix::operator+=(CNmatrix& m)
{
for(int i=0;i<3;++i)
for(int j=0;j<3;++j)
(*this)(i,j)+=m(i,j);
return *this;
}
CMatrix CMatrix::operator+(CMatrix& m)
{
CMatrix R(*this);
return R+=m;
}
CMatrix CMatrix::operator-() { return -1*(*this); }
La STL
std::string (<string>)
Chane de caractres
std::string MonNom=Vincent;
MonNom.size()==7;
MonNom+= Lascaux;
MonNom==Vincent Lascaux;
MonNom[0]==V; MonNom[3]==c;
std::vector (<vector>)
std::vector<int> Tableau;
Tableau.resize(20);
Tableau[0]=12; Tableau[1]=42;
for(int i=2;i<20;i++) Tableau[i]=i*i;
Tableau.reserve(40);
for(int i=0;i<20;++i)
Tableau.push_back(12);
std::list (<list>)
std::list<std::string> Liste;
Liste.push_back(CNedra);
Liste.push_back(rulez);
Liste.push_front(See how );
//Liste = [See how, CNedra, rulez]
Itrateurs
Itrateurs
int Somme=0;
for(std::vector<int>::iterator it=Tableau.begin(); it!
=Tableau.end();it++)
Somme+=*it;
int Somme=0;
for(std::list<int>::iterator it=Liste.begin(); it!=Liste.end();it++)
Somme+=*it;
Dcriture du code
Du code
Tutoriaux
Thinking in C++
STL : aide de tout bon compilo,
tutoriaux sur le net, MSDN
Un peu de C
Les variables
int a;
int a,b;
int a=2;
int a=2, b=3;
int Puissance4(int x)
{
int carre;
carre=x*x;
return carre*carre;
}
Quelques exemples
#include <iostream>
int main()
{
std::cout << Hello World\n;
return 0;
}
Types entiers :
char, int, short, long
//Retourne le double de x
int Double(int x)
{
return 2*x;
}
Types rels :
float, double
Pointeurs
Un type de variable stockant ladresse dune
zone en mmoire
Une valeur particulire : NULL
int a;
int* p;
p=&a;
*p=2; //a==2
a=3; //*p==3
Contrle du flux
Condition
if else
Boucles do-while, while
Boucle for
Condition
Le type dune condition est bool, il peut prendre les
valeurs true ou false. Les nombres entiers
peuvent tre teste :
0 false, autre nombre true
<, <=, >, >=, ==, !=
&&, ||, !
A==3 && B==2 A==((3 && B)==2)
(A==3) && (B==2)
if - else
int sign(int x)
{
if(x<0)
{
return -1;
} else {
return 1;
}
}
int sign(int x)
{
if(x<0) return -1;
else return 1;
}
Boucle for
int i;
for(i=0;i<10;i++)
{
cout << 10 fois;
}
for(int i=0;i<10;i++)
cout << 10 fois;
Transtypage (cast)
Changer le type dune expression
double a; (int)a;
int a; (double)a;
Transtypage implicite
double Carre(double a) {return a*a;}
Carre(2); //Carre((double)2), Carre(2.)
const