Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <fstream>
- #define ERROR_DIV_ZERO "ERROR: can not be divided by zero.\n"
- #define ERROR_CALC_PARAM "ERROR: parameter was not calculated.\n"
- #define ERROR_OPEN_FILE "ERROR: the file can not be opened.\n"
- #define ERROR_CREATE_FILE "ERROR: the file can not be created.\n"
- #define inputfile "input.txt"
- #define outputfile "output.txt"
- using namespace std;
- class calculationObject{
- protected:
- double *calculatedParameter;
- public:
- virtual void set() = 0;
- virtual void calcParameter() = 0;
- virtual void get() = 0;
- virtual double getResult() = 0;
- };
- class Charge : virtual public calculationObject{
- private:
- double q1, q2, eps, r;
- public:
- Charge() : calculationObject(){
- q1 = q2 = eps = r = 0;
- calculatedParameter = NULL;
- }
- Charge(double _q1, double _q2, double _eps, double _r) : calculationObject(), q1(_q1), q2(_q2), eps(_eps), r(_r){}
- Charge(const Charge& x){
- q1 = x.q1;
- q2 = x.q2;
- eps = x.eps;
- r = x.r;
- }
- void set(){
- cout << " please, enter information about charge " << endl;
- cout << " q1 = ";
- cin >> q1;
- cout << " q2 = ";
- cin >> q2;
- cout << " eps = ";
- cin >> eps;
- cout << " r = ";
- cin >> r;
- cout << endl;
- }
- void get(){
- cout << " information about charge " << endl;
- cout << " q1 = " << q1 << endl;
- cout << " q2 = " << q2 << endl;
- cout << " eps = " << eps << endl;
- cout << " r = " << r << endl;
- cout << endl;
- }
- void calcParameter(){
- if(r == 0 || eps == 0) {
- cout << ERROR_DIV_ZERO;
- return;
- }
- calculatedParameter = new double;
- *calculatedParameter = (q1 * q2) / (eps * pow(2.,r)) ;
- }
- double getResult(){
- if(calculatedParameter == NULL) {
- cout << ERROR_CALC_PARAM << endl;
- return 0;
- }
- cout << " calculated parameter = " << *calculatedParameter << endl;
- return *calculatedParameter;
- }
- Charge& operator = (const Charge& x){
- q1 = x.q1;
- q2 = x.q2;
- eps = x.eps;
- r = x.r;
- return *this;
- }
- bool operator == (const Charge& x) {return (calculatedParameter == x.calculatedParameter);}
- bool operator != (const Charge& x) {return (calculatedParameter != x.calculatedParameter);}
- bool operator < (const Charge& x) {return (calculatedParameter < x.calculatedParameter);}
- bool operator <= (const Charge& x) {return (calculatedParameter <= x.calculatedParameter);}
- bool operator > (const Charge& x) {return (calculatedParameter > x.calculatedParameter);}
- bool operator >= (const Charge& x) {return (calculatedParameter >= x.calculatedParameter);}
- friend ifstream& operator >> (ifstream&, Charge&);
- friend ofstream& operator << (ofstream&, const Charge&);
- friend istream& operator >> (istream&, Charge&);
- friend ostream& operator << (ostream&, const Charge&);
- };
- ifstream& operator >> (ifstream& fin, Charge& x) {
- if(!fin.is_open()){
- cout << ERROR_OPEN_FILE;
- return fin;
- }
- fin >> x.q1 >> x.q2 >> x.eps >> x.r;
- return fin;
- }
- ofstream& operator << (ofstream& fout, const Charge& x) {
- if(!fout.is_open()){
- cout << ERROR_CREATE_FILE;
- return fout;
- }
- fout << " information about charge: " << endl;
- fout << " q1 = " << x.q1 << endl;
- fout << " q2 = " << x.q2 << endl;
- fout << " eps = " << x.eps << endl;
- fout << " r = " << x.r << endl;
- fout << endl;
- return fout;
- }
- istream& operator >> (istream& in, Charge& x) {
- cout << " please, enter information about charge " << endl;
- cout << " q1 = ";
- in >> x.q1;
- cout << " q2 = ";
- in >> x.q2;
- cout << " eps = ";
- in >> x.eps;
- cout << " r = ";
- in >> x.r;
- cout << endl;
- return in;
- }
- ostream& operator << (ostream& out, const Charge& x) {
- out << " information about charge " << endl;
- out << " q1 = " << x.q1 << endl;
- out << " q2 = " << x.q2 << endl;
- out << " eps = " << x.eps << endl;
- out << " r = " << x.r << endl;
- out << endl;
- return out;
- }
- int main(){
- Charge x;
- cout << endl << "Default constructor:" << endl;
- cout << x;
- Charge y(5.3, 2, 0.3, 7);
- cout << "Init constructor:" << endl;
- cout << y;
- Charge z = y;
- cout << " Copy constructor " << endl;
- cout << z;
- cout << " Set function " << endl;
- x.set();
- cout << " Get function " << endl;
- x.get();
- x.calcParameter();
- y.calcParameter();
- cout << " Result function " << endl;
- cout << " x ";
- x.getResult();
- cout << endl << endl;
- cout << " y ";
- y.getResult();
- cout << endl;
- cout << " Operator == for x, y returned " << (x == y) << endl << endl;
- cout << " Operator != for x, y returned " << (x != y) << endl << endl;
- cout << " Operator < for x, y returned " << (x < y) << endl << endl;
- cout << " Operator <= for x, y returned " << (x <= y) << endl << endl;
- cout << " Operator > for x, y returned " << (x > y) << endl << endl;
- cout << " Operator >= for x, y returned " << (x >= y) << endl << endl;
- cout << endl;
- cout << " Reading input file" << endl;
- Charge a;
- ifstream fin(inputfile);
- ofstream fout(outputfile);
- fin >> a;
- cout << a;
- fout << a;
- a.calcParameter();
- fout << "calculated parameter = " << a.getResult() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement