// Electrical quantities classes // ----------------------------- #ifndef EPOWER // These classes define the real numeric quantities in electric circuits // and the operations on meaningful combinations of them. (Naive version: // no local macros.) // Interactions among the classes: // ------------------------------ // Forward class definitions to allow any of the related classes to // declare function parameters or return values of any of the others: class ECurrent; class EPower; class EPotential; class EResistance; // Binary operator functions: (all are inline) // 1. Both operands of the same class: They are defined as members of that // class, and are usually defined inline within the class definition. // 2. Two operands of different classes: They are members of the class // of the left operand, but are defined outside the class definition // in order to avoid forward references to undefined classes. // The output stream operators are defined as non-member friends, but coded // inline within the class definition. #define EPOTENTIAL const EPotential #define ECURRENT const ECurrent #define ERESISTANCE const EResistance #define EPOWER const EPower // Voltage (potential difference) class // ------------------------------------ class EPotential { double value; public: friend ECurrent; friend EPower; friend EResistance; static const char unitName[10]; EPotential (DOUBLE x = 0) {value = x;} EPotential& operator+= (EPOTENTIAL rs) {value += rs.value; return *this;} EPotential& operator-= (EPOTENTIAL rs) {value -= rs.value; return *this;} EPotential& operator*= (DOUBLE rs) {value *= rs; return *this;} EPotential& operator/= (DOUBLE rs) {value /= rs; return *this;} EPotential operator+ (EPOTENTIAL rs) const {EPotential result = *this; return result += rs;} EPotential operator- (EPOTENTIAL rs) const {EPotential result = *this; return result -= rs;} EPotential operator* (DOUBLE rs) const {EPotential result = *this; return result *= rs;} EPotential operator/ (DOUBLE rs) const {EPotential result = *this; return result /= rs;} double operator/ (EPOTENTIAL rs) const {return value / rs.value;} EPower operator* (ECURRENT i) const; EResistance operator/ (ECURRENT i) const; ECurrent operator/ (ERESISTANCE r) const; bool operator==(EPOTENTIAL rs) const {return value==rs.value;} bool operator <(EPOTENTIAL rs) const {return value <=rs.value;} friend ostream& operator<< (ostream& ls, EPOTENTIAL rs) {return ls << rs.value << ' ' << EPotential::unitName;} }; // Resistance class // ---------------- class EResistance { friend ECurrent; friend EPotential; friend EPower; double value; public: static const char unitName[10]; EResistance (DOUBLE x = 0) : value(x) {} EResistance operator& (ERESISTANCE rs) const // Series connection {return value + rs.value;} EResistance operator| (ERESISTANCE rs) const // Parallel connection {return value * rs.value / (value + rs.value);} EPotential operator* (ECURRENT rs) const; // Ohm's law bool operator==(ERESISTANCE rs) const {return value==rs.value;} bool operator <(ERESISTANCE rs) const {return value <=rs.value;} friend ostream& operator<< (ostream& ls, ERESISTANCE rs) {return ls << rs.value << ' ' << EResistance::unitName;} }; // Current class // ------------- class ECurrent { friend EPotential; friend EPower; friend EResistance; double value; public: static const char unitName[10]; ECurrent (DOUBLE x = 0) : value(x) {} ECurrent& operator+= (ECURRENT rs) {value += rs.value; return *this;} ECurrent& operator-= (ECURRENT rs) {value -= rs.value; return *this;} ECurrent& operator*= (DOUBLE rs) {value *= rs; return *this;} ECurrent& operator/= (DOUBLE rs) {value /= rs; return *this;} ECurrent operator+ (ECURRENT rs) const {ECurrent result = *this; return result += rs;} ECurrent operator- (ECURRENT rs) const {ECurrent result = *this; return result -= rs;} ECurrent operator* (DOUBLE rs) const {ECurrent result = *this; return result *= rs;} ECurrent operator/ (DOUBLE rs) const {ECurrent result = *this; return result /= rs;} double operator/ (ECURRENT rs) const {return value / rs.value;} EPower operator* (EPOTENTIAL v) const ; EPotential operator* (ERESISTANCE r) const ; bool operator==(ECURRENT rs) const {return value==rs.value;} bool operator <(ECURRENT rs) const {return value <=rs.value;} friend ostream& operator<< (ostream& ls, const ECurrent rs) {return ls << rs.value << ' ' << ECurrent::unitName;} }; // Power class // ----------- class EPower { friend ECurrent; friend EPotential; friend EResistance; double value; public: static const char unitName[10]; EPower(DOUBLE x) : value(x) {} EPower& operator+= (EPOWER rs) {value += rs.value; return *this;} EPower& operator-= (EPOWER rs) {value -= rs.value; return *this;} EPower& operator*= (DOUBLE rs) {value *= rs; return *this;} EPower& operator/= (DOUBLE rs) {value /= rs; return *this;} EPower operator+ (EPOWER rs) const {EPower result = *this; return result += rs;} EPower operator- (EPOWER rs) const {EPower result = *this; return result -= rs;} EPower operator* (DOUBLE rs) const {EPower result = *this; return result *= rs;} EPower operator/ (DOUBLE rs) const {EPower result = *this; return result /= rs;} double operator/ (EPOWER rs) const {return value / rs.value;} EPotential operator/(ECURRENT i) const ; ECurrent operator/(EPOTENTIAL v) const ; bool operator==(EPOTENTIAL rs) const {return value==rs.value;} bool operator <(EPOTENTIAL rs) const {return value <=rs.value;} friend ostream& operator<< (ostream& ls, EPOWER rs) {return ls << rs.value << ' ' << EPower::unitName;} }; // Arithmetic involving mixed classes // ---------------------------------- inline EPower EPotential ::operator* (ECURRENT rs) const {return value * rs.value;} inline EPower ECurrent ::operator* (EPOTENTIAL rs) const {return value * rs.value;} inline EPotential EPower ::operator/ (ECURRENT rs) const {return value / rs.value;} inline ECurrent EPower ::operator/ (EPOTENTIAL rs) const {return value / rs.value;} inline EPotential EResistance::operator* (ECURRENT rs) const {return value * rs.value;} inline EPotential ECurrent ::operator* (ERESISTANCE rs) const {return value * rs.value;} inline ECurrent EPotential ::operator/ (ERESISTANCE rs) const {return value / rs.value;} inline EResistance EPotential ::operator/ (ECURRENT rs) const {return value / rs.value;} // Left side scalar multiplications: (commutative) inline EPotential operator* (DOUBLE ls, EPOTENTIAL rs) {return rs * ls;} inline EPower operator* (DOUBLE ls, EPOWER rs) {return rs * ls;} inline ECurrent operator* (DOUBLE ls, ECURRENT rs) {return rs * ls;} #endif