mercredi 22 juin 2016

(C++ )If I declare something private in a class but it can be changed via public methods of class, then why should I declare it private?


I created a new class fraction and since it is common sense to have input for numerator and denominator. I was advised by my senior to declare numerator and denominator in private. But if they can be changed and data abuse can occur to them anyhow, then why should I declare them private?

My code for fraction class(if you want to refer to my code):

#ifndef FRACTION_H
#define FRACTION_H
#endif // FRACTION_H

#include<string>
#include<iostream>
using namespace std;

class fraction{
int numer, denom;
void change_to_simplest_form();
void get_value_in_string();
public:
    fraction(int,int);
    fraction(){numer =0; denom =0;}
    void display();
    int get_num(){return numer;}
    int get_denom(){return denom;}
    float get_float_num(){return float(numer);}
    float get_float_denom(){return float(denom);}
    void get_it_all_done();
    inline bool operator<(fraction &rhs){if(float(this->get_float_num()/this->get_float_denom())<float(rhs.get_float_num()/rhs.get_float_denom()))return true; else return false;}
    inline bool operator>(fraction &rhs){if(float(this->get_float_num()/this->get_float_denom())>float(rhs.get_float_num()/rhs.get_float_denom()))return true; else return false;}
    inline bool operator<=(fraction &rhs){if(float(this->get_float_num()/this->get_float_denom())<=float(rhs.get_float_num()/rhs.get_float_denom()))return true; else return false;}
    inline bool operator>=(fraction &rhs){if(float(this->get_float_num()/this->get_float_denom())>=float(rhs.get_float_num()/rhs.get_float_denom()))return true; else return false;}
    inline bool operator==(fraction &rhs){if(float(this->get_float_num()/this->get_float_denom())==float(rhs.get_float_num()/rhs.get_float_denom()))return true; else return false;}
    inline void operator++(int){numer+=denom;}
    inline void operator+=(int a){numer+=(denom*a);}
    inline void operator--(int){numer-=denom;}
    inline void operator-=(int a){numer-=(denom*a);}
    inline void operator=(string a){bool denom_not_one = true;int i =0;numer=0;denom=0;for(i =0;a[i]!='/';++i){if(a[i]=='�'){denom=1;denom_not_one=false;break;}if(int(a[i])>=48 && int(a[i])<=57){numer*=10;if(a[i]!='0'){numer+=(int(a[i]-48));}}}for(;a[i]!='�' && denom_not_one;++i){if(int(a[i])>=48 && int(a[i])<=57){denom*=10;if(a[i]!='0'){denom+=(int(a[i]-48));}}}}
    inline void operator=(fraction &rhs){this->numer=rhs.get_num();this->denom=rhs.get_denom();}
    inline fraction operator*(fraction &rhs){fraction tmp(this->numer*rhs.get_num(),this->denom*rhs.get_denom()); return tmp;}
    inline void operator*=(fraction &rhs){this->numer*=rhs.get_num();this->denom*=rhs.get_denom();}
    inline void operator/=(fraction &rhs){this->numer*=rhs.get_denom();this->denom*=rhs.get_num();}
};
void fraction:: get_it_all_done(){
change_to_simplest_form();
}

fraction::fraction(int a, int b){
    denom = b;
    numer =a;
//    display();
}

void fraction::change_to_simplest_form(){
int divisor = 1;
bool islessthanzero = false;
if(numer<0){
    numer*=(-1);
    islessthanzero = true;
}
while(divisor <= numer && divisor <= denom){
    if(numer%divisor==0){
        if(denom%divisor==0){
            numer/=divisor;
            denom/=divisor;
        }
    }

divisor++;
}
if(islessthanzero){numer*=(-1);}
}

void fraction::display(){
change_to_simplest_form();
cout << "(" << numer << "/" << denom << ")" ;
}



std::ostream &operator<<(std::ostream &os,fraction &m){
        m.get_it_all_done();

        if(m.get_denom()!=1){
            cout << "(" << m.get_num() << "/" << m.get_denom() << ")" ;

        }
        else cout << m.get_num();
            return os;
}

Aucun commentaire:

Enregistrer un commentaire