Langauge/C++

Langauge/C++

[열혈 C++] 연산자 오버로딩2 정리 및 예제 풀이

배열의 인덱스 연산자 오버로딩 #include #include using namespace std; class BoundCheckIntArray { private: int* arr; int arrlen; public: BoundCheckIntArray(int len) :arrlen(len) { arr = new int[len]; } int& operator[] (int index) { if (index = arrlen) { cout

Langauge/C++

[열혈 C++] 연산자 오버로딩1 내용 요약 및 예제 풀이

Chapter10 연산자 오버로딩 문제 1번 풀이 #include using namespace std; class Point { private: int xpos, ypos; public: Point(int x = 0, int y = 0) :xpos(x), ypos(y) { } void ShowPoint() const { cout

Langauge/C++

Constructor and Separating Files

클래스 생성자 (constructor) * 생성자는 객체가 생성될 때 implicitly(암묵적으로) 호출된다. * 클래스와 같은 이름으로 정의되고, 값을 반환하지 않는다 (void형과는 다름) * 기본 생성자(default constructor)는 매개변수를 가지지 않는다. - 객체 클래스의 데이터 멤버의 생성자만 호출 #include using namespace std; class GradeBook { public: GradeBook(string name) { setCourseName(name); } // 클래스명과 이름이 같고 return 이 없다(void는 아님) void setCourseName(string name) { courseName = name; } string getCourseName..

Langauge/C++

Introduction to classes and objects #1

#pragma once #ifndef TIME_H //TIME_H가 define 되어있지 않으면 #define TIME_H //TIME_H를 define 하여라 class Time { public: // 행동 Time(); //constructor void setTime(int, int, int); void printUniversal(); void printStandard(); private: //속성 int hour; int minute; int second; }; // end class Time #endif // TIME_H가 이미 define 되어있으면 #define문은 실행되지 않는다. ifndef와 endif는 세트이다. - 클래스 정의 : 컴파일러에게 클래스에 속한 멤버 함수와 데이터 멤버를 ..

Langauge/C++

[열혈 C++ 프로그래밍] Chapter 07(상속의 이해)

열혈 C++ 프로그래밍의 상속 파트를 포스팅해보겠다. #include using namespace std; #pragma warning(disable : 4996) class PermanentWorker { private: char name[100]; int salary; public: PermanentWorker(const char name[100], int money) :salary(money) { strcpy(this->name, name); } int GetPay() const { return salary; } void ShowSalaryInfo() const { cout

Langauge/C++

[열혈 C++ 프로그래밍] Chapter06 (friend와 static그리고 const) 정리 및 문제 해결

const 함수 오버로딩 const의 유무에 따라 같은 이름의 함수도 구분될 수 있다. #include using namespace std; class SoSimple { private: int num; public: SoSimple(int n) : num(n) { } SoSimple& AddNum(int n) { num += n; return *this; } void ShowData() const { cout

Langauge/C++

[열혈 C++ 프로그래밍] Chapter05(복사 생성자) 정리 및 문제 해결

복사 생성자란, 생성자의 한 종류로서 호출 시키는 객체의 선언과 동시에 초기화할 때 쓸 수 있다. 이미 생성되고 초기화 된 객체를 이용해 다른 객체를 초기화시킬 수 있는 것이다. 다음 예제를 보자. #include using namespace std; class SoSimple { private: int num1; int num2; public: SoSimple(int n1, int n2) :num1(n1), num2(n2) { } SoSimple(SoSimple& copy) : num1(copy.num1), num2(copy.num2) { cout

Langauge/C++

[열혈 C++ 프로그래밍] Chapter04(클래스의 완성) 정리 및 문제 해결 #2

객체는 메모리 공간을 할당한 후 생성자 호출까지 마쳐야 객체라고 할 수 있다. 즉 생성자는 객체가 되기 위해 반드시 호출되어야하는 것인데, C++에서는 default 생성자를 두어 예외가 발생하지 않도록 한다. class aaa { private: int num; public: /* * aaa(){} */ int GetNum() { return num; } }; 다음 코드에서 주석을 처리한 부분은 default 생성자이다. new 연산자를 이용한 객체의 생성과정에서도 생성자가 호출된다. aaa *ptr = new aaa; class SoSimple { private: int num; public: SoSimple(int n) : num(n) { } }; 이니셜라이저로 num값에 n을 넣는 생성자를 만들었..

Langauge/C++

[열혈 C++ 프로그래밍] Chapter04(클래스의 완성) 정리 및 문제 해결 #1

#include using namespace std; class Point { public : int x; int y; }; class Rectangle { public : Point upLeft; Point lowRight; public: void ShowRecInfo() { cout

MINGYUM
'Langauge/C++' 카테고리의 글 목록 (2 Page)