By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,412 Members | 1,007 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,412 IT Pros & Developers. It's quick & easy.

method operator+ (try catch)/* Help needed*/

P: n/a
Hi again, I would like to make sure I am right (if I am). This is the last
time I'll bother you with this code I promise! It's just that I don't have a
teacher and I have to prepare an exam. I would really be grateful if someone
could have a quick look at it!
I had to implement the surcharge of the operator+ and its utilization too.
Thoraire Thoraire::operator+(const Thoraire& hor)const throw
(TenseignantError,Toverflow)
{
if(hor.m_nomEnseignant != m_nomEnseignant)

throw TenseignantError();

for(int i=0;i<hor.m_nbCases;i++)
{
if(m_nbCases>CcasesMax)
throw Toverflow();

m_cases[m_nbCases] = hor.m_cases[i];

m_nbCases++;
}
return *this;
}

// in the main file
Thoraire horaireSemestre1("Lambert");

Thoraire horaireSemestre2("Lambert");

Thoraire horaireAnnuel("Lambert");

try{
horaireAnnuel = horaireSemestre1 + horaireSemestre2;
}

catch (Toverflow){

cout<<"depassement de capacity de l'horaire"<<endl;
}

catch(TenseignantError){

cout<<"le nom de l'enseignant ne correspond pas"<<endl;
}
....
Here are my tow classes

/--

# ifndef CASE_H
# define CASE_H
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std ;
const int Clundi = 1 ;
const int Cmardi = 2 ;
const int Cmercredi = 3 ;
const int Cjeudi = 4 ;
const int Cvendredi = 5 ;
const int Csamedi = 6 ;
const int Cdimanche = 7 ;
class Tcase{
public :
Tcase (int numSemaine, int numJour, double heureDebut, double duree,char
matiere[], char classe[]= " ", char salle[]= " ") ;
int getNumSemaine() const ;
double getDuree() const ;
void printLn() const ;
private :
int m_numSemaine ; //1..53
int m_numJour ; //1..7
double m_heureDebut ; //13h30 = 13.5
double m_duree ; //2h45 = 2.75
char m_matiere[10] ; //p ex. prog7
char m_classe[10] ; //p. ex. 3IG-EE
char m_salle[10] ; //p. ex A46A
}
#endif

# ifndef HORAIRE_H
# define HORAIRE_H
# include <iostream>
# include <cstring>
using namespace std ;
#include case.h
class TenseignantError{} ; // the teacher is not matching
class Toverflow{} ; //capacity overflow
const int CcasesMax = 1000 ;
class Thoraire{
public :
//create a new hours schedule
Thoraire (const char nomEnseignant[]) ;
//add a case in the hours schedule
void ajouteCase (Tcase* caseHor) throw (Toverflow) ;
//calculate the total hours
double chargeHoraire() const ;
//return a sample of the schedule
//return an empty schedule if the number of the week doesn't
exist
Thoraire horaireHebdomadaire (int numSem) const ;
/*
Create a new hours schedule by fusionning both schedule, throw an exception
of class TenseignantError when the schedules don't have the same teacher
(enseignant) name.
Throw an exception of class Toverflow when the fusion create an overflow of
capacity (>CcasesMax)
*/
Thoraire operator+ (const Thoraire & hor) const throw (TenseignantError,
Toverflow) ;
Void printLn() ;
Private :
Char m_nomEnseignant [30] ; // obligatory
Tcase* m_cases[CcasesMax] ;
Int m_nbCases ;
} ;
#endif


Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
stephane wrote:
Hi again, I would like to make sure I am right (if I am). This is the last
time I'll bother you with this code I promise! It's just that I don't have a
teacher and I have to prepare an exam. I would really be grateful if someone
could have a quick look at it!
I had to implement the surcharge of the operator+ and its utilization too.
Thoraire Thoraire::operator+(const Thoraire& hor)const throw
(TenseignantError,Toverflow) This operator shouldn't be adding hor to *this, it should be making a
new object which is the result of adding how and *this, and returning
that. I think what you've implemented here is operator+=. While you
don't have to, a guide I've found is the best to follow in almost all
situations is "do the same as int".

Also while you can give throw specifications to functions, most people
(even experts) don't bother. If however your teacher expects them, you
might have to put them in...

{
if(hor.m_nomEnseignant != m_nomEnseignant)

throw TenseignantError();

for(int i=0;i<hor.m_nbCases;i++)
{
if(m_nbCases>CcasesMax)
throw Toverflow();

You could move this throw outside of the loop perhaps, and check it
before starting? This would have the advantage of not leaving the object
in a broken state when you threw.
Apart from that it appears reasonable to me :)

Chris
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.