Hi. 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 class 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!