啪啪拉拉噼里啪啦

初学者天堂资料汇集

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  16 随笔 :: 73 文章 :: 16 评论 :: 0 Trackbacks

构造函数和拷贝构造函数的实例

#include<iostream.h>

class Point
{  public:                                          //共有数据  外部接口
     Point(int xx=0,int yy=0)          //构造函数
            { 
                X=xx;
                Y=yy;
           }
      Point(Point &P);                      // 拷贝构造函数
      int GetX()
        { return X;}
      int GetY()
         { retrun Y;}
 private:
    int X, int Y;            // 私用数据
};
// 成员函数的实现

Point ::Point(Point &P)
{  X=P.X;
   Y=P.Y;
  cout<<"拷贝构造函数被调用"<<endl;
}
void fun1(Point P)
{  cout<<P.GetX()<<endl;
}
void fun2()
{  Point A(1,2)
    retrun A;

}

void main()
{  Point A(4,5);
   Point B(A);
  cout<<B.GetX()<<endl;
  fun1(B);
B=fun2();
cout<<B.GetX()<<endl;
}

posted on 2005-04-01 17:56 噼里啪啦的世界 阅读(99) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: