본문 바로가기
대외활동/한화비전 VEDA

메서드 오버로딩 & 디폴트 매개변수 & static & 프렌드 & 연산자 오버로딩

by hoshi03 2024. 7. 26.

• 메서드 오버로딩

이름이 같고 인자가 다른 메서드를 이용해서 다형성을 구현

인자가 모호하면 오버로딩이 불가능하다

#include <iostream>
#include <bits/stdc++.h>
#include <random>
#include <string>
using namespace std;


int getMin(int a, int b) {
	return (a > b) ? b : a;
}

double getMin(double a, double b) {
	return (a >=b) ? b : a;
}

string getMin(string a, string b) {
	return (a.compare(b) > 0) ? b : a;
}

int getMin(int arr[], int size) {
	int min = INT16_MAX;
	for (int i = 0; i < size; i++) if(min < arr[i]) min = arr[i];
	return min;
}

int main() {

	cout << getMin(5, 10) << '\n';
	cout << getMin(5.5, 10.2) << '\n';
	cout << getMin("AAAA", "ABVB") << '\n';


	return 0;
}

 

• 디폴트 매개변수

 

디폴트 매개변수는 함수의 매개변수에 기본값을 먼저 넣어두는 방법

사용할때 오른쪽 부터 채우는 방식으로 사용한다!

#include <iostream>
#include <bits/stdc++.h>
#include <random>
#include <string>
using namespace std;

void star(int a);

void star(int a = 5) {
	for (int i = 0; i < a; i++)
	{
		for (int j = 0; j < i+1; j++) {
			cout << '*' << ' ';
		}
		cout << '\n';
	}
}


int main() {

	star();
	cout << '\n';
	star(5);

	return 0;
}

 

• static

 

클래스의 static 변수는

클래스명 :: 변수 형태로 접근한다, 클래스 객체의 인자로 접근하지 않고 클래스 :: 형태로 바로 접근 가능하다

 

• 프렌드 함수

 

프렌드 키워드를 통해서 클래스의 멤버와 동일한 접근 자격을 줄 수 있음

 

클래스 내에 프렌드 함수를 선언하면 외부 함수에서 클래스에 자유롭게 접근할 수 있다

friend bool equals(Rectangle a, Rectangle b);

 

클래스 외부 함수, 여기서 접근 가능하다

bool equals(Rectangle a, Rectangle b) {
	if (a.width == b.width && a.height == b.height) return true;
	return false;
}

 

• 연산자 오버로딩

 

클래스 내에 오퍼레이터 연산자를 선언하고 += , == ,+ 등 연산자로 할 동작을 재정의

Rectangle operator+(Rectangle& obj);
Rectangle operator+(Rectangle& obj);
Rectangle& operator+=(Rectangle& obj);

 

#include <iostream>
using namespace std;

class Rectangle {
	int width;
	int height;
	static int num;
public:

	Rectangle(int width, int height);
	Rectangle();
	~Rectangle();
	static int getNumOfCircles();
	friend bool equals(Rectangle a, Rectangle b);

	int getArea() {
		return width * height;
	}

	int operator+(Rectangle& obj); // 넓이 합
	bool operator==(Rectangle& obj); // 넓이 동일 여부
	Rectangle& operator+=(Rectangle& obj); // 넓이를 합쳐서 대입
};

int Rectangle::num = 0;


Rectangle::Rectangle() {
	width = 5;
	height = 10;
	num++;
	//cout << "width : " << width << " height : " << height << '\n';
}

Rectangle::~Rectangle() {
	num--;
}

Rectangle::Rectangle(int width, int hegiht) {
	this->width = width;
	this->height = hegiht;
}


int Rectangle::getNumOfCircles() {
	return num;
}

bool equals(Rectangle a, Rectangle b) {
	if (a.width == b.width && a.height == b.height) return true;
	return false;
}

int Rectangle :: operator+(Rectangle& obj) {
	int tmp = 0;
	tmp += this->getArea();
	tmp += obj.getArea();
	return tmp;
}

bool Rectangle :: operator==(Rectangle& obj) {
	if (this->getArea() == obj.getArea()) return true;
	return false;
}

Rectangle& Rectangle :: operator+=(Rectangle& obj) {
	this->width += obj.width;
	return *this;
}

int main() {
	Rectangle a(5,4);
	Rectangle b(3,2);

	cout << a + b << '\n';
	if (a == b) cout << "넓이가 같음!" << '\n';


}

 

• 참조 리턴 << 연산자

 

>> 연산자로 Power의 인자 kick과 punch에 각각 3을 더하고 다시 5,6을 더하는 함수를 만들어보자

Power& Power :: operator << (int n) {
	kick += n;
	punch += n;
	return *this;
}

 

 

#include <iostream>

using namespace std;

class Power {
	int kick;
	int punch;

public :
	Power(int kick = 0, int punch = 0) {
		this->kick = kick; this->punch = punch;
	}

	void show();
	Power& operator << (int n); // 참조 리턴
};

void Power::show() {
	cout << "kick = " << kick << ',' << "punch" << punch << '\n';
}

Power& Power :: operator << (int n) {
	kick += n;
	punch += n;
	return *this;
}


int main() {
	Power a(1, 2);
	a << 3 << 5 << 6;
	a.show();
}

 

• 입출력 연산자 오버로딩

집가서 정리해둘것

 

'대외활동 > 한화비전 VEDA' 카테고리의 다른 글

템플릿 & STL  (0) 2024.07.29
상속, 오버라이딩, 추상화  (0) 2024.07.29
C++ 객체지향 프로그래밍  (0) 2024.07.25
컴파일러 최적화  (2) 2024.07.23
보이드 포인터를 이용한 제네릭 프로그래밍  (1) 2024.07.23