Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

미시Cpp/0323: Difference between revisions

From ZeroWiki
imported>장용운
No edit summary
 
imported>nerumin90
No edit summary
 
(4 intermediate revisions by 2 users not shown)
Line 4: Line 4:
= 미시Cpp =
= 미시Cpp =
회차 : 2회차
회차 : 2회차
시간 : 월요일 11시 30분 ~
시간 : 월요일 11시 30분 ~ 2시 30분
장소 : 6층 학회실
장소 : 6층 학회실


Line 12: Line 12:
| 멘토
| 멘토
| 장용운
| 장용운
| 미시행
| 출석
|-
|-
| rowspan="2" | 멘티
| rowspan="2" | 멘티
| 유재범
| 유재범
| 미시행
| 출석
|-
|-
| 신형철
| 신형철
| 미시행
| 출석
|}
|}


Line 53: Line 53:


== 코드 ==
== 코드 ==
 
* 생성자 오버로딩
 
class TestClass
{
private:
int member_1;
int member_2;
public:
TestClass();
TestClass(int number);
TestClass(int number_1, int number_2);
};
TestClass::TestClass() {}
TestClass::TestClass(int number) {}
TestClass::TestClass(int number_1, int number_2) {}
* 디폴트 생성자/소멸자
class TestClass
{
};
TestClass::TestClass() {} // 디폴트 생성자
TestClass::~TestClass() {} // 디폴트 소멸자
// 그 어떤 생성자/소멸자라도 정의되면 컴파일러에 의해 자동으로 만들어지지 않음.
* 생성자 초기화 리스트
#include <array>
class TestClass
{
private:
int number;
bool isPrime;
std::array<int, 5> arr;
public:
TestClass();
};
TestClass::TestClass() : number(4), arr({ { 1, 2, 3, 4, 5 } })
{
isPrime = false;
}
* new와 생성자 / delete와 소멸자
void main(void)
{
TestClass * inst = new TestClass(2, 3);
delete inst; // 소멸자를 호출
}
* 접근 권한
class TestClass
{
private:
int privateMem;
double privateMem_2;
// 자기 클래스에서만 접근 허용
public:
int publicMem;
float publicMem_2;
// 외부에서도 접근 허용
protected:
bool protectedMem;
bool protectedMem_2;
// 자기 클래스와 상속 받은 클래스에서만 접근 허용
};
* 객체 배열 / 객체 포인터 배열
void main(void)
{
TestClass * arr[3] = { new TestClass(), new TestClass(1), new TestClass(2, 3) };
TestClass * allocArr = new TestClass[10];
delete[] allocArr;
delete arr[1];
}
* std::string
#include <string>
        // 필요함
using namespace std;
int main(void)
{
int startIndex = 0;
string replaceString = string("a");
string strInst = string("TestInstance");
int strLength = strInst.length();
        // strLength = 12, strInst.size()로도 가능
strInst.append("Append");
        // strInst = TestInstanceAppend
char charAt = strInst.at(4);
        // charAt = I (Zero Index 기준)
strInst.compare("CompareMethod");
strInst.replace(startIndex, strLength, replaceString);
        // 기존의 문자를 덮어쓰면서 replace 하는 점에 주의.
stoi(strInst);
        // string to int (C++11)
strInst.find("Search Method");
        // 찾지 못하면 string::npos를 return.
string itoa = to_string(3);
        // ToString (C++11)
strInst.clear();
        // strInst = null
return 0;
}
* std::initializer_list
#include <initializer_list>
// 필요함
#include <vector>
using namespace std;
int AddArray(initializer_list<int> list);
int main(void)
{
int doTest = AddArray({ 1, 2, 3, 4, 5, 6, 7, 8, 9 });
int doTest_2 = AddArray({ 2, 3, 4, 5, 6 });
return 0;
}
int AddArray(initializer_list<int> list)
{
int result = 0;
vector<int> elem = vector<int>(list.begin(), list.end());
for (int i = 0; i < list.size(); i++)
{
result += elem.at(i);
}
return result;
}
* 명시적 디폴트/명시적으로 삭제한 생성자, 복사 생성자, 대입 연산자
class TestClass
{
private:
int amount;
double price;
public:
TestClass() = default;
TestClass(const TestClass &) = delete;
TestClass& operator = (const TestClass&) = delete;
};
void main(void)
{
TestClass a;
TestClass b;
a = b;
// 삭제된 연산자이므로 컴파일 오류
TestClass c(a);
// 삭제된 복사 생성자이므로 컴파일 오류
}
// 아무 함수에도 delete를 선언하여 삭제할 수 있다. ex) 형변환
* 생성자 위임
class TestClass
{
private:
int amount;
double price;
public:
TestClass();
TestClass(double price);
TestClass(int amount, double price);
};
TestClass::TestClass() : TestClass::TestClass(1.0)
{
}
TestClass::TestClass(double price) : TestClass::TestClass(1, price)
{
}
TestClass::TestClass(int amount, double price)
{
(*this).amount = amount;
(*this).price = price;
}
= 잡담 =
= 잡담 =
*
* 코드 추가 완료! - [[신형철]]


---------------------------------------------------------------------
---------------------------------------------------------------------

Latest revision as of 04:35, 27 April 2015

  객체 지향의 세계로 당신을 초대합니다
  드루와

미시Cpp

회차 : 2회차 시간 : 월요일 11시 30분 ~ 2시 30분 장소 : 6층 학회실

참가원

멘토 장용운 출석
멘티 유재범 출석
신형철 출석

이번에 배울 것

  • 클래스와 객체
○ 메서드의 위치
  • 데이터 추상화
  • 정보 은닉과 캡슐화
○ 정보 은닉 : uninterferable
○ 캡슐화 : independent
  • 생성자와 소멸자
○ 객체의 생명주기
○ 생성자 오버로딩
○ 디폴트 생성자/소멸자
○ 생성자 초기화 리스트(멤버 이니셜라이저)
○ new와 생성자
○ delete와 소멸자
○ 접근 권한
○ 객체 배열, 객체 포인터 배열과 ctor/dtor의 관계
  • 복사 생성자
○ 복사 생성자 호출시기
○ 깊은 복사와 얕은 복사
○ 대입 연산자와 복사 생성자
  • static과 const
  • ★(C++11)std::string
  • ★(C++11)std::initializer_list
  • ★(C++11)명시적 디폴트 생성자, 복사 생성자, 대입 연산자
  • ★(C++11)명시적으로 삭제된 생성자, 복사 생성자, 대입 연산자
  • ★(C++11)생성자 위임

스터디 진행

내용

코드

  • 생성자 오버로딩
class TestClass
{
private:
	int member_1;
	int member_2;

public:
	TestClass();
	TestClass(int number);
	TestClass(int number_1, int number_2);
};

TestClass::TestClass() {}
TestClass::TestClass(int number) {}
TestClass::TestClass(int number_1, int number_2) {}
  • 디폴트 생성자/소멸자
class TestClass
{
	
};

TestClass::TestClass() {} // 디폴트 생성자

TestClass::~TestClass() {} // 디폴트 소멸자

// 그 어떤 생성자/소멸자라도 정의되면 컴파일러에 의해 자동으로 만들어지지 않음.
  • 생성자 초기화 리스트
#include <array>

class TestClass
{
private:
	int number;
	bool isPrime;
	std::array<int, 5> arr;

public:
	TestClass();
};

TestClass::TestClass() : number(4), arr({ { 1, 2, 3, 4, 5 } })
{
	isPrime = false;
}
  • new와 생성자 / delete와 소멸자
void main(void)
{
	TestClass * inst = new TestClass(2, 3);
	
	delete inst; // 소멸자를 호출
}
  • 접근 권한
class TestClass
{
private:
	int privateMem;
	double privateMem_2;
// 자기 클래스에서만 접근 허용

public:
	int publicMem;
	float publicMem_2;
// 외부에서도 접근 허용

protected:
	bool protectedMem;
	bool protectedMem_2;
// 자기 클래스와 상속 받은 클래스에서만 접근 허용
};
  • 객체 배열 / 객체 포인터 배열
void main(void)
{
	TestClass * arr[3] = { new TestClass(), new TestClass(1), new TestClass(2, 3) };
	TestClass * allocArr = new TestClass[10];

	delete[] allocArr;
	delete arr[1];

}
  • std::string
#include <string>											
       // 필요함

using namespace std;

int main(void)
{
	int startIndex = 0;
	string replaceString = string("a");
	string strInst = string("TestInstance");

	int strLength = strInst.length();						
        // strLength = 12, strInst.size()로도 가능

	strInst.append("Append");								
        // strInst = TestInstanceAppend

	char charAt = strInst.at(4);							
        // charAt = I (Zero Index 기준)

	strInst.compare("CompareMethod");

	strInst.replace(startIndex, strLength, replaceString);	
        // 기존의 문자를 덮어쓰면서 replace 하는 점에 주의.
	
	stoi(strInst);											
        // string to int (C++11)
	
	strInst.find("Search Method");							
        // 찾지 못하면 string::npos를 return.

	string itoa = to_string(3);								
        // ToString (C++11)

	strInst.clear();										
        // strInst = null

	return 0;
}
  • std::initializer_list
#include <initializer_list>
// 필요함
#include <vector>

using namespace std;

int AddArray(initializer_list<int> list);

int main(void)
{
	int doTest = AddArray({ 1, 2, 3, 4, 5, 6, 7, 8, 9 });
	int doTest_2 = AddArray({ 2, 3, 4, 5, 6 });

	return 0;
}

int AddArray(initializer_list<int> list)
{
	int result = 0;
	vector<int> elem = vector<int>(list.begin(), list.end());

	for (int i = 0; i < list.size(); i++)
	{
		result += elem.at(i);
	}

	return result;
}
  • 명시적 디폴트/명시적으로 삭제한 생성자, 복사 생성자, 대입 연산자
class TestClass
{
private:
	int amount;
	double price;

public:
	TestClass() = default;
	
	TestClass(const TestClass &) = delete;
	TestClass& operator = (const TestClass&) = delete;


};

void main(void)
{
	TestClass a;
	TestClass b;

	a = b;
	// 삭제된 연산자이므로 컴파일 오류

	TestClass c(a);
	// 삭제된 복사 생성자이므로 컴파일 오류
}

// 아무 함수에도 delete를 선언하여 삭제할 수 있다. ex) 형변환
  • 생성자 위임
class TestClass
{
private:
	int amount;
	double price;

public:
	TestClass();
	TestClass(double price);
	TestClass(int amount, double price);
};

TestClass::TestClass() : TestClass::TestClass(1.0)
{

}

TestClass::TestClass(double price) : TestClass::TestClass(1, price)
{

}

TestClass::TestClass(int amount, double price)
{
	(*this).amount = amount;
	(*this).price = price;
}

잡담


활동지도/2015 미시Cpp