永发信息网

c++ class 题目求教 急!!!

答案:1  悬赏:30  手机版
解决时间 2021-01-14 07:42
  • 提问者网友:谁的错
  • 2021-01-13 19:31
c++ class 题目求教 急!!!
最佳答案
  • 五星知识达人网友:由着我着迷
  • 2021-01-13 20:56
// problem 1
#include
using std::cout;
using std::endl;

class first_class;

class third_class {
public:
third_class() {}
void print(const first_class &fc);
};

class first_class {
friend class second_class;
friend void third_class::print(const first_class &);
public:
first_class(int d) : data(d) {}
void print() const { cout << data << endl; }
private:
int data;
};

class second_class {
public:
second_class(int d) : fc(d) {}
void print() { cout << fc.data << endl; }
first_class get() { return fc; }
private:
first_class fc;
};

void third_class::print(const first_class &fc) {
fc.print();
}

int main() {
second_class sc(1);
sc.print();

third_class tc;
tc.print(sc.get());
return 0;
}

// problem 2
#include
using std::cout;
using std::endl;

class Hen {
public:
Hen() {}
void display() {
Nest nest(1);
cout << "I am a hen" << endl;
cout << nest.egg.egg_data << endl;
nest.display();
}

private:
class Nest {
friend class Hen;
public:
Nest(int i) : egg(i) {}
void display() {
cout << "I am a nest" << endl;
cout << egg.egg_data << endl;
egg.display();
}
private:
class Egg {
friend class Hen;
friend class Nest;
public:
Egg(int n) : egg_data(n) {}
void display() {
cout << "I am an egg" << endl;
}
private:
int egg_data;
};

Egg egg;
};
};

int main() {
Hen hen;
hen.display();

return 0;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯