码迷,mamicode.com
首页 > 其他好文 > 详细

构造函数与重载

时间:2015-06-09 00:44:22      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

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

#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);

 

构造函数与重载

标签:

原文地址:http://www.cnblogs.com/ROHALLOWAY/p/4562259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!