반응형

7.5> 2차원 행렬을 추상화한 Matrix 클래스를 작성하고, show() 멤버 함수와 다음 연산이 가능하도록 연산자를 모두 구현하라.

(1) 연산자 함수를 Matrix의 멤버 함수로 구현하라.

<코드1>

#include <iostream>

 

using namespace std;

 

class Matrix{

private:

        int mat[4]; //인트형 어레이 mat 크기 4

public:

        Matrix(int x0=0,int x1=0,int x2=0, int x3=0);// 디폴트 매개변수는 0

        void show();

        Matrix operator+(Matrix op);

        Matrix operator+=(Matrix op);

        bool operator==(Matrix op);

};

Matrix::Matrix(int x0,int x1,int x2, int x3){ //생성자, 매개변수를 원소에 대입

        mat[0]=x0;     mat[1]=x1;

        mat[2]=x2;     mat[3]=x3;

}

void Matrix::show(){ //출력함수

        cout<<"Matrix = {";

        for(int i=0;i<4;i++) {cout<<" "<<mat[i];}

        cout<<" }"<<endl;

}

 

Matrix Matrix::operator+(Matrix op){ //+연산자 오버로딩, 각원소를 전부 더해서 객체 리턴

        Matrix tmp;

        for(int i=0;i<4;i++){

               (tmp.mat)[i]=(this->mat)[i]+(op.mat)[i];

        }

        return tmp;

}

 

Matrix Matrix::operator+=(Matrix op){ //+=연산자 오버로딩 객체자신과 op 원소를 각각더해서

        for(int i=0;i<4;i++){                 //자기 자신에 대입하고 객체자신 리턴

               (this->mat)[i]=(this->mat)[i]+(op.mat)[i];

        }

        return *this;

}

 

bool Matrix::operator==(Matrix op){ //==오퍼레이터 오버로딩

        for(int i=0;i<4;i++){

               if((this->mat)[i]!=(op.mat)[i]) return false; //같은 인덱스 원소 비교해서 다르면 바로 false 리턴

        }

        return true; //다맞으면 true 리턴

}

int main(void){

        Matrix a(1,2,3,4), b(2,3,4,5), c;

        c=a+b; //a+b값을 c 대입

        a+=b; //a+b값을 a 대입

        a.show(); b.show(); c.show();

        if(a==c) //ac 같으면..

               cout<<"a and c are the same" <<endl;

 

        return 0;

}

 

(2) 연산자 함수를 Matrix의 프렌드 함수로 구현하라.

<코드2>

#include <iostream>

 

using namespace std;

class Matrix{

private:

        int mat[4]; //인트형 어레이 mat 크기 4

public:

        Matrix(int x0=0,int x1=0,int x2=0, int x3=0);// 디폴트 매개변수는 0

        void show();

        friend Matrix operator+(Matrix op1,Matrix op2);

        friend Matrix operator+=(Matrix &op1,Matrix op2);

        friend bool operator==(Matrix op1,Matrix op2);

};

Matrix::Matrix(int x0,int x1,int x2, int x3){ //생성자, 매개변수를 원소에 대입

        mat[0]=x0;     mat[1]=x1;

        mat[2]=x2;     mat[3]=x3;

}

void Matrix::show(){ //출력함수

        cout<<"Matrix = {";

        for(int i=0;i<4;i++) {cout<<" "<<mat[i];}

        cout<<" }"<<endl;

}

Matrix operator+(Matrix op1,Matrix op2){  // 원소를 더해 나온 행렬을 리턴

        Matrix tmp;

        for(int i=0;i<4;i++){

               (tmp.mat)[i]=(op1.mat)[i]+(op2.mat)[i];

        }

        return tmp;

}

Matrix operator+=(Matrix &op1,Matrix op2){ //레퍼런스op1 쓰는 이유는 op1 바뀌어야해서

        for(int i=0;i<4;i++){                

               (op1.mat)[i]=(op1.mat)[i]+(op2.mat)[i]; //배열의 같은 인덱스 값을 더해서 op1 인덱스원소에 대입

        }

        return op1;

}

bool operator==(Matrix op1,Matrix op2){

               for(int i=0;i<4;i++){

               if((op1.mat)[i]!=(op2.mat)[i]) return false; //같은 인덱스 원소 비교해서 다르면 바로 false 리턴

        }

        return true; //다맞으면 true 리턴

}

 

 

int main(void){

        Matrix a(1,2,3,4), b(2,3,4,5), c;

        c=a+b; //a+b값을 c 대입

        a+=b; //a+b값을 a 대입

        a.show(); b.show(); c.show();

        if(a==c) //ac 같으면..

               cout<<"a and c are the same" <<endl;

 

        return 0;

}

 

<결과창>

       

                            

 

반응형

'컴퓨터 & 프로그래밍 & 전자공학 > C++' 카테고리의 다른 글

Circle class  (0) 2017.12.25
Matrix class 활용, 연산자 오버로딩  (0) 2017.12.25
Trace 클래스  (0) 2017.12.25
Random 클래스  (0) 2017.12.25
배열 빼기  (0) 2017.12.25

+ Recent posts