design problem - inheritance with static variables
I have the following hierarchy:
Graduate.cpp (abstract)
College.cpp (abstract)
Ecollege.cpp
University.cpp (abstract)
Tuniversity.cpp
Huniversity.cpp
class Huniversity for example represents a student that graduated from H university. each non-abstract class has to implement the following method which is defined in Graduate.h:
virtual double CalcGraduateAvg() = 0;
Tuniversity has use info from Huniversity. this info is stored it static variables. this is for example Tuniversity header and implementation:
#include "University.h"
#define AVG_FACTOR 5
class Tuniversity : public University {
public:
Tuniversity(int id, char identifier, double salary, double grades);
virtual double CalcGraduateAvg();
protected:
static int _numTGrads;
static double _sumTGrades;
};
#include "Tuniversity.h"
#include "Huniversity.h"
//initialize static variables:
int Tuniversity::_numTGrads = 0;
double Tuniversity::_sumTGrades = 0;
Tuniversity::Tuniversity(int id, char identifier, double salary, double grades)
: University(id,identifier, salary, grades) {
_numTGrads++;
_sumTGrades += grades;
}
double Tuniversity::CalcGraduateAvg() {
//HERE I CAN'T access sumHGrades and numHGrades since it's private
double A = Huniversity::_sumHGrades / Huniversity::_numHGrads;
double T = _sumTGrades / _numTGrads;
_normalizedGradesAvg = A / T * _gradesAvg + AVG_FACTOR;
return _normalizedGradesAvg;
}
My design problem is how Tuniv' reads data from Huniv' (the two static variables). Besides the fact that both clas开发者_如何学JAVAs inherit University there is not connection between them. I don't want to make these variables public since its wrong. I don't want to make a none static virtual function GetAvg() since it will force me to create and object in order to get the data. I tried to make virtual static function but it's not possible. Do you think there is a good solution to this problem (I rather not implement a static function GetAvg() in Tuni' and Huniv which will return the data because if i expand my program, for instance add Muniv' there is no interface that forces the class to implement that GetAvg() function, so i consider that option a bad design). As far as i see there is no good solution to this problem. Using the 2 static variables in each class is my exercise requirement and i wish to consult with you before i ask my TA to change that requirement.
Thanks you all!
class Huniversity for example represents a student that graduated from H university
So why do you call this class Huniversity
and not Hstudent
?
I think you're mixing university and student and put them into the same class (because you also have static members representing per-university information in a class which you say represents a student). The universities could contain a vector of (pointers to) student objects where each student object contains its grade.
I would introduce separate classes for students and make the static members for number of students and sum of grades non-static and add the method GetAvg()
.
You say that there is no connection between TUniv
and HUniv
. You're wrong. If the average of TUniv
is dtermined in terms of HUniv
, that's a connection.
And that connection should be modeled somehow. If this kind of connection occurs between only these two university types, you can easily solve it by using a friend
declaration. But maybe the classes need a more extensive interface.
Since C++ has no reflection, it may be a good idea to set up an object used to represent the graduate's university. That, by the way, reflects the real world better, too: a graduate actually graduates at an institution.
If you do that, the institution (an object) can keep references to it's graduates. The different metrics can be defined as member functions on the university objects, and the metric function for the TUniv
object will require an explicit reference (argument) to it's HUniv
connection.
精彩评论