본문 바로가기

공부/C++

생성자의 초기화 리스트 사용 예제 초기화가 필요한 멤버를 가진 경우. 생성자의 초기화 리스트를 사용하여 멤버 변수들을 초기화해야 초기화가 이루어진다. #include using namespace std; class NeedConstructor {//클래스 정의. public: const int maxCount; //const속성 int& ref; //레퍼런스 타입 int sample; NeedConstructor(); }; NeedConstructor::NeedConstructor() : maxCount(100), ref(sample) // 이 부분에서 정해주는것이 생성자의 초기화 리스트 이다. { sample = 200; } int main() { NeedConstructor cr; cout 더보기
뇌를 자극하는 C++ 프로그래밍 복사 생성자 Exercise 21-2 복사생성자 관련. 예제 코드.와 Exercise 21-2 #include using namespace std; class Point {//클래스 정의. public: int x, y;//멤버 변수 void print();//멤버 함수 Point();//생성자들 Point(int initialX, int initialY); Point(const Point& pt); //복사생성자 }; Point::Point(const Point& pt) { cout 더보기
뇌를 자극하는 C++ 프로그래밍 비타민 퀴즈 클래스의 경우에도 연쇄적인 대입 연산자 사용이 가능한가? 에 대한 코드. #include using namespace std; class Point { public: int x, y; void print(); }; void Point::print()//멤버함수를 밖에서 정의함. { cout 더보기
뇌를 자극하는 C++ 프로그래밍 Exercise Exercise 21-1 내마음대로 코딩한 문제. 문제 내용은 distancefromorigin 을 멤버함수로 만들어서 두 값을 입력하여 대각선의 거리를 반환하는 것. #include #include #include using namespace std; class Point { public: int x, y; float z; float DistanceFromOrigin() { int a,b; //int값으로 받음 a= x*x; b= y*y; z = sqrt((float)a+(float)b); //계산된 값들을 float형으로 바꾼뒤 제곱근을 계산. return z;// z를 리턴. } void print(){ DistanceFromOrigin();//위의 멤버함수를 먼저 동작시킨 후 프린팅. printf.. 더보기
돌죽 번역 관련 ㅇㅇ 더보기