构造函数与重载

本文介绍C++中构造函数的基本概念,包括构造函数的定义、使用及如何实现函数重载。通过具体实例展示了不同构造函数的作用以及析构函数的功能。
//--构造函数--------------------------------------------------------

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

 

转载于:https://www.cnblogs.com/ROHALLOWAY/p/4562259.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值