Click here to Skip to main content
15,886,518 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: In type conversion of 2 different classes can i convert both ways ? Pin
CPallini25-Mar-18 10:50
mveCPallini25-Mar-18 10:50 
GeneralRe: In type conversion of 2 different classes can i convert both ways ? Pin
Tarun Jha25-Mar-18 23:25
Tarun Jha25-Mar-18 23:25 
GeneralRe: In type conversion of 2 different classes can i convert both ways ? Pin
CPallini26-Mar-18 0:18
mveCPallini26-Mar-18 0:18 
QuestionAdding two matrix objects and assigning the result to third object using overloaded operators ? Pin
Tarun Jha24-Mar-18 8:19
Tarun Jha24-Mar-18 8:19 
AnswerRe: Adding two matrix objects and assigning the result to third object using overloaded operators ? Pin
Victor Nijegorodov24-Mar-18 8:35
Victor Nijegorodov24-Mar-18 8:35 
GeneralRe: Adding two matrix objects and assigning the result to third object using overloaded operators ? Pin
Tarun Jha24-Mar-18 10:06
Tarun Jha24-Mar-18 10:06 
AnswerRe: Adding two matrix objects and assigning the result to third object using overloaded operators ? Pin
Richard MacCutchan24-Mar-18 21:13
mveRichard MacCutchan24-Mar-18 21:13 
GeneralRe: Adding two matrix objects and assigning the result to third object using overloaded operators ? Pin
Tarun Jha25-Mar-18 5:08
Tarun Jha25-Mar-18 5:08 
i was told that these type of questions are to be posted here.
and i did the program..

#include <iostream>
using namespace std;

class matrix{
    int **p;                        //pointer to Matrix
    int d1, d2;
    int id=0;                        //identif which operator is used

public:
    //matrix(){}                      //default constructor
    matrix(int x=20, int y=20);       //parameterised

    //to pass private data-members to other objects
    int getD_1(){return (d1);}
    int getD_2(){return (d2);}
    //int getId(const int x){id =x ;}
    //void showId(){cout<<"ID :\t"<<id<<endl;}

    //for taking input nad printing output
    void get_element();
    void put_element();

    //to check feasibility
    friend int checkAdd(matrix &, matrix &);                                     //to check the feasibility of the operation
    friend int checkMul(matrix &, matrix &);

    //Rule of three
    friend matrix operator+(matrix &, matrix &);
    friend matrix operator*(matrix &, matrix &);
    //friend matrix operator-(matrix &, matrix &);

    void operator = (matrix );

    ~matrix(){
        for (int i = 0; i < d1; i++)
            delete p[i];
        delete p;
    }
};

//=================================================================================================================

//defining overloaded assingment operator
void matrix::operator = (matrix obj){
    this->d1 = obj.d1;
    this->d2 = obj.d2;
    this->id = obj.id;
    for(int i=0; i<d1; i++)
        for(int j=0; j<d2; j++)
            this->p[i][j] = obj.p[i][j];
}

//defining overloaded addition operator
matrix operator+(matrix &a, matrix &b){
    matrix temp;
    temp.d1 = a.d1;
    temp.d2 = a.d2;
    temp.id = 1;

    if(checkAdd(a, b) == 0){                //checks the viability if it's feasible or not.
        for(int i=0; i<temp.d1; i++)
            for(int j=0; j<temp.d2; j++)
                temp.p[i][j] = a.p[i][j] + b.p[i][j] ;

        return(temp);
    }
    else {
        cout<<"Matrix addition not possible as rows and coloumn's of given 2 matrices are not equal"<<endl;
        return(a);
    }
}

//defining overloaded Multiplication operator
matrix operator*(matrix &a, matrix &b){
    matrix temp;
    temp.d1 = a.d1;
    temp.d2 = b.d2;
    temp.id = 2;

    if(checkMul(a, b) == 0){              //checks the viability if it's feasible or not.
        int sum=0;
        for (int c = 0; c < a.d1; c++) {
            for (int d = 0; d < b.d2; d++) {
                for (int k = 0; k <b.d1; k++) {
                    sum = sum + a.p[c][k]*b.p[k][d];
                }
                temp.p[c][d] = sum;
                sum = 0;
            }
        }
        return (temp);
    }
    else {
        cout<<"Matrix Multiplication is not possible as coloumn of 1st Matrix != Row of 2nd Matrix "<<endl;
        return(a);
    }
}

//defining parameterised constucter
matrix ::matrix(int x, int y){
    d1 = x;
    d2 = y;
    p = new int *[d1];                  //creates an array of pointers

    for(int i=0; i<d1; i++)
        p[i] = new int [d2];            //creates row for each row
}

//defining checkAdd f^n to check if given 2 matrices obj can be added or not
int checkAdd(matrix &a, matrix &b){
    if(a.getD_1() == b.getD_1() && a.getD_2() == b.getD_2())
        return 0;
    else return 1;
}

//defining checkMul f^n to check if 2 matrices obj can be Multiplied.
int checkMul(matrix &a, matrix &b){
    if(a.getD_2() == b.getD_1())
        return 0;
    else return 1;
}

//gets the element from the user.
void matrix::get_element(){
    for(int i=0; i<d1; i++)
        for(int j=0; j<d2; j++){
            cout<<"m["<<(i+1)<<"]["<<(j+1)<<"] = ";
            cin>>p[i][j];
        }
}


//displays the current matrix
void matrix::put_element(){
    if(id == 1) cout<<"\nThe result of addition of 2 matrices is :"<<endl;
    else if(id==2)  cout<<"\nThe result of product of 2 matrices is:"<<endl;

    for(int i=0; i<d1; i++){
        for(int j=0; j<d2; j++){
            cout<<p[i][j]<<"\t";
        }
        cout<<endl;
    }
    cout<<"\n";
}

//=====================================================================================================================

int main(){
    matrix A(3, 2), B(2, 4), C;                     //matrix objects a constucted

    cout<<"Enter matrix elements row by row \n\n";
    A.get_element();
    A.put_element();

    B.get_element();
    B.put_element();

    C = A * B;
    C.showId();
    C.put_element();

    C = A + B;
    C.showId();
    C.put_element();

    return 0;
}


and thank you for the advice
GeneralRe: Adding two matrix objects and assigning the result to third object using overloaded operators ? Pin
Richard MacCutchan25-Mar-18 5:32
mveRichard MacCutchan25-Mar-18 5:32 
GeneralRe: Adding two matrix objects and assigning the result to third object using overloaded operators ? Pin
Tarun Jha25-Mar-18 6:37
Tarun Jha25-Mar-18 6:37 
GeneralRe: Adding two matrix objects and assigning the result to third object using overloaded operators ? Pin
Richard MacCutchan25-Mar-18 7:16
mveRichard MacCutchan25-Mar-18 7:16 
GeneralRe: Adding two matrix objects and assigning the result to third object using overloaded operators ? Pin
Tarun Jha25-Mar-18 8:06
Tarun Jha25-Mar-18 8:06 
QuestionLinux C++ implementation of ioctl SOLVED Pin
Vaclav_23-Mar-18 8:50
Vaclav_23-Mar-18 8:50 
AnswerRe: Linux C++ implementation of ioctl Pin
Jochen Arndt24-Mar-18 23:39
professionalJochen Arndt24-Mar-18 23:39 
AnswerRe: Linux C++ implementation of ioctl Pin
leon de boer26-Mar-18 16:53
leon de boer26-Mar-18 16:53 
GeneralRe: Linux C++ implementation of ioctl Pin
Vaclav_4-Apr-18 5:08
Vaclav_4-Apr-18 5:08 
QuestionVS2017: Cannot debug Dll's Pin
indrekm23-Mar-18 1:13
indrekm23-Mar-18 1:13 
SuggestionRe: VS2017: Cannot debug Dll's Pin
Richard MacCutchan23-Mar-18 1:21
mveRichard MacCutchan23-Mar-18 1:21 
SuggestionRe: VS2017: Cannot debug Dll's Pin
Jochen Arndt23-Mar-18 1:25
professionalJochen Arndt23-Mar-18 1:25 
AnswerRe: VS2017: Cannot debug Dll's Pin
Randor 23-Mar-18 8:09
professional Randor 23-Mar-18 8:09 
QuestionSelect and Pick lines in OpenGL Pin
Gopi Nath22-Mar-18 19:40
Gopi Nath22-Mar-18 19:40 
AnswerRe: Select and Pick lines in OpenGL Pin
leon de boer22-Mar-18 20:30
leon de boer22-Mar-18 20:30 
GeneralRe: Select and Pick lines in OpenGL Pin
Gopi Nath22-Mar-18 21:08
Gopi Nath22-Mar-18 21:08 
GeneralRe: Select and Pick lines in OpenGL Pin
Gopi Nath24-Jul-18 21:17
Gopi Nath24-Jul-18 21:17 
QuestionNotifiation Messages for CSpinButtonCtrl in MFC Pin
Sampath57922-Mar-18 3:46
Sampath57922-Mar-18 3:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.