본문 바로가기

반응형

전체 글

(304)
[C++] 상속된 클래스에서 형변환 & 가상 함수 #include using namespace std; /*** Class alpha ***/ class alpha { public: int _x; public: alpha(); alpha(int x); alpha(const alpha& pt); void print(); }; alpha::alpha() { _x=0; } alpha::alpha(int x) { _x=x; } alpha::alpha(const alpha& pt) { _x=pt._x; } void alpha::print() { cout print(); return 0; } 가상함수가 없는 원본 코드일때 class alpha에서 print()함수가 virtual 함수일때의 결과 class beta에서 print()함수가 virtual 함수일때의 ..
[C++] 얕은 복사의 착각 #include #include using namespace std; class oop { public: char* str; oop(); oop(const oop& pt); void print(); }; oop::oop(const oop& pt) { str=pt.str; /////////////
[C++] 클래스 포함 cur 클래스가 point 클래스를 포함하는 관계 cur 클래스의 정보은닉 약속된 것 : public으로 지정된 멤버 함수들의 원형 약속되지 않은 것 : 두 개의 point 객체를 갖는 벰버 #include #include "cur.h" using namespace std; int main() { cur a(1,2,3,4); // cur 클래스 타입 변수 선언 a.print(); // 출력 cur b; // 초기화 값이 0인지 확인하기 위해 대입 없이 선언 b.print(); // point 클래스의 기본 생성자의 값에 따라 바뀐다. point::point() cur c; c.setbottomright(point(3, 4)); // 아래의 오른쪽 x, y값 설정 c.settopleft(point(1, 2..
[C++] typedef로 멤버 함수 포인터 사용 #include using namespace std; // typedef int (*TYPE)(int); 일반 함수를 typedef로 만든는 형식 class ask { public: int x; int f(int k); ask(); }; int ask::f(int k) { x=k; cout
[C++] typedef로 함수 포인터 사용 #include using namespace std; typedef int (*TYPE)(int); int f(int); int main() { f(3); TYPE x; x=&f; x(5); return 0; } int f(int k) { k*=k; cout
[C++] 클래스에서 static과 소멸자 역할 확인 #include using namespace std; class ask { public: static int cnt; // 객체 생성시 객체 생성수 카운트 static void print(); int obj; ask(); ask(const ask& pt); ~ask(); // 소멸자 }; int ask::cnt=0; // static 변수 초기화 void ask::print() { cout
[C++] 동적 할당 및 소멸자 사용 (얕은 복사의 문제점) #include using namespace std; class dynamic { public: int *arr; dynamic(int size); // 생성자 ~dynamic(); // 소멸자 }; dynamic::dynamic(int size) { arr=new int [size]; //메모리 동적 할당 } dynamic::~dynamic() { delete[] arr; // arr 동적 할당 메모리 해제 arr=NULL; // 에러 방지 위해 NULL 삽입 } int main() { int usize; cin >> usize; dynamic arr1(usize); for(int i=0; i
[C] 파일 입출력 끝부분 인지 확인 if(feof(File open val)!=0) break; printf("data : %c", ch);

반응형