Année académique 2023-2024
Classe : L2IT
Date : 18/12/2023
Enseignant : M. LOYA
TP n°4 de C++ : CORRECTION
Exercice 1
Question 1
//le fichier de déclaration employe.h
#ifndef EMPLOYE_H
#define EMPLOYE_H
#define SALFIXE 10000.
#include <string>
class Employe
{ std :: string nom,matricule;
int indice_salarial;
static double taux_sal;//donnée membre statique
public: Employe(std :: string n, std :: string m, int ind);
Employe():nom(""),matricule(""),indice_salarial(0){}
double salaire()const;
void afficher()const;
static void setTaux(double t);
static double gettaux();
void setNom(std :: string n){nom=n;}
std ::string getNom()const{return nom;}
std ::string getmatricule()const
{
return matricule;
}
~Employe();
};
#endif // EMPLOYE_H
//le fichier de définition employe.cpp
#include "Employe.h"
#include <iostream>
using namespace std;
double Employe::taux_sal =SALFIXE;
Employe::Employe(string n, string m, int ind)
{ nom= n;
matricule=m;
indice_salarial=ind;
}
double Employe:: salaire() const
{
cout<<"classe Employ\x82"<<endl;
return (taux_sal*indice_salarial);
}
void Employe::afficher() const
{
cout<<"Matricule:"<<matricule<<endl;
cout<<"Nom:"<<nom<<endl;
cout<<"Indice:"<<indice_salarial<<endl;
}
void Employe::setTaux(double t)
{
if(t>0)
taux_sal=t;
}
double Employe::gettaux()
{
return taux_sal;
}
Employe::~Employe(){}
Question 2
//le fichier de déclaration responsable.h
#ifndef RESPONSABLE_H
#define RESPONSABLE_H
#include "Employe.h"
#include<vector>
class Responsable: public Employe
{
public: Responsable();
Responsable(string n, string m, int ind, Employe *subordonnes=nullptr, int nb=0);
void ajouterSub(const Employe& empl);
void supprimerSub();
size_t nbSub()const{return subordonnes.size();}
void afficher()const;// Afficher les informations dun responsable
void afficherSub()const;// afficher les subordonnées directs du responsable
private:
vector<Employe> subordonnes;
};
#endif // RESPONSABLE_H
//le fichier de définition responsable.cpp
include "responsable.h"
#include <iostream>
using namespace std;
Responsable::Responsable(){}
Responsable::Responsable(string n, string m, int ind, Employe *sub,int nb):Employe(n,m,ind){
if(sub!=nullptr) subordonnes.insert(subordonnes.begin(),sub,sub+nb);
}
void Responsable::ajouterSub(const Employe &empl){ subordonnes.push_back(empl);
}
void Responsable::supprimerSub(){ subordonnes.pop_back();
}
void Responsable::afficher()const{ this->Employe::afficher();
this->afficherSub();
}
void Responsable::afficherSub()const{
for(auto it:subordonnes)
it.afficher();
}
Question 3
//le fichier de déclaration commercial.h
#ifndef COMMERCIAL_H
#define COMMERCIAL_H
#include "Employe.h"
class commercial:public Employe
{ double _vente;
static float taux;
public:
void maj_vente(double vente);
void raz();
double salaire()const;
commercial(string n, string m, int ind);
};
#endif // COMMERCIAL_H
//le fichier de définition commercial.h
#include "Commercial.h"
#include <iostream>
using namespace std;
float commercial::taux=0.02f;
commercial::commercial(string n,string m, int ind):Employe(n, m, ind){
_vente=0;
}
void commercial::maj_vente(double vente){
if(vente>0) {
_vente+=vente;
}
}
void commercial::raz(){
_vente=0;
}
double commercial::salaire()const{
cout<<"classe Commercial"<<endl;
return Employe::salaire()+_vente*double(taux);
}
3.3