반응형

문제

2차원 행렬을 추상화한 Matrix 클래스를 활용하는 다음 코드가 있다.

int main()
{
	Matrix a(4, 3, 2, 1), b;
	int x[4], y[4] = { 1,2,3,4 };
	a >> x;
	b << y;
	for (int i = 0; i < 4; i++)cout << x[i] << ' ';
	cout << endl;
	b.show();
}

 

결과

4 3 2 1

Matrix = { 1 2 3 4 }

 

1. <<, >> 연사자 함수를 Matrix 멤버 함수로 구현하라.

#include<iostream>
using namespace std;

class Matrix {
	int a[4];
public:
	Matrix() {};
	Matrix(int a, int b, int c, int d) {
		this->a[0] = a;
		this->a[1] = b;
		this->a[2] = c;
		this->a[3] = d;
	}
	void show();
	void operator>>(int x[]);
	void operator<<(int y[]);
};
void Matrix::show()
{
	cout << "Matrix = { " << a[0] << ' ' << a[1] << ' ' << a[2] << ' ' << a[3] << " }" << endl;
}
void Matrix::operator>>(int x[])
{
for(int i =0;i<4;i++)
	x[i] = a[i];
}
void Matrix::operator<<(int y[])
{
for(int i= 0 ;i<4;i++)
	a[i] = y[i];
}
int main()
{
	Matrix a(4, 3, 2, 1), b;
	int x[4], y[4] = { 1,2,3,4 };
	a >> x;
	b << y;
	for (int i = 0; i < 4; i++)cout << x[i] << ' ';
	cout << endl;
	b.show();
}

 

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

#include<iostream>
using namespace std;

class Matrix {
	int a[4];
public:
	Matrix() {};
	Matrix(int a, int b, int c, int d) {
		this->a[0] = a;
		this->a[1] = b;
		this->a[2] = c;
		this->a[3] = d;
	}
	void show();
	friend void operator>>(Matrix a, int x[]);
	friend void operator<<(Matrix& b,int y[]);
};
void Matrix::show()
{
	cout << "Matrix = { " << a[0] << ' ' << a[1] << ' ' << a[2] << ' ' << a[3] << " }" << endl;
}
void operator>>(Matrix a,int x[])
{
	for (int i = 0; i < 4; i++)
		x[i] = a.a[i];
}
void operator<<(Matrix& b, int y[])
{
	for (int i = 0; i < 4; i++)
		b.a[i] = y[i];
}
int main()
{
	Matrix a(4, 3, 2, 1), b;
	int x[4], y[4] = { 1,2,3,4 };
	a >> x;
	b << y;
	for (int i = 0; i < 4; i++)cout << x[i] << ' ';
	cout << endl;
	b.show();
}

 

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

반응형
반응형

문제

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 book("벼룩시장", 0, 50); //가격은 0
	if (!book) cout << "꽁짜다" << endl;
}

 

결과

공짜다

 

소스 코드

#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; }
	bool operator!();
};
bool Book::operator!()
{
	if (price == 0)
		return true;
	else
		return false;
}
int main() {
	Book book("벼룩시장", 0, 50); //가격은 0
	if (!book) cout << "꽁짜다" << endl;
}

 

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

반응형
반응형

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; }
};

 

2. Book 객체를 활용하는 사례이다.

int main() {
	Book a("명품 C++", 30000, 500), b("고품 C++", 30000, 500);
	if (a == 30000)cout << "정가 30000원" << endl;       //price비교
	if (a == "명품 C++")cout << "명품 C++ 입니다." << endl; //책 title 비교
	if (a == b)cout << "두 책이 같은 책입니다." << endl; //title,price,pages 모두 비교
}

 

결과

정가 30000원

명품 C++입니다.

 

1. 세 개의 == 연산자를 함수를 가진 Book 클래스를 작성하라.

#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; }
    bool operator==(int a);
	bool operator==(string str);
	bool operator==(Book& a);
};
bool Book::operator==(int a)
{
	if (price == a)
		return true;
	else
		return false;
}
bool Book::operator==(string str)
{
	if (title == str)
		return true;
	else
		return false;
}
bool Book::operator==(Book b)
{
	if (price == b.price && title == b.title && pages == b.pages)
		return true;
	else
		return false;
}
int main() {
	Book a("명품 C++", 30000, 500), b("고품 C++", 30000, 500);
	if (a == 30000)cout << "정가 30000원" << endl;       //price비교
	if (a == "명품 C++")cout << "명품 C++ 입니다." << endl; //책 title 비교
	if (a == b)cout << "두 책이 같은 책입니다." << endl; //title,price,pages 모두 비교
}

 

2. 세 개의 == 연산자를 프렌드 함수로 작성하라.

#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==(Book b, int a);
	friend bool operator==(Book a, string str);
	friend bool operator==(Book a, Book b);
};
bool operator==(Book a, int b)
{
	if (a.price == b)
		return true;
	else
		return false;
}
bool operator==(Book a, string str)
{
	if (a.title == str)
		return true;
	else
		return false;
}
bool operator==(Book a, Book& b)
{
	if (a.price == b.price && a.title == b.title && a.pages == b.pages)
		return true;
	else
		return false;
}
int main() {
	Book a("명품 C++", 30000, 500), b("고품 C++", 30000, 500);
	if (a == 30000)cout << "정가 30000원" << endl;       //price비교
	if (a == "명품 C++")cout << "명품 C++ 입니다." << endl; //책 title 비교
	if (a == b)cout << "두 책이 같은 책입니다." << endl; //title,price,pages 모두 비교
}

 

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

반응형
반응형

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; }
};

 

1. Book 객체에 대해 다음 연산을 하려고 한다.

int main()
{
	Book a("청춘", 20000, 300), b("미래", 30000, 500);
	a += 500; // 책 a의 가격 500원 증가
	b -= 500; // 책 b의 가격 500원 감소
	a.show();
	b.show();
	return 0;
}

 

결과

청춘 20500원 300 페이지

미래 29500원 500 페이지

 

1. +=, -= 연산자 함수를 Power 클래스의 멤버 함수로 구현하라

#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; }
    Book& operator+= (int a);
	Book& operator-= (int a);
};
Book& Book::operator+=(int a)
{
	price += a;
	return *this;
}
Book& Book::operator-=(int a)
{
	price -= a;
	return *this;
}
int main()
{
	Book a("청춘", 20000, 300), b("미래", 30000, 500);
	a += 500;
	b -= 500;
	a.show();
	b.show();
	return 0;
}

 

2. +=, -= 연산자 함수를 Power 클래스의 외부 함수로 구현하라

#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 Book operator+=(Book& b, int a);
	friend Book operator-=(Book& b, int a);
};
Book operator+=(Book& b, int a)
{
	b.price += a;
	return b;
}
Book operator-=(Book& b, int a)
{
	b.price -= a;
	return b;
}
int main()
{
	Book a("청춘", 20000, 300), b("미래", 30000, 500);
	a += 500;
	b -= 500;
	a.show();
	b.show();
	return 0;
}

 

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

반응형

+ Recent posts