c++day06

#include <iostream>

using namespace std;

/********************对象调const  *************************************/

class Date
{
public:
    Date (int year =0,int month = 1,int day =1){
        _year =year;

        _month=month;
        _day=day;
    }

    /*
     * const Date* p1   *p1指向的内容
     * Date const *p2   *p2
     * Date* const p3   p3 指针的本身
    */

    //const 修饰成员函数
    void Print()const{ // ->void Print(const Date* this)
        cout<<_year<<" "<<_month<<" "<<_day<<endl;

    }


    void f1()
    {

    }

    void f2()
    {

    }
private:
    int _year;
    int _month;
    int _day;
};

void f(const Date& d)
{
    d.Print();
}

void test01()
{
    Date d(1,1,1);
    f(d);
}
/*********************************************************/


/*********************成员函数调用const ************************************/

class Date1
{
public:
    Date1 (int year =0,int month = 1,int day =1){
        _year =year;

        _month=month;
        _day=day;
    }

    /*
     * const Date* p1   *p1指向的内容
     * Date const *p2   *p2
     * Date* const p3   p3 指针的本身
    */

    //const 修饰成员函数
    void Print()const{ // ->void Print(const Date* this)
        cout<<_year<<" "<<_month<<" "<<_day<<endl;

    }


    void f1()
    {
        f2(); //权限的缩小
    }

    void f2() const
    {

    }

    void f3()
    {

    }

    Date1* operator&()
    {
        cout<<"operator&"<<endl;
        return  this;
    }

    const Date1* operator&() const
    {

        cout<<"const opreator&()"<<endl;

        return  this;
    }

    void f4() const//void f4(const Date* this)
    {
        //f3() this ->f(this) 权限的放大
    }
private:
    int _year;
    int _month;
    int _day;
};

void test02()
{
    const Date1 d1;
    Date1 d2;
    cout<<&d1<<endl;
    cout<<&d2<<endl;
}
/**************************************************************/


/**************************************************************/

//为什么有初始化列表.初始化列表是对象的成员变量的定义地方
class Date2
{
public:

    //初始化列表
    Date2 (int year =0,int month = 1,int day =1):
        _year (year),_month(month),_day(day)
    {

    }


private:
    int _year;
    int _month;
    int _day;
};
/*******************************************************************/


/*********************隐式类型的转换**********************************************/
class Date3{
private:
    int _year;
    int _month;
    int _day;
public:
    //explict用来进行拒绝饮食类型转换
    Date3(int year):_year(year)
    {

    }
};


void test04()
{
    Date3 d1(1);//构造
    Date3 d2=2;// 隐式类型的转换 构造出temp(2) +在用tmp构造拷贝d2(tmp)+优化
    Date3 d3=d1;//构造拷贝
    int i =1;
    const double& d =i; //隐式类型转换,会产生中间的临时变量

}



/*******************************************************************/


/*******************************************************************/
int n  = 0;
class A
{
public:
    A()
    {
        ++n;
        cout<<this<<endl;
    }

    A(const  A& a)
    {
        ++n;
         cout<<this<<endl;
    }
};

A f1(A a)
{
    cout<<"f1"<<endl;
    return a;
}


void test05()
{
    A a1;
    A a2;
    f1(a1);
    cout<<n<<endl;
    return ;
}
/*******************************************************************/




int main()
{
   test05();
    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值