1. 1
2. 4
3.
class Sample{
friend SampleManager;
};
4.
class Sample
{
friend bool SampleManager::compare(Sample& a, Sample& b);
};
5.
원인: 클래스의 멤버가 아닌 isValid함수가 Student클래스 객체의 private변수에 접근해서
고친것:
class Student
{
int id;
public:
Student(int id) { this->id = id; }
friend bool isValid(Student s);
};
6.
원인: 멤버함수가 아닌 함수에서 private변수에 접근해서.
고친 것:
class Student;
class Professor;
class Student
{
int id;
public:
Student(int id) { this->id = id; }
friend void show(Student s, Professor p);
};
class Professor
{
string name;
public:
Professor(string name) { this->name = name; }
friend void show(Student s, Professor p);
};
void show(Student s, Professor p)
{
cout << s.id << p.name;
}
7.
오류: shopping함수에서 부적절하게 Food객체의 멤버변수에 접근해서.
class Person;
class Food;
class Food
{
int price;
string name;
public:
Food(string name, int price);
void buy();
friend Person;
};
class Person
{
int id;
public:
void shopping(Food food)
{
if (food.price < 1000)
food.buy();
}
int get() { return id; }
};
8. 4
9. friend함수는 멤버가 아니기 때문에 객체의 멤버처럼 호출될수 없다. 그래서 friend 를 지워야 한다.
10. 필요없다. 변수 x가 private으로 선언되지 않았기 때문에 굳이 friend를 붙여주지 않아도 된다.
11. cout객체의 <<연산자로서 문자를 출력하기도 하고,
12. 밀가루 + 계란 = 빵
13. 4
14. 4
15. 3
16. 4?
3번, 참조로 값을 받아야 한다. Power operator ++ (Power& a, int b)
17. 복사할 필요 없다. 내용만 복사되는것이고 하나의 객체가 되는것이 아니기 때문이다.
'IT공부 > C++' 카테고리의 다른 글
1/24 명품C++프로그래밍 8단원 이론문제 (0) | 2024.01.21 |
---|---|
1/19-20 명품C++프로그래밍 6단원 이론문제 (0) | 2024.01.19 |
1/18-19 명품c++ 프로그래밍 5단원 이론문제 (0) | 2024.01.18 |
1/17~18명품c++ 4단원 이론문제 (0) | 2024.01.17 |
1/17 명품C++프로그래밍 04 (0) | 2024.01.17 |