- 论坛徽章:
- 1
|
本帖最后由 shihyu 于 2015-04-23 11:28 编辑
- #include <iostream>
- using namespace std;
- class PointDemo {
- public:
- friend class Point;
- PointDemo(int);
- ~PointDemo();
-
- void show();
- void show1();
- private:
- // Nested Class
- class Point {
- public:
- Point(PointDemo *p);
- Point(int, int);
- int x() { return _x; }
- int y() { return _y; }
- void x(int x) { _x = x; }
- void y(int y) { _y = y; }
- private:
- int _x;
- int _y;
- };
-
- Point **_points;
- int _length;
- };
- // 实作内部类别
- PointDemo::Point::Point(PointDemo *p) {
- p->show1();
- _x = 0;
- _y = 0;
- }
- // 实作内部类别
- PointDemo::Point::Point(int x, int y) {
- _x = x;
- _y = y;
- }
- PointDemo::PointDemo(int length) : _length(length) {
- _points = new Point*[_length];
- for(int i = 0; i < _length; i++) {
- _points[i] = new Point(this);
- _points[i]->x(i*5);
- _points[i]->y(i*5);
- }
- }
- void PointDemo::show() {
- for(int i = 0; i < _length; i++) {
- cout << "(x, y) = ("
- << _points[i]->x() << ", "
- << _points[i]->y() << ")"
- << endl;
- }
- }
- void PointDemo::show1() {
- cout << "PointDemo::show1" << endl;
- }
- PointDemo::~PointDemo() {
- for(int i = 0; i < _length; i++) {
- delete _points[i];
- }
- delete [] _points;
- }
- int main() {
- PointDemo demo(10);
- demo.show();
- return 0;
- }
复制代码 外部类对象传到内部类 |
|