dimanche 30 septembre 2018

+ Operator Not Overloaded

I'm writing a program that adds two imaginary numbers. The problem i'm facing is when i output the program the numbers doesn't get added and i get gibberish result.. I've wasted my whole day trying to find the solution but invain. I have an exam tomorrow and i really really would appreciate if anyone can help.

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


 class ComplexNo{
    int real, img;
    public:
    ComplexNo operator+(ComplexNo c){

        ComplexNo temp;
        temp.real = real + c.real;
        temp.img = img + c.img;
        return temp;

    }//have to give something
    friend istream& operator>>(istream& in, ComplexNo& m);
    friend ostream& operator<<(ostream& out,ComplexNo m);

 };

 istream& operator>>(istream& in, ComplexNo&m){

     int i,j;
     cout << "Enter the real number =";
     in >> m.real;
     cout << "Enter the imaginary number =";
     in >> m.img;

     return in;
 }

 ostream& operator<<(ostream&  out,ComplexNo m){

       out<<m.real<<((m.img<0)?"":"+")<<m.img<<"i" ;
       return out;
 }



class MatrixOfComplex{
    ComplexNo **data;
    int row, col;
    public:
    MatrixOfComplex& operator+(MatrixOfComplex &m)//Have to give
    {
        int i,j;
        MatrixOfComplex temp;

        if(row == m.row && col == m.col)
        {
            temp.row = row;
            temp.col = col;
            temp.data = new ComplexNo*[row];

            for(i=0;i<row;i++)
            {
                temp.data[i] = new ComplexNo[col];
                for(j=0;j<col;j++)
                {
                    temp.data[i][j] = data[i][j] + m.data[i][j];
                }
            }

        }
        else{
            cout << "The matrices cannot be added = ";
        }



    }

    void populateMatrix(int row, int col)
    {
        int i,j;
        cout << endl;
        cout << "Enter no of rows =";
        cin >> row;
        cout << "Enter no of cols = ";
        cin >> col;

        data = new ComplexNo*[row];

        if(data == NULL)
        {
            cout << "Matrix is yet to be populated";
        }
        else{
        for(i=0;i<row;i++)
        {
            data[i] = new ComplexNo[col];
            for(j=0;j<col;j++)
            {
                cin >> data[i][j];
            }
        }
    }
    }

   /// MatrixOfComplex operator[](int x){



    //show all ComplexNo objects of x-th row of the client using cout<<
    //and return the complex no of that row with highest real value.

    friend istream& operator>>(istream& in, MatrixOfComplex& m);
    friend ostream& operator<<(ostream& out, MatrixOfComplex m);
};

    ostream& operator<<(ostream& out, MatrixOfComplex m){

        int i,j;

        for(i=0;i<m.row;i++)
        {
            for(j=0;j<m.col;j++)
            {
                out << m.data[i][j] << " ";
            }
                out << endl;
        }
            return out;

    }


    istream& operator>>(istream& in,MatrixOfComplex &m){

        int i,j;
        cout << endl;
        cout << "Enter no of rows =";
        in >> m.row;
        cout << "Enter no of cols = ";
        in >> m.col;

        m.data = new ComplexNo*[m.row];

        if(m.data == NULL)
        {
            cout << "Matrix is yet to be populated";
        }
        else{
        for(i=0;i<m.row;i++)
        {
            m.data[i] = new ComplexNo[m.col];
            for(j=0;j<m.col;j++)
            {
                in >> m.data[i][j];
            }
        }
        }
        return in;
    }

int main(){
    MatrixOfComplex m1, m2, m3;
    cout <<"Enter the first matrix= " << endl;
    cin>>m1;
    //should ask no of rows, cols and
    //then populate rows*cols ComplexNo objects
    cout << m1;

   /// populateMatrix(row,col);
    cout << "Enter the 2nd matrix =" << endl;
     m2.populateMatrix(4,4);
    cout << endl;
    //set no of rows, cols with parameters and
    //then populate rows*cols ComplexNo objects with random real & img values
    cout << m2;

    m3 = m1 + m2;
    cout << m3;

//    ComplexNo temp = m1[0];
   // cout<<temp;
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire