How to implement operator overload and in C ++?

2

Hello, I'm doing an exercise that consists of the following statement:

Sorryforputtingaprint,butIcouldnotselectthetextanditwouldbeanunnecessaryjobtotypeeverythinghere.

Finally,Imadetheclassandimplementedtheoverloadofthe+,-,*and/operators,butI'mnotabletodotheoperatorspart

asked by anonymous 02.01.2017 / 22:01

2 answers

1

I solved the above problems, so I'll leave the code here if anyone needs a day. I just need to implement the methods that return the roots of the equation, I'm trying to do that.

#include <iostream>
using namespace std;

class MeuInt{

    public:
        MeuInt(int i=0);
        int operator+(int);
        int operator-(int);
        int operator*(int);
        int operator*(MeuInt);
        int operator/(int);


        int getInteiro();
        void setInteiro(int);

    private:
        int inteiro;
        friend ostream &operator<<(ostream&, const MeuInt&);
        friend istream &operator>>(istream&, MeuInt&);
};

MeuInt::MeuInt(int i){
    inteiro = 1;
}



int MeuInt::operator+(int x){
    inteiro = x + inteiro;
    return inteiro;
}
int MeuInt::operator-(int x){
        inteiro = x - inteiro;
    return inteiro;
}
int MeuInt::operator*(int x){
    inteiro = x * inteiro;
    return inteiro;
}

int MeuInt::operator*(MeuInt x){
    inteiro = x * inteiro;
    return inteiro;
}


int MeuInt::operator/(int x){
    inteiro = x / inteiro;
    return inteiro;
}

int MeuInt::getInteiro(){
    return inteiro;
}

void MeuInt::setInteiro(int x){
    inteiro = x;
}

MeuInt calcDelta(MeuInt a, MeuInt b, MeuInt c){
    MeuInt delta = b*b - a * c * 4;
    return delta;

}

ostream &operator<<(ostream& out, const MeuInt& meuint){
    out << meuint.inteiro;

    return out;
}

istream &operator>>(istream& in, MeuInt& meuint){

    in >> meuint.inteiro;

    return in;
}



int main(int argc, char** argv) {

    MeuInt inteiro1(10);
    MeuInt A, B, C;

    int c = inteiro1 + 1;

    cout << "inteiro1 + 1: ";
    cout << c << endl;

    cout << "Entre com as variáveis: " << endl;
    cout << "A: ";
    cin >> A;

    cout << "B: ";
    cin >> B;

    cout << "C: ";
    cin >> C;


    return 0;
}
    
03.01.2017 / 01:43
2

In the header of the function: ostream &operator>>(ostream& saida, const MeuInt& meuint) Try to change the >> operator to << , as this is what is used in exit operations. The problem 'output' não foi declarado anteriormente , is because you passed the parameter of type ostream& with the name saida , and in the function you are using with the name output . In the istream& operator>>(std::istream&, MeuInt&) function, the problem as you said is because the function is not void, so you should return an element, in this case an implementation of the insert operator istream& entrada .

    
02.01.2017 / 23:45