중간고사 예상문제
class Circle {
int radius;
public:
Circle(int radius) {
this->radius = radius; // 용도 1
}
Circle& setRadius(int radius) {
this->radius = radius;
return *this; // 용도 2
}
};
class Circle {
int radius;
public:
Circle() : radius(1) { }
Circle(int r) : radius(r) { }
~Circle() { }
};
void dynamic_test() {
Circle *pDonut = new Circle(20);
Circle *pArray = new Circle[10];
pDonut->getArea();
pArray[0].getArea();
delete pDonut;
delete [] pArray;
}
class Circle {
int radius;
public:
Circle() : radius(1) {}
void setRadius(int r) { radius = r; }
};
// 용도 1: 참조에 의한 호출 (Call by Reference)
void readRadius(Circle &c) {
int r;
cin >> r;
c.setRadius(r);
}
// 용도 2: 참조 리턴
int arr[] = {10, 20, 30};
int& getElement(int index) {
return arr[index];
}
class Person {
char* name;
int id;
public:
Person(int id, const char* name) {
this->id = id;
int len = strlen(name);
this->name = new char [len+1];
strcpy(this->name, name);
}
~Person() {
if(name) delete [] name;
}
// 깊은 복사 생성자
Person(const Person& p) {
this->id = p.id;
int len = strlen(p.name);
this->name = new char [len+1]; // 1. 별도 메모리 할당
strcpy(this->name, p.name); // 2. 내용 복사
}
};
void f(Person p) {
// 2. 값에 의한 호출 시 p의 복사 생성자 호출
}
Person g() {
Person mother(2, "Jane");
return mother; // 3. 값 리턴 시 복사 생성자 호출
}
int main() {
Person father(1, "Kitae");
Person son = father; // 1. 객체로 초기화 시 복사 생성자 호출
f(father);
Person daughter = g();
}
// 1. 형 변환 모호성
float square(float a) { return a*a; }
double square(double a) { return a*a; }
// 2. 참조 매개 변수 모호성
int add(int a, int b) { return a+b; }
int add(int a, int &b) { b=a+b; return b; }
// 3. 디폴트 매개 변수 모호성
void msg(int id) { ... }
void msg(int id, string s="") { ... }
// 생성자 중복 (간소화)
class Circle {
int radius;
public:
Circle(int r = 1) { radius = r; }
};
class Circle {
int radius;
static int numOfCircles;
public:
Circle(int r = 1) : radius(r) {
numOfCircles++;
}
~Circle() {
numOfCircles--;
}
double getArea() {
return 3.14 * radius * radius;
}
static int getNumOfCircles() {
return numOfCircles;
}
};
int Circle::numOfCircles = 0;
int main() {
Circle *p = new Circle[10];
cout << Circle::getNumOfCircles() << endl; // 10
delete [] p;
cout << p[0].getNumOfCircles() << endl; // 0
}
class RectManager {
public:
bool equals(Rect r, Rect s);
void copy(Rect& dest, Rect& src);
};
bool equals(Rect r, Rect s);
class Rect {
int width, height;
public:
Rect(int w=0, int h=0) : width(w), height(h) {}
// 유형 1: 전역 함수를 프렌드로 선언
friend bool equals(Rect r, Rect s);
// 유형 2: 다른 클래스의 멤버 함수를 프렌드로 선언
friend bool RectManager::equals(Rect r, Rect s);
// 유형 3: 다른 클래스 전체를 프렌드로 선언
friend class RectManager;
};
// 유형 1 구현
bool equals(Rect r, Rect s) {
return (r.width == s.width && r.height == s.height);
}
// 유형 2 구현
bool RectManager::equals(Rect r, Rect s) {
return (r.width == s.width && r.height == s.height);
}
// 유형 3 구현
void RectManager::copy(Rect& dest, Rect& src) {
dest.width = src.width;
dest.height = src.height;
}
class Power {
int kick;
int punch;
public:
Power(int k=0, int p=0) : kick(k), punch(p) {}
// 1. 이항 연산자 (멤버 함수)
Power operator+ (Power op2) {
Power tmp;
tmp.kick = this->kick + op2.kick;
tmp.punch = this->punch + op2.punch;
return tmp;
}
// 2. 이항 연산자 (friend 함수)
friend Power operator+ (int op1, Power op2);
// 3. 단항 전위 연산자
Power& operator++ () {
kick++;
punch++;
return *this;
}
// 4. 단항 후위 연산자
Power operator++ (int x) {
Power tmp = *this;
kick++;
punch++;
return tmp;
}
};
// 2. friend 함수 구현
Power operator+ (int op1, Power op2) {
Power tmp;
tmp.kick = op1 + op2.kick;
tmp.punch = op1 + op2.punch;
return tmp;
}
int main() {
Power a(1,2), b(3,4), c;
c = a + b; // 1. a.operator+(b) 호출
c = 2 + a; // 2. operator+(2, a) 호출
b = ++a; // 3. a.operator++() 호출
c = a++; // 4. a.operator++(0) 호출
}
class Person {
protected:
int age;
public:
void show() { }
};
class Student : public Person {
double score;
public:
void showStudent() {
cout << age;
show();
}
};
int main() {
Student mark;
Person* pBase;
Student* pDer;
// 1. 업 캐스팅
pBase = &mark;
pBase->show();
// pBase->showStudent(); : ERROR
// 2. 다운 캐스팅
pDer = (Student*)pBase;
pDer->showStudent();
}
class Base {
int a;
public:
Base() { cout << "Base() 생성자" << endl; }
Base(int x) : a(x) { cout << "Base(int) 생성자" << endl; }
~Base() { cout << "Base 소멸자" << endl; }
};
class Derived : public Base {
int b;
public:
// 1. 묵시적으로 Base() 호출
Derived() : b(0) {
cout << "Derived() 생성자" << endl;
}
// 2. 명시적으로 Base(x) 호출
Derived(int x, int y) : Base(x), b(y) {
cout << "Derived(int, int) 생성자" << endl;
}
~Derived() { cout << "Derived 소멸자" << endl; }
};
int main() {
Derived d(1, 2);
return 0;
}
class Person {
public:
int age;
};
// 1. 문제 상황 (모호성 발생)
class Student : public Person { };
class Employee : public Person { };
class Intern : public Student, public Employee {
public:
void setAge(int a) {
Student::age = a;
}
};
// 2. 해결 (가상 상속)
class StudentV : virtual public Person { };
class EmployeeV : virtual public Person { };
class InternV : public StudentV, public EmployeeV {
public:
void setAge(int a) {
age = a;
}
};