반응형

문제

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

int main() {
	Matrix a(1, 2, 3, 4), b(2, 3, 4, 5), c;
	c = a + b;
	a += b;
	a.show(); b.show(); c.show();
	if (a == c)
		cout << "a and c are same " << endl;
}

 

결과

Matrix = { 3 5 7 9 }

Matrix = { 2 3 4 5 }

Matrix = { 3 5 7 9 }

a and c are the same

 

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

#include<iostream>
using namespace std;
class Matrix {
	int a, b, c, d;
public:
	Matrix() {};
	Matrix(int a, int b, int c, int d);
	void show();
    Matrix& operator+(Matrix& a);
    Matrix& operator+=(Matrix& a);
    bool operator==(Matrix b);
};

Matrix::Matrix(int a, int b, int c, int d)
{
	this->a = a;
	this->b = b;
	this->c = c;
	this->d = d;
}

void Matrix::show()
{
	cout << "Matrix = { " << this->a << " " << this->b << " " << this->c << " " << this->d << " }" << endl;
}

Matrix& Matrix::operator+(Matrix& a)
{
	Matrix b;
	b.a = this->a + a.a;
	b.b = this->b + a.b;
	b.c = this->c + a.c;
	b.d = this->d + a.d;
	return b;
}

Matrix& Matrix::operator+=(Matrix& a)
{
	this->a += a.a;
	this->b += a.b;
	this->c += a.c;
	this->d += a.d;
	return *this;
}

bool Matrix::operator==(Matrix b)
{
	if (this->a == b.a && this->b == b.b && this->c == b.c && this->d == b.d)
		return true;
	return false;
}

int main() {
	Matrix a(1, 2, 3, 4), b(2, 3, 4, 5), c;
	c = a + b;
	a += b;
	a.show(); b.show(); c.show();
	if (a == c)
		cout << "a and c are same " << endl;
}

 

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

#include<iostream>
using namespace std;
class Matrix {
	int a, b, c, d;
public:
	Matrix() {};
	Matrix(int a, int b, int c, int d);
	void show();
    friend Matrix operator+(Matrix a, Matrix b);
	friend Matrix operator+=(Matrix& a, Matrix b);
	friend bool operator==(Matrix a, Matrix b);
};
Matrix::Matrix(int a, int b, int c, int d)
{
	this->a = a;
	this->b = b;
	this->c = c;
	this->d = d;
}
void Matrix::show()
{
	cout << "Matrix = { " << this->a << " " << this->b << " " << this->c << " " << this->d << " }" << endl;
}
Matrix operator+(Matrix a, Matrix b)
{
	Matrix c;
	c.a = a.a + b.a;
	c.b = a.b + b.b;
	c.c = a.c + b.c;
	c.d = a.d + b.d;
	return c;
}
Matrix operator+=(Matrix& a, Matrix b)
{
	a.a += b.a;
	a.b += b.b;
	a.c += b.c;
	a.d += b.d;
	return a;
}
bool operator ==(Matrix a, Matrix b)
{
	if (a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d)
		return true;
	return false;
}
int main() {
	Matrix a(1, 2, 3, 4), b(2, 3, 4, 5), c;
	c = a + b;
	a += b;
	a.show(); b.show(); c.show();
	if (a == c)
		cout << "a and c are same " << endl;
}
반응형
반응형

다음 main()에서 Color 클래스는 3요소(빨강, 초록, 파랑)로 하나의 색을 나타내는 클래스이다. (4장 실습 문제 1번 참고). + 연사자로 색을 더하고, == 연사자로 색을 비교하고자 한다. 실행결과를 참고하여 Color 클래스와 연산자, 그리고 프로그램을 완성하라.

int main() {
	Color red(255, 0, 0), blue(0, 0, 255), c;
	c = red + blue;
	c.show();

	Color fuchsia(255, 0, 255);
	if (c == fuchsia)
		cout << "보라색 맞음";
	else
		cout << "보라색 아님";
}

 

결과

255 0 255

보라색 맞음

 

1. + 와 == 연사자를 Color 클래스의 멤버 함수로 구현하라.

#include<iostream>
using namespace std;
class Color {
	int r, g, b;
public:
	Color() {};
	Color(int r, int g, int b) { this->r = r; this->g = g; this->b = b; };
	void show();
    Color operator+(Color b);
	bool operator==(Color f);
    };
Color Color::operator+(Color b)
{
	Color a;
	a.r = this->r + b.r;
	a.g = this->g + b.g;
	a.b = this->b + b.b;
	return a;
}
bool Color::operator==(Color f)
{
	if (this->r == f.r && this->g == f.g && this->b == b)
		return true;
	return false;
}
int main() {
	Color red(255, 0, 0), blue(0, 0, 255), c;
	c = red + blue;
	c.show();

	Color fuchsia(255, 0, 255);
	if (c == fuchsia)
		cout << "보라색 맞음";
	else
		cout << "보라색 아님";
}

 

2. +와 == 연사자를 Color 클래스의 프렌드 함수로 구현하라.

#include<iostream>
using namespace std;
class Color {
	int r, g, b;
public:
	Color() {};
	Color(int r, int g, int b) { this->r = r; this->g = g; this->b = b; };
	void show();
    friend Color operator+(Color a, Color b);
	friend bool operator==(Color a, Color b);
};
void Color::show()
{
	cout << this->r << " " << this->g << " " << this->b << endl;
}
Color operator+(Color a, Color b)
{
	Color c;
	c.r = a.r + b.r;
	c.g = a.g + b.g;
	c.b = a.b + b.b;
	return c;
}
bool operator==(Color a, Color b)
{
	if (a.r == b.r && a.g == b.g && a.b == b.b)
		return true;
	return false;
}
int main() {
	Color red(255, 0, 0), blue(0, 0, 255), c;
	c = red + blue;
	c.show();

	Color fuchsia(255, 0, 255);
	if (c == fuchsia)
		cout << "보라색 맞음";
	else
		cout << "보라색 아님";
}

 

오류 또는 수정 사항 궁금한 점 있으시면 댓글로 남겨주세요.

반응형
반응형

1~4번 까지 쓰일 Book 클래스입니다.

class Book {
	string title;
	int price, pages;
public:
	Book(string title = "", int price = 0, int pages = 0) {
		this->title = title; this->price = price; this->pages = pages;
	}
	void show() {
		cout << title << ' ' << price << "원 " << pages << " 페이지" << endl;
	}
	string getTitle() { return title; }
}

 

다음 연산을 통해 책의 제목을 사전 순으로 비교하고자 한다. <연산자를 작성하라.

int main()
{
	Book a("청춘", 20000, 300);
	string b;
	cout << "책 이름을 입력하세요 >> ";
	getline(cin, b); //키보드로부터 문자열로 책 이름을 입력받음
	if (b < a)
		cout << a.getTitle() << "이 " << b << "보다 뒤에 있구나!" << endl;
}

 

결과

책 이름을 입력하세요>>바람과 함께 사라지다

청춘이 바람과 함께 사라지다보다 뒤에 있구나!

 

소스코드(friend 함수 사용)

#include<iostream>
#include<string>
using namespace std;
class Book {
	string title;
	int price, pages;
public:
	Book(string title = "", int price = 0, int pages = 0) {
		this->title = title; this->price = price; this->pages = pages;
	}
	void show() {
		cout << title << ' ' << price << "원 " << pages << " 페이지" << endl;
	}
	string getTitle() { return title; }
	friend bool operator<(string b, Book a);
};
bool operator<(string b, Book a)
{
	if (b < a.title)
		return true;
	else
		return false;
}
int main()
{
	Book a("청춘", 20000, 300);
	string b;
	cout << "책 이름을 입력하세요 >> ";
	getline(cin, b); //키보드로부터 문자열로 책 이름을 입력받음
	if (b < a)
		cout << a.getTitle() << "이 " << b << "보다 뒤에 있구나!" << endl;
}
반응형

+ Recent posts