#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;
}
c++day06
最新推荐文章于 2024-07-24 20:55:30 发布