Click here to Skip to main content
13,861,454 members

C / C++ / MFC

 
AnswerRe: Hackerrank:Down to Zero problem Pin
David Crow25-Mar-18 16:41
memberDavid Crow25-Mar-18 16:41 
QuestionIn type conversion of 2 different classes can i convert both ways ? Pin
Tarun Jha25-Mar-18 6:14
memberTarun Jha25-Mar-18 6:14 
AnswerRe: In type conversion of 2 different classes can i convert both ways ? Pin
Victor Nijegorodov25-Mar-18 9:07
memberVictor Nijegorodov25-Mar-18 9:07 
GeneralRe: In type conversion of 2 different classes can i convert both ways ? Pin
Tarun Jha26-Mar-18 0:26
memberTarun Jha26-Mar-18 0:26 
AnswerRe: In type conversion of 2 different classes can i convert both ways ? Pin
CPallini25-Mar-18 11:50
mveCPallini25-Mar-18 11:50 
GeneralRe: In type conversion of 2 different classes can i convert both ways ? Pin
Tarun Jha26-Mar-18 0:25
memberTarun Jha26-Mar-18 0:25 
GeneralRe: In type conversion of 2 different classes can i convert both ways ? Pin
CPallini26-Mar-18 1:18
mveCPallini26-Mar-18 1:18 
QuestionAdding two matrix objects and assigning the result to third object using overloaded operators ? Pin
Tarun Jha24-Mar-18 9:19
memberTarun Jha24-Mar-18 9:19 
here ih the code below i have tried to add two matrix objects and assign it to the third object but it doesn't give expected result and the rather some garbage value.

include <iostream>
using namespace std;

class matrix{
    int **p;    //pointer to Matrix
    int d1, d2;

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

    void get_element();
    void put_element();
    int checkAdd(matrix &);                                     //to check the feasibility of the operation

    //Rule of three
    matrix operator + (matrix &);
    void operator = (matrix );

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

matrix temp;                    //global matrix temp

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

void matrix::operator = (matrix obj){

    this->d1 = obj.d1;
    this->d2 = obj.d2;
    for(int i=0; i<d1; i++)
        for(int j=0; j<d2; j++)
            this->p[i][j] = obj.p[i][j];

    //put_element();
    //return(*this);
}

matrix matrix::operator+(matrix &obj){
    if(checkAdd(obj)==0)
    {
        //matrix temp;
        for(int i=0; i<obj.d1; i++)
            for(int j=0; j<obj.d2; j++)
                temp.p[i][j] = this->p[i][j] + obj.p[i][j];


        //temp.put_element();
        return(temp) ;
    }

    cout<<"coloumn and rows of both matrix are not equal !"<<endl;
    return (*this);
}

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
}

int matrix::checkAdd(matrix &obj){
    if((this->d1 == obj.d1) && (this->d2 == obj.d2))
        return 0;
    else return 1;
}

void matrix::get_element(){
    for(int i=0; i<d1; i++)
        for(int j=0; j<d2; j++){
            cout<<"m["<<i<<"]["<<j<<"] = ";
            cin>>p[i][j];
        }
    //cout<<"Following is your Matrix : \n\n";
    //put_element();
}

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

}

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

int main(){
    matrix A(3, 2), B(3, 2), 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;
    cout<<"\n\n";
    C.put_element();

    return 0;
}


thank you
AnswerRe: Adding two matrix objects and assigning the result to third object using overloaded operators ? Pin
Victor Nijegorodov24-Mar-18 9:35
memberVictor Nijegorodov24-Mar-18 9:35 
GeneralRe: Adding two matrix objects and assigning the result to third object using overloaded operators ? Pin
Tarun Jha24-Mar-18 11:06
memberTarun Jha24-Mar-18 11:06 
AnswerRe: Adding two matrix objects and assigning the result to third object using overloaded operators ? Pin
Richard MacCutchan24-Mar-18 22:13
protectorRichard MacCutchan24-Mar-18 22:13 
GeneralRe: Adding two matrix objects and assigning the result to third object using overloaded operators ? Pin
Tarun Jha25-Mar-18 6:08
memberTarun Jha25-Mar-18 6:08 
GeneralRe: Adding two matrix objects and assigning the result to third object using overloaded operators ? Pin
Richard MacCutchan25-Mar-18 6:32
protectorRichard MacCutchan25-Mar-18 6:32 
GeneralRe: Adding two matrix objects and assigning the result to third object using overloaded operators ? Pin
Tarun Jha25-Mar-18 7:37
memberTarun Jha25-Mar-18 7:37 
GeneralRe: Adding two matrix objects and assigning the result to third object using overloaded operators ? Pin
Richard MacCutchan25-Mar-18 8:16
protectorRichard MacCutchan25-Mar-18 8:16 
GeneralRe: Adding two matrix objects and assigning the result to third object using overloaded operators ? Pin
Tarun Jha25-Mar-18 9:06
memberTarun Jha25-Mar-18 9:06 
QuestionLinux C++ implementation of ioctl SOLVED Pin
Vaclav_23-Mar-18 9:50
memberVaclav_23-Mar-18 9:50 
AnswerRe: Linux C++ implementation of ioctl Pin
Jochen Arndt25-Mar-18 0:39
mveJochen Arndt25-Mar-18 0:39 
AnswerRe: Linux C++ implementation of ioctl Pin
leon de boer26-Mar-18 17:53
memberleon de boer26-Mar-18 17:53 
GeneralRe: Linux C++ implementation of ioctl Pin
Vaclav_4-Apr-18 6:08
memberVaclav_4-Apr-18 6:08 
QuestionVS2017: Cannot debug Dll's Pin
indrekm23-Mar-18 2:13
memberindrekm23-Mar-18 2:13 
SuggestionRe: VS2017: Cannot debug Dll's Pin
Richard MacCutchan23-Mar-18 2:21
protectorRichard MacCutchan23-Mar-18 2:21 
SuggestionRe: VS2017: Cannot debug Dll's Pin
Jochen Arndt23-Mar-18 2:25
mveJochen Arndt23-Mar-18 2:25 
AnswerRe: VS2017: Cannot debug Dll's Pin
Randor 23-Mar-18 9:09
professional Randor 23-Mar-18 9:09 
QuestionSelect and Pick lines in OpenGL Pin
Gopi Nath22-Mar-18 20:40
memberGopi Nath22-Mar-18 20:40 

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.


Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190214.1 | Last Updated 15 Feb 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid