CHAPTER 05:EX 04
2008-05-18 00:02
369 查看
/*
4. Write two classes, each of which has a member function
that takes a pointer to an object of the other class. Create
instances of both objects in main( ) and call the
aforementioned member function in each class.
*/
#include<iostream>
using namespace std;
//如果要让一个类里面的成员函数的参数是另一个类,那么必须要先申明另外一个类
//这样的声明让编译器先看到需要用到的类,以便完成非完全类型检验。
class B;
class A {
public:
//获取B类型对象,并返回那个B类型对象的地址
B* addrB(B*);
};
class B {
public:
//获取A类型对象,并返回那个A类型对象的地址
A* addrA(A*);
};
B* A::addrB(B* b) {
return b;
}
A* B::addrA(A* a) {
return a;
}
int main() {
B b;
A a;
cout << "B class b object's address: "
<< &b << endl;
cout << "A class a object's member function's result: "
<< a.addrB (&b) << endl;
cout << "A class a object's address: "
<< &a << endl;
cout << "B class b object's member function's result: "
<< b.addrA (&a) << endl;
}
4. Write two classes, each of which has a member function
that takes a pointer to an object of the other class. Create
instances of both objects in main( ) and call the
aforementioned member function in each class.
*/
#include<iostream>
using namespace std;
//如果要让一个类里面的成员函数的参数是另一个类,那么必须要先申明另外一个类
//这样的声明让编译器先看到需要用到的类,以便完成非完全类型检验。
class B;
class A {
public:
//获取B类型对象,并返回那个B类型对象的地址
B* addrB(B*);
};
class B {
public:
//获取A类型对象,并返回那个A类型对象的地址
A* addrA(A*);
};
B* A::addrB(B* b) {
return b;
}
A* B::addrA(A* a) {
return a;
}
int main() {
B b;
A a;
cout << "B class b object's address: "
<< &b << endl;
cout << "A class a object's member function's result: "
<< a.addrB (&b) << endl;
cout << "A class a object's address: "
<< &a << endl;
cout << "B class b object's member function's result: "
<< b.addrA (&a) << endl;
}
相关文章推荐
- CHAPTER 04:EX 07
- CHAPTER 04:EX 18
- CHAPTER 04:EX 24
- CHAPTER 04:EX 25
- CHAPTER 05:EX 05
- head first JavaScript chapter 04 & 05 & 06
- 第15讲:Chapter 04--Creating a Database
- Chapter 3-04
- Chapter 04 - Subqueries
- 数字图像处理实验(9):PROJECT 04-05,Correlation in the Frequency Domain
- 数字图像处理实验(13):PROJECT 05-04,Parametric Wiener Filter
- 【收藏】本周ASP.NET英文技术文章推荐[04/29 - 05/05]
- 08--04--05阶段 工作经验总结,,布局,复杂列表模型数据监听
- CHAPTER 10:EX 34
- mongoDB的读书笔记(04)_【Replica】(05)_初探Replica set副本集的搭建 By Test模式
- Posting only possible in periods 2009/05 and 2009/04 in company 1000
- 2017 ACM-ICPC 亚洲区(沈阳赛区)网络赛 - 02 04 05 08 12
- 04-05 mysql的学习笔记
- Chapter_04 数组和指针并不相同
- 初识设计模式 chapter 04-工厂模式