help please / object programming

S

stephane

I am preparing an exam and I have a copy of last year exam.

I answered the first questions and I wander if I am right or wrong. Can
someone have a look at the questions and at my answers please?

There is no code to write just general questions about the two class'.

the code:

/--



# 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 :



//attributs obligatoires



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



//attributs optionnels



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



The questions:



1) Does the method Thoraire::chargeHoraire() have the right to access the
member m_duree of the class Tcase?



2) Does the method Tcase::printLn() have the right to access the member
m_heureDebut of the class Tcase?



3) What are the exceptions that can be thrown by the method Thoraire
Thoraire::eek:perator+(const Thoraire& hor) ?



4) What are the exceptions that can be thrown by the method double
Thoraire::chargeHoraire()?



5) What's the technical reason that forces the developper of the class
Thoraire to declare an array of Tcase pointers as member of the class
instead of an array of Tcase?



Answers according to me:



1) No because it's trying to access the private part of another class.



2) Yes because the method belongs to the same class.



3) when "enseignant" is not matching and when the name of "enseignant" is
not the same.



4) none ? ( I am not sure of this at all)



5) to avoid creating an object which would be infinite. Because it would be
a recursive creation of the objet. meaning an object created in an object
that crate another object and on and on.



thanks for your help!
 
M

Matthias

stephane said:
1) Does the method Thoraire::chargeHoraire() have the right to access the
member m_duree of the class Tcase?

Not directly, but you can get its value by calling the getter. I guess
your prof will probably want to hear what you said: No, because it's a
private member and an implementation detail.
2) Does the method Tcase::printLn() have the right to access the member
m_heureDebut of the class Tcase?

Sure. They both belong to the same class.
3) What are the exceptions that can be thrown by the method Thoraire
Thoraire::eek:perator+(const Thoraire& hor) ?

TenseignantError and Toverflow. If it throws an exception which is not
in this list, your program will call unexpected() and terminate (if you
don't override this handler and do something more appropriate).
4) What are the exceptions that can be thrown by the method double
Thoraire::chargeHoraire()?

All exceptions imaginable. If it wouldn't be allowed to throw any
exceptions, it would have to be declared using 'throw ()'. If it is
declared without specifying which exceptions it will throw, it may throw
any kind of exception.
5) What's the technical reason that forces the developper of the class
Thoraire to declare an array of Tcase pointers as member of the class
instead of an array of Tcase?

I can't see immediately why this is needed. Maybe someone else can
answer this. I also read your answer, but I'm not sure what you mean
with recursive creation. It's just an array of objects. Nothing
recursive there.
 
S

shez

Matthias said:
I can't see immediately why this is needed. Maybe someone else can
answer this. I also read your answer, but I'm not sure what you mean
with recursive creation. It's just an array of objects. Nothing
recursive there.
Probably because it doesn't have a default constructor?

-shez-
 
M

Matthias

Stephane said:
What would have been the difference if it had a default constructor?

The problem is, if it doesn't, how should the compiler know how to
construct e.g. 10 Tcase objects in an array?

Tcase array[10];

It is obvious here that the default constructor of the 10 Tcase objects
has to be called. However, you didn't define one. In other words,
you have no chance here to pass the arguments to the constructor defined
by Tcase. That is illegal, you can't just omit them. However, if you
hold Tcase pointers instead:

Tcase* array[10];

In this case you *don't* directly construct 10 Tcase object, but are
only storing pointers to possible Tcase objects. Now you can do this in
some init function of your program:

for( int i=0; i<10; ++i )
{
array = new Tcase(a,b,c,...); // call ctor with mandatory args
}
 
O

osmium

Stephane Vollet said:
What would have been the difference if it had a default constructor?

One of the rules is that there must be a default constructor to create an
array of objects. If you don't like the initializing that results, use a
function that initializes each object to your taste. You would probably
call the initializer with a for loop.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top