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

Discussion in 'C++' started by stephane, Feb 14, 2005.

  1. stephane

    stephane Guest

    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::eek:perator+(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;

    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
     
    stephane, Feb 14, 2005
    #1
    1. Advertising

  2. 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::eek:perator+(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
     
    Chris Jefferson, Feb 15, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bienwell
    Replies:
    4
    Views:
    3,910
    bienwell
    May 27, 2005
  2. Michael Kantarovich

    Very strange try/catch problem. Please help!

    Michael Kantarovich, Sep 10, 2003, in forum: C++
    Replies:
    3
    Views:
    334
    Michael Kantarovich
    Sep 15, 2003
  3. John Salerno
    Replies:
    20
    Views:
    887
    John Salerno
    Aug 11, 2006
  4. Fabio Z Tessitore

    who is simpler? try/except/else or try/except

    Fabio Z Tessitore, Aug 12, 2007, in forum: Python
    Replies:
    5
    Views:
    399
  5. =?Utf-8?B?QUo=?=

    When to TRY and not to TRY

    =?Utf-8?B?QUo=?=, Oct 16, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    346
    sloan
    Oct 17, 2007
Loading...

Share This Page