프로그래밍 방법론 중간고사 (2008년 10월 22일)

1. 다음 C++ 프로그램의 출력을 쓰시오 (20).

(a)	(b)

class O {	class A {

public:	public: A(int i) {data = i;}

O ( ) { cout << "O()" << endl;}	int print() {cout <<  data << endl;}

~O ( ) { cout << "~O()" << endl;}	A& foo(int j) { data += j; return *this;}

};	A boo(int k) { data += k; return *this;}

class A {	private: int data;

public:	};

A ( ) { cout << "A()" << endl;}

A (A& a) { count << "copy A()" << endl;}    main() {

~A ( ) { cout << "~A()" << endl;}	A a(1);

private:	a.foo(2).boo(3).print();

O o;	a.print();

};

A b(1);

A a;	b.boo(2).foo(3).print();

void boo() { O  o; }	b.print();

void foo() { static O o; }	}

main() {

A b(a);                                        (c)

cout << "---" << endl; foo();	int i = 1;

cout << "---" << endl; boo();	void foo(int i, int* j, int& k) { i++; (*j)++; k++;}

cout << "---" << endl; foo();	main() {

cout << "---" << endl; boo();	int* j = &i; int& k = i;

cout << "---" << endl;	foo(i, j, k);

}	cout << i << " " << *j << " " << k << endl;

}

2. 다음 각각에 대하여 컴파일/수행 시 에러가 있는 것은 에러의 이유를 설명하고 에러가 없는 것은 출력을 쓰시오 (20).

(a)                                        (b)

class O {                                  class A {

public: O (int i) { data = i;}                 public: A(int i = 0) { data = i;}

int data;                                    A() { data = 0;}

};                                                    int data;

class A {                                  };

public: static O o;

};                                          main() {

A  a;

O A::o (10);

main() {                                       cout << a.data << endl;

A  a;                                    }

cout << a.o.data << endl;

}

(c)                                             (d)

class A {                                     class A {

public: A(int i=10) {data = i;}                  public: A(int i=10) {data=i;}

int boo() {return data;}                 int boo() {return data;}

int foo() const {return( boo()+2);}       A foo(int j) {data=j; return *this;}

private: const int data;                         A& zoo(int k) {data=k; return *this;}

};                                                private: int data;

main() {                                      };

const A a;                                 main() {

cout << a.foo() << endl;                     A a;

}                                               cout<<a.foo(20).zoo(30).boo()<<endl;

cout << a.boo() << endl;

}

3. 2차원 좌료를 표현하는 point class와 n-각형 (n-polygon)을 표현하는 polygon class를 다음과 같이 정의하였다. (함수, 변수, 클래스의 이름은 수정 불가!) (20)

class point {

int x;           // x좌표 값

int y;           // y좌표 값

int getX();       // x좌표값을 반환

int getY();       // y좌표값을 반환

void setX(int x); // x좌표값을 설정

void setY(int y); // y좌표값을 설정

};

class polygon {

point* p;         // point의 array

int n;            // n각형

double area();    // polygon의 넓이

};

(a) point와 polygon class를 data encapsulation을 활용하여 class를 재 정의하라. (10pts)

(함수 내부 구현은 필요 없음)

(b) point class의 getX, getY, setX, setY 함수를 구현하라. (10pts)

(c) polygon class의 copy constructor를 구현하라. (10pts)

4. Double Linked List is a list of nodes where each node has pointers to previous and next node. The following C++ program shows an double linked list with dummy nodes, i.e. head and tail, as we already practiced many times in the class. Append() appends a new node at the end of listand a node can be deleted using Remove(). (20)

struct node {

int n;

struct node* prev;

struct node* next;

};

class LinkedList {

public:

struct node head, tail;

int size; // number of nodes except head and tail

LinkedList() {…} // correctly implemented

// find node with value n and returns the node, returns NULL if not found

node* Find(int n) {…} // correctly implemented

// deletes the node with value n from the list

void Remove(int n) {

node *t = Find(n);

node *p = t->prev;

node *n = t->next;

p = t->prev; n = t->next;

p->next = n; n->prev = p;

size--;

free(t);

}

void Append(int n) {

node* t = (node*) malloc(sizeof(node));

t->n = n;

tail->prev = t;	t->next = &tail;

tail->prev->next = t; t->prev = tail->prev;

size++

}

};

int main() {

LinkedList list;

list.size=0;

for (int I=0; i<5; I++) {list.Append(i);}

list.Remove(5);

list.Remove(1);

list.Remove(6);

}

(a) Is the above program compiled correctly? If not, point out the error statement(s) and explain how to fix them.

(b) Does the above program work correctly as expected above? If not, point out the wrong statement(s) and explain how to fix with a minimal change.

(c) For the LinkedList class, does the above program practice data abstraction correctly? If not, explain the problem. In fact, when do we say that an object-oriented program practices data abstraction correctly? Explain clearly.

(d) Explain how to update LinkedList and main function with minimal changes so that it practices data abstraction correctly.

5. 다음 각각을 간단히 설명하시오 (복잡하게 쓰면 감점 있음) (20)

(a) Data abstraction의 개념과 장점 (8)

(b) Static member variable의 특징과 유용성 (4)

(c) Reference type의 parameter와 value type의 parameter의 차이점 (4)

(d) Function overloading의 개념과 장점 (4)