c++ 질문이 있습니다 ㅠ
글쓴이: 쿠키앤 크림 (쿠앤크)@Google / 작성시간: 토, 2017/12/02 - 4:28오후
#include <iostream> using namespace std; class complex{ protected : int real; int image; public: complex(int r=0,int i=0); friend istream &operator>>(istream &in,complex &a); friend ostream &operator<<(ostream &os,complex &a); friend complex operator+(complex left,complex right); }; complex::complex(int r,int i) : real(r),image(i) {} complex operator+(complex left,complex right) { complex result; result.real=left.real+right.real; result.real=left.image+right.image; return result; } ostream &operator<<(ostream &os,complex &a) { os<<a.real<<"+"<<a.image<<"i"; return os; } istream &operator>>(istream &in,complex &a) { cout<<endl<<"복소수 입력"<<endl<<endl; cout<<"실수부 입력 = "; in>>a.real; cout<<"허수부 입력 = "; in>>a.image; return in; } class matrix : public complex{ public : complex a; ostream &operator=(ostream &os,complex &b); }; void main() { matrix x[3][3]; matrix y[3][3]; matrix z[3][3]; complex a[3][3]; cout<<"첫번째 행렬 입력"<<endl; for(int r=0;r<3;r++){ for(int i=0;i<3;i++){ cin>>x[r][i]; } } cout<<endl<<"두번째 행렬 입력"<<endl; for(int r=0;r<3;r++){ for(int i=0;i<3;i++){ cin>>y[r][i]; } } cout<<endl; for(int r=0;r<3;r++){ for(int i=0;i<3;i++){ cout<<x[r][i]<<"\t"; } cout<<endl; } cout<<endl; for(int r=0;r<3;r++){ for(int i=0;i<3;i++){ cout<<y[r][i]<<"\t"; } cout<<endl; } for(int r=0;r<3;r++){ for(int i=0;i<3;i++){ a[r][i]=x[r][i]+y[r][i]; z[r][i]=a[r][i]; } } }
3x3 행렬인데 복소수가 있는 행렬 두개의 사칙연산을 만들고있습니다.
앉아서 씨름하고 있는데 여기서 막히네요 ㅠㅠ 도움을 청합니다..
연산자 오버로딩을 꼭 써야하는데요 matrix z의 원소에 complex a를 넣으려고 하는데
=를 따로 오버로딩을 해야될거같은데 잘 모르겠네요 ㅠㅠ
Forums:
이것저것 문제가 많아 보이는 C++ 코드입니다만,
이것저것 문제가 많아 보이는 C++ 코드입니다만, 과제물이라는 게 너무 명백하니 따로 드릴 말씀은 없군요.
일단 바라시는 내용에 대한 레퍼런스를 드립니다. 대입 연산자를 제대로 작성한다면 동작하게 만들 수는 있을 겁니다.
http://en.cppreference.com/w/cpp/language/copy_assignment
https://www.tutorialspoint
https://www.tutorialspoint.com/cplusplus/cpp_overloading.htm 도 보셔요.
세벌 https://sebuls.blogspot.kr/
댓글 달기