473,404 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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

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
1 1093
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Chris S. | last post by:
Is there a purpose for using trailing and leading double underscores for built-in method names? My impression was that underscores are supposed to imply some sort of pseudo-privatization, but would...
4
by: daniel.w.gelder | last post by:
I wrote a template class that takes a function prototype and lets you store and call a C-level function, like this: inline string SampleFunction(int, bool) {..} functor<string (int, bool)>...
0
by: Roman | last post by:
I'm trying to create the form which would allow data entry to the Client table, as well as modification and deletion of existing data rows. For some reason the DataGrid part of functionality stops...
12
by: ThyRock | last post by:
I am working on a WebRequest accessing the US Postal Service WebTools test API. This service uses a DLL file (ShippingAPITest.dll) with a query string which includes XML. The web service accepts...
6
by: TuxC0d3 | last post by:
Hi! I'm diving into the some more ++ specific aspects of c++ (and finally accepting that c++ is more than "a plus added to c" :), so that means using namespaces, templates, std::strings, lists,...
6
by: Lighter | last post by:
Big Problem! How to overload operator delete? According to C++ standard, "A deallocation function can have more than one parameter."(see 3.7.3.2); however, I don't know how to use an overloaded...
2
by: bharath.donnipad | last post by:
Hi All, I was going through a c++ manual in msdn site "http://msdn2.microsoft.com/en-us/library/kewsb8ba.aspx". There there was a small note on usage of new operator : "When new is used to...
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.