构造函数与重载

//--构造函数--------------------------------------------------------

#include <iostream.h>

class Point 
{
public:
    int x;
    int y;
    Point() //构造函数
    {
        x=0;
        y=0;
    }
    Point(int a,int b) //多个构造函数(多个构造函数自动匹配,这就叫做“函数重载”)
    {
        x=a;
        y=b;
    }
    ~Point() //# 析构函数 # 释放构造函数占用的内存
    {
        
    }
    void output()
    {
        cout<<x<<endl<<y<<endl;
    }
};

void main()
{
    Point pt(6,9);    //函数重载匹配到有参数的构造函数
    pt.output();  //程序执行到此处会跳转到析构函数处(#号处)释放构造函数占用的内存
}

 不能重载的函数形式:

//----------------------

void output();

int output(); //只有返回值不同的不能重载

//----------------------

void output(int a,int b=3); //有常量作为参数的函数不能重载

void output(int a);

 

posted on 2015-06-08 23:22  Rohalloway  阅读(148)  评论(0编辑  收藏  举报

导航