main.cpp

#include <iostream>
#include "CScreen.h"
#include "CPoint.h"


void Swap(int* const _lhs, int* const _rhs);
void Swap(int& _lhs, int& _rhs);
void Swap(float* const _lhsf, float* const _rhsf);

// 디폴트 매개변수(Default Parameter, Default Argument)
int Sum(int _lhs, int _rhs = 8) { 
//기본값을 줌 (인자를 안 줄 시 기본값 5를 입력 기본값은 오른쪽부터 
//입력되어있어야 왼쪽도 입력 가능)
//오른쪽에 넣어줘도 왼쪽값 입력안해도 기본값이 들어감
	return _lhs + _rhs;
}

int Sum(int _lhs) {
	return _lhs + 5;
}

int main() {
	// 자료형(Data Type)
	// Boolean(True, False)를 저장
	bool b = true;
	// false : 0
	// true : 1
	// C에선 #define으로 정의해서 사용

	std::cout << "bool Size: " << sizeof(bool) << " Byte" << std::endl; //1바이트
	b = !b; // 값을 넣어도 0 아님 다 1(true)
			// 대입할때 복합연산자와 혼동하는것 조심
	std::cout << "b: " << b << std::endl;

	//while (true) {}

	//////////////////////////////////
	std::cout <<  std::endl;


	// 함수명은 같아도 뒤에 들어오는 자료형에 따라서 호출되는 함수가 정해지는것
	// 매개변수 자료형으로만 함수 호출을 함(반환형 불가)
	// 매개변수 인수가 들어가는 값이 달라도 결과적으로 똑같은 수의 매개변수를 사용할 시 오류가 남
	// 함수 오버로딩(Function Overloading)
	int lhs = 10, rhs = 20;
	Swap(lhs, rhs);
	std::cout << "Swap(lhs: " << lhs << ",rhs: " << rhs << ")" << std::endl;


	/////////////////////////
	std::cout << std::endl;

	std::cout << lhs << " + " << rhs << " = " << Sum(lhs,rhs) << std::endl;

	///////////////////////////////////////
	std::cout << std::endl;

	CScreen screen;
	CPoint point(1,2);
	point.SetX(4);
	point.SetY(2);
	point.Print();
	//////////////////////////////
	std::cout << std::endl;

	// 포인터
	CPoint* pPt;
	pPt = &point;
	pPt->SetY(10);
	pPt->Print();

	// 참조(Reference) - 포인터와 다르게 이름을 하나 더 만드는 형식, 그래서 바이트도 같다고 나온다. 
						// 주소값 또한 포인터와 다르게 원래 값과 동일하다.
	// 새로 만드는게 아니라서 생성과 동시에 초기화를 해줘야함
	CPoint& rPt = point;
	//rPt = point; <- 안됨
	rPt.SetX(20);
	rPt.Print();

	std::cout << "CPoint& Size: " << sizeof(rPt) << "Byte" << std::endl;
	printf("point Address: %p\n", &point);
	printf("rPt Address: %p\n", &rPt);
	printf("pPt Address: %p\n", &pPt);
	
	point.Print();

	///////////////////////////
	std::cout << std::endl;

	//깊은 복사(Deep-Copy), 얕은 복사(Shallow-Copy)
	//CPoint pt2nd(point);
	//CPoint pt2nd = point;
	CPoint pt2nd;
	pt2nd = point;
	//pt2nd.operator=(point); 풀어서
	pt2nd.Print();

	pt2nd + point;
	pt2nd.Print();

	return 0;
}

void Swap(int* const _lhs, int* const _rhs) {
	std::cout << "Swap with int Call" << std::endl;
	//NULL -> nullptr C++ 넘어오면서 사용(안정성)
	if (_lhs == nullptr || _rhs == nullptr) return;

	int tmp = *_lhs;
	*_lhs = *_rhs;
	*_rhs = tmp;
}
void Swap(int& _lhs, int& _rhs) {
	std::cout << "Swap with int Reference Call" << std::endl;
	int tmp = _lhs;
	_lhs = _rhs;
	_rhs = tmp;
}

void Swap(float* const _lhsf, float* const _rhsf) {
	std::cout << "Swap with float Call" << std::endl;
	if (_lhsf == nullptr || _rhsf == nullptr) return;
	float tmp = *_lhsf;
	*_lhsf = *_rhsf;
	* _rhsf = tmp;
}

 

CPoint.cpp

#include <iostream>
#include "CScreen.h"
#include "CPoint.h"


void Swap(int* const _lhs, int* const _rhs);
void Swap(int& _lhs, int& _rhs);
void Swap(float* const _lhsf, float* const _rhsf);

// 디폴트 매개변수(Default Parameter, Default Argument)
int Sum(int _lhs, int _rhs = 8) { //기본값을 줌 (인자를 안 줄 시 기본값 5를 입력 기본값은 오른쪽부터 입력되어있어야 왼쪽도 입력 가능) 
								  // 오른쪽에 넣어줘도 왼쪽값 입력안해도 기본값이 들어감
	return _lhs + _rhs;
}

int Sum(int _lhs) {
	return _lhs + 5;
}

int main() {
	// 자료형(Data Type)
	// Boolean(True, False)를 저장
	bool b = true;
	// false : 0
	// true : 1
	// C에선 #define으로 정의해서 사용

	std::cout << "bool Size: " << sizeof(bool) << " Byte" << std::endl; //1바이트
	b = !b; // 값을 넣어도 0 아님 다 1(true)
			// 대입할때 복합연산자와 혼동하는것 조심
	std::cout << "b: " << b << std::endl;

	//while (true) {}

	//////////////////////////////////
	std::cout <<  std::endl;


	// 함수명은 같아도 뒤에 들어오는 자료형에 따라서 호출되는 함수가 정해지는것
	// 매개변수 자료형으로만 함수 호출을 함(반환형 불가)
	// 매개변수 인수가 들어가는 값이 달라도 결과적으로 똑같은 수의 매개변수를 사용할 시 오류가 남
	// 함수 오버로딩(Function Overloading)
	int lhs = 10, rhs = 20;
	Swap(lhs, rhs);
	std::cout << "Swap(lhs: " << lhs << ",rhs: " << rhs << ")" << std::endl;


	/////////////////////////
	std::cout << std::endl;

	std::cout << lhs << " + " << rhs << " = " << Sum(lhs,rhs) << std::endl;

	///////////////////////////////////////
	std::cout << std::endl;

	CScreen screen;
	CPoint point(1,2);
	point.SetX(4);
	point.SetY(2);
	point.Print();
	//////////////////////////////
	std::cout << std::endl;

	// 포인터
	CPoint* pPt;
	pPt = &point;
	pPt->SetY(10);
	pPt->Print();

	// 참조(Reference) - 포인터와 다르게 이름을 하나 더 만드는 형식, 그래서 바이트도 같다고 나온다. 
						// 주소값 또한 포인터와 다르게 원래 값과 동일하다.
	// 새로 만드는게 아니라서 생성과 동시에 초기화를 해줘야함
	CPoint& rPt = point;
	//rPt = point; <- 안됨
	rPt.SetX(20);
	rPt.Print();

	std::cout << "CPoint& Size: " << sizeof(rPt) << "Byte" << std::endl;
	printf("point Address: %p\n", &point);
	printf("rPt Address: %p\n", &rPt);
	printf("pPt Address: %p\n", &pPt);
	
	point.Print();

	///////////////////////////
	std::cout << std::endl;

	//깊은 복사(Deep-Copy), 얕은 복사(Shallow-Copy)
	//CPoint pt2nd(point);
	//CPoint pt2nd = point;
	CPoint pt2nd;
	pt2nd = point;
	//pt2nd.operator=(point); 풀어서
	pt2nd.Print();

	pt2nd + point;
	pt2nd.Print();

	return 0;
}

void Swap(int* const _lhs, int* const _rhs) {
	std::cout << "Swap with int Call" << std::endl;
	//NULL -> nullptr C++ 넘어오면서 사용(안정성)
	if (_lhs == nullptr || _rhs == nullptr) return;

	int tmp = *_lhs;
	*_lhs = *_rhs;
	*_rhs = tmp;
}
void Swap(int& _lhs, int& _rhs) {
	std::cout << "Swap with int Reference Call" << std::endl;
	int tmp = _lhs;
	_lhs = _rhs;
	_rhs = tmp;
}

void Swap(float* const _lhsf, float* const _rhsf) {
	std::cout << "Swap with float Call" << std::endl;
	if (_lhsf == nullptr || _rhsf == nullptr) return;
	float tmp = *_lhsf;
	*_lhsf = *_rhsf;
	* _rhsf = tmp;
}

CPoint.h

// 중복 컴파일
//#ifndef _CPOINT_H_
//#define _CPOINT_H_
// 뒤에 정의가 안되어있으면 참 if'n'def , 건너뜀 #endif로 같이 사용해야함
// c에서 사용가능 c++에서도 선호
#pragma once
// 프라그마중에서 once라는 기능을 사용
// 중복 컴파일 방지 위와 아래 둘다 사용함

#include <iostream>

class CPoint {
private:
	int m_iX = 0, m_iY = 0;

public:
	CPoint();
	// 오버로딩 생성자(Overloading Constructor)
	CPoint(int _x, int _y);
	//복사 생성자(Copy Constructor)
	CPoint(const CPoint& _pt);
	~CPoint();
	
	// Getter / Setter
	// Property
	// Inline Function
	void SetX(int _x) { m_iX = _x; }
	int GetX(){ return m_iX; }
	inline void SetY(int _y) { m_iY = _y; };
	inline int GetY() { return m_iY; };//inline 검사하는거로 남용 금지 , 사용 시 정의 내용을 .h 파일에 작성해놔야함
	//스크린 좌표는 정수, 3D는 실수

	void Print();
public:
	// 연산자 오버로딩(Operator Overloading)
	// 복사 대입 연산자
	CPoint operator=(const CPoint& _pt);
	CPoint operator+(const CPoint& _pt);
};

//#endif

 

CScreen.cpp

#include "CScreen.h"

CScreen::CScreen() {
	std::cout << "CScreen Constructor Call" << std::endl;
}
	CScreen::~CScreen() {
		std::cout << "CScreen Constructor Call" << std::endl;
}

CScreen.h

#include <iostream>
#include "CPoint.h"


class CScreen {
private:
	CPoint pt;

public:
	CScreen();
	~CScreen();
};

'수업 일기장' 카테고리의 다른 글

Unity.2 UI/UX 및 버튼 이동  (0) 2021.02.24
Unity.1 이동 및 추격  (0) 2021.02.19
수업 일기장 #클래스(Class)  (0) 2021.01.25
수업 일기장 #참조(Reference)  (0) 2021.01.25
수업 일기장 #멤버 함수  (0) 2021.01.25

+ Recent posts