Starting from:

$29.99

CSED232 Midterm Exam Solution



1. 다음은 객체지향 프로그래밍의 중요한 개념들이다. 각각의 개념들이 무엇을 뜻하는지 자세히 기술하시오.
a) Encapsulation
b) Information hiding (or data hiding)

2. 아래와 같은 코드가 있다. MyNameSpace 안의 func2 에서 MyNameSpace 밖의 func 함수를 호출하기 위해서는 어떤 식으로 호출해야 하는지 빈 칸 (a)에 들어갈 코드를 작성하시오.
void func()
{
std::cout << "Hello" << std::endl;
} namespace MyNameSpace { void func()
{
std::cout << "Bye~" << std::endl;
}

void func2()
{
(a)
}
}

3. 아래와 같은 코드가 있다. 아래 코드에서 Namespace1 에 정의된 함수인 func()를 Namespace2 에 포함시켜서 Namespace2::func() 와 같이 사용하고 싶다. 이때 필요한 코드를 빈칸 (a)에 작성하시오.
namespace Namespace1 { void func()
{
std::cout << "func()" << std::endl;
}
void func(int a)
{
std::cout << "func(int)" << std::endl;
}
void func(double a)
{
std::cout << "func(double)" << std::endl; }
}
namespace Namespace2 { void func2(int a, int b) { std::cout << "func2(int,int)" << std::endl; }

(a)
}
int main()
{
Namespace2::func();
Namespace2::func(3); Namespace2::func(3.); return 0;
}

4. 아래의 코드는 main.cpp 를 컴파일할 때 에러가 발생한다. 이 원인은 무엇이고 이를 해결하기 위해서는 어떻게 해야 하는지 기술하시오.

// polar.h
struct polar { double distance, angle; }; struct rect { double x, y; };

// polar_ops.h
#include "polar.h"
rect polar_to_rect(polar p); polar rect_to_polar(rect r);

// main.cpp
#include <iostream>
#include "polar.h"
#include "polar_ops.h"

using namespace std;

int main()
{ rect rplace; polar pplace; cout << "Enter the x and y values: "; while (cin >> rplace.x >> rplace.y) { pplace = rect_to_polar(rplace);
cout << "polar: " << pplace.distance << ", " << pplace.angle << endl; cout << "Next two numbers (q to quit): ";
}
cout << "Done. "; return 0;
}

5. 다음과 같이 class 와 static method 를 이용하면 namespace 와 비슷한 기능을 흉내 낼 수 있다. 그렇다면 class 와 static method 를 사용하는 것과 비교하였을 때 namespace 를 사용하는 장점은 무엇인가? Namespace 의 장점들을 최대한 많이 기술하시오.

class FakeNamespace
{ public:
static void func(); static void func2(); static void func3();
};
int main()
{
FakeNamespace::func();

return 0;
}





6. 아래의 코드는 문자열을 다루기 위한 간단한 String 클래스이다. main 함수에 있는 예제와 같이 이 클래스의 객체를 기존의 C-style 문자열을 처리하기 위한 strcpy 와 같은 함수에 바로 사용하고 싶다. 그러나 현재에는 이런 것을 가능하게 하는 코드가 빠져 있어서 아래의 코드는 컴파일 에러가 난다. String 객체를 C-style 문자열처럼 다룰 수 있게 하기 위해서 String 클래스에 무엇을 추가해야 하는가? 추가해야 하는 코드를 작성하시오.
#include<iostream>
#include<cstring>
class String { private: char *str; // pointer to string int len; // length of string public:
String(const char *s); // constructor
String(const String &st); // copy constructor
String(); // default constructor
~String(); // destructor

int length() const { return len; }

String &operator=(const String &st);
};

String::String() { len = 0; str = new char[1]; str[0] = ‘’; }
String::String(const char *s)
{ len = std::strlen(s); str = new char[len+1]; std::strcpy(str, s); }
String::String(const String &s)
{ len = s.len; str = new char[len+1]; std::strcpy(str, s.str); }
String::~String() { delete[] str; }

String& String::operator=(const String &st)
{
delete[] str; len = st.len; str = new char[len+1]; strcpy(str, st.str); return *this;
}
int main()
{
String pennywise("You'll float too."); char georgie[100];
std::strcpy(georgie, pennywise); return 0;
}

7. 다음 코드에서 Derived 클래스의 constructor 의 파라미터 중 v_는 Base 클래스로부터 상속받은 v 를 초기화하기 위한 값이고, vv_는 Derived 클래스의 멤버인 vv 를 초기화하기 위한 값이다. 빈 칸 (a) 에 들어갈 코드를 작성하시오.
#include <iostream>
class Base { private: int v; public:
Base(int v_) { v = v_; }
~Base() {}
}; class Derived : public Base { private: int vv; public:
Derived(int v_, int vv_);
~Derived() {}
};

Derived::Derived(int v_, int vv_)

(a)






8. 아래의 코드는 빨간 색으로 표시된 부분에서 컴파일 에러가 발생한다. 이를 수정하기 위해서는 어떻게 해야 하는가? 빈칸 (a)에 들어갈 컴파일 에러를 피하기 위한 코드를 작성하시오.

class IntVector { private:
int *arr; int len; public:
IntVector(int len_) : len(len_), arr(new int[len]) { }
IntVector(const IntVector &v) : len(v.len), arr(new int[len])
{ memcpy(arr, v.arr, sizeof(int) * len); }
IntVector() : len(0), arr(nullptr) { }
~IntVector() { delete[] arr; } int length() const { return len; }

char &operator[](int i)
{
return arr[i];
}



(a)


};
ostream& operator<<(ostream& os, const IntVector& v)
{
for(int i = 0; i < v.length(); i++) os << v[i] << " "; return os;
}


9. C++에서 클래스의 상속을 통해 구현되는 다형성이 무엇인지 자세히 기술하시오. 그리고 이 때 virtual method 를 왜 사용해야 하는지 설명하시오.



10. 다음 코드를 실행하면 코드의 아래에 있는 것과 같은 내용이 출력된다. 빈 칸 (a), (b), (c)에 들어갈 적절한 코드를 작성하시오.

#include <iostream>

class Vehicle { public:
Vehicle() {} virtual ~Vehicle() {} void show() const; protected:
(a)
};
void Vehicle::show() const
{ const int n_wheels = num_wheels();
std::cout << "Num wheels: " << n_wheels << std::endl;
}
class Sedan : public Vehicle
{ public: Sedan() {} ~Sedan() {} protected:
(b)
};
class Motorcycle : public Vehicle
{ public:
Motorcycle() {} ~Motorcycle() {} protected:
(c)
};
int main()
{
Vehicle *v[2]; v[0] = new Sedan(); v[1] = new Motorcycle();
for(int i = 0; i < 2; i++) v[i]->show(); delete v[0]; delete v[1]; return 0; }


출력:
Num wheels: 4
Num wheels: 2


More products