//Auteur : Raphael De Oliveira
#include <iostream>
#include <string>
#include <cmath>
#include "nomfille.h"
using namespace std;
int main()
{
cout<<"STATIC CAST : -----------------------------------"<<endl<<endl;
double x = M_PI;
cout<<"(double)x = "<<x<<endl;
cout<<"(int)x = "<<static_cast<int>(x)<<endl;
cout<<"(long double)x = "<<static_cast<long double>(x)<<endl;
cout <<endl<< "DYNAMIC CAST :----------------------------------------------------" << endl<<endl;
NomMere* ptr1 = new NomFille(30, "Juliette", "Du Chesse",50);
NomFille* ptr2 = dynamic_cast<NomFille*>(ptr1); //Dynamic cast
cout << endl << ptr2->NomMere::toString() << endl << endl;
cout << ptr2->toString() << endl;
ptr2->setEntier_private(50);
cout << ptr2->toString() << endl;
cout<<endl<<"REINTERPRET CAST : ---------------------------------"<<endl<<endl;
int j=10;
long long *ptrj = reinterpret_cast<long long*>(j);
long long k = reinterpret_cast<long long>(ptrj);
cout<<"int j="<<j<<endl;
cout<<"long long *ptrj = "<<ptrj<<" (reinterpret_cast<long long*>(j))"<<endl;
cout<<"long long k = "<<k<<" (reinterpret_cast<long long>(ptrj))"<<endl;
cout<<endl<<"CONST CAST : ---------------------------------"<<endl<<endl;
int i=4;
cout<<"int i = "<<i<<endl;
cout<<"static cast const *char : "<<const_cast<char*>("troto")<<endl;
const int *p = &i;
cout<<"const int *p = "<<*p<<" (&i)"<<endl;
int *l = const_cast<int*>(p);
cout<<"int *l = "<<*l<<" (const_cast<int*>(p))"<<endl;
//Libérer la mémoire allouée
//delete ptr1;
delete ptr2;
return 0;
}
//-------------------------------------------------------------------------------------------------------------------
#ifndef NOMFILLE_H
#define NOMFILLE_H
#include "nommere.h"
#include <string>
using namespace std;
class NomFille : public NomMere
{
public:
NomFille(int , string , string , int);
int depot_bancaire;
int entier_public;
void setEntier_private(int);
int getEntier_private();
string toString() override;
void fonction() override;
private:
int entier_prive;
};
#endif // NOMFILLE_H
//-------------------------------------------------------------------------------------------------------------------
#ifndef NOMMERE_H
#define NOMMERE_H
#include <string>
using namespace std;
class NomMere
{
public:
int age;
string prenom;
string nom;
NomMere(int,string,string);
virtual string toString()=0;
virtual void fonction()=0;
};
#endif // NOMMERE_H
//-------------------------------------------------------------------------------------------------------------------
#include "nomfille.h"
#include <iostream>
#include <string>
using namespace std;
NomFille::NomFille(int a, string p, string n, int argent) : NomMere(a,p,n)
{
cout<<"Je suis dans le constructeur de la classe fille"<<endl;
this->depot_bancaire=argent;
this->entier_public=1;
this->entier_prive=2;
}
void NomFille::setEntier_private(int x){
this->entier_prive = x;
cout<<endl<<"l'entier privé de la classe a été modifié"<<endl<<endl;
}
int NomFille::getEntier_private(){
return this->entier_prive;
}
string NomFille::toString(){
return "toString() de la classe fille : Entier public vaut : " + to_string(this->entier_public) + ", Entier prive vaut : " + to_string(this->entier_prive) + ", depot bancaire vaut : " + to_string(this->depot_bancaire);
}
void NomFille::fonction(){
cout<<"je suis dans fonction"<<endl;
}
//-------------------------------------------------------------------------------------------------------------------
#include "nommere.h"
#include <string>
#include <iostream>
using namespace std;
NomMere::NomMere(int a, string p, string n)
{
cout<<"Je suis dans le constructeur de la classe mere"<<endl;
this->age=a;
this->nom=n;
this->prenom=p;
}
string NomMere::toString(){
string result="toString() de la classe mere :\nLa personne s'appelle : ";
result += nom +" "+prenom+" ";
return result;
}
Add a code snippet to your website: www.paste.org