| // 面向对象8.3.cpp : 此文件包含 “main” 函数。程序执行将在此处开始并结束。//
 //
 //#include
 //
 //int main()
 //{
 // std::cout << “Hello World!\n”;
 //}
 //
 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
 调试程序: F5 或调试 >“开始调试”菜单
 //
 入门使用技巧:
 1. 使用解决方案资源管理器窗口添加/管理文件
 2. 使用团队资源管理器窗口连接到源代码管理
 3. 使用输出窗口查看生成输出和其他消息
 4. 使用错误列表窗口查看错误
 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
 日期类 实现日期类的操作
 //#include
 //using namespace std;
 //class Date
 //{
 //public:
 // Date(int y = 1, int m = 1, int d = 1)
 // {
 // //判断日期是否有效
 // if (_y <= 0 || m <= 0 || m > 12 || d <= 0 || d > getDay(y, m))
 // {
 // //日期无效
 // _y = 1;
 // _m = 1;
 // _d = 1;
 // //cout << “日期无效,设为默认值:1 - 1 - 1”<< endl;
 // }
 // else
 // {
 // _y = y;
 // _m = m;
 // _d = d;
 // }
 // }
 // int getDay(int y, int m)
 // {
 // static int days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 // int day = days[m];
 // //如果是2月,且为二月,修改天数
 // if (m == 2 && (((y % 4 == 0) && (y % 100 != 0)) || y % 400 == 0))
 // day += 1;
 // return day;
 // }
 //
 // //日期运算
 // //Date+=int int a,int b a+=b
 // //左操作数+=右操作数;左操作数的内容发生变化;返回值;相加之后的值
 // Date& operator+=(int day)
 // {
 // //判断日期是否为负数
 // if (day < 0)
 // return this -= -day;
 // //日期;2021 ,2,4
 // //day:1 30 90 360
 // //2021,2,4+1=2021,2,5 2021,2,4+30=2021,2,34 ----28----》2021…3.6
 // //2021,2,4+90=2021,2,94–28---->2021.3.66-31=2021,4,35- 30—>2021,5,5
 // //相加天数 首先是天数的更新
 // _d += day;
 // //判断天数是否溢出
 // while (_d > getDay(_y, _m))
 // {
 // //减去当月的天数
 // _d -= getDay(_y, _m);
 // ++_m;
 // //判断月份是否溢出
 // if (_m == 13)
 // {
 // //年份的进位
 // //下一年的1月份
 // //年份进位
 // ++_y;
 // _m = 1;
 // }
 // }
 // //相加之后的结果是可以直接返回this,因为这里是左操作数,还有就是这里是成员函数所以说是包含有this指针的操作。
 // return *this;
 // }
 // //Date + int
 // //加法:返回相加之后的结果
 // Date operator+(int day)
 // {
 // //重新创建一个新的,然后再就是返回值
 // //使用拷贝构造,使用新生成的对象进行返回
 // Date tmp = *this;
 // return tmp += day;
 //
 // //下边的也是可以的
 // Date copy(*this);
 // //Date copy = *this;
 // copy += day;
 // return copy;
 // }
 // //++Date
 // Date& operator++()
 // {
 // //使用的是引用 就是日期类对象本身
 // return *this += 1;
 // }
 // //后置++
 //
 // //Date++
 // Date operator++(int)
 // //这里使用的int只是为了和上边进行区分。为了构成函数的重载。
 // {
 // Date copy(*this);
 // *this += 1;
 // //返回++之前的值
 // return copy;
 //
 // }
 // //减等操作
 // Date& operator-=(int day)
 // {
 // //判断日期是否为负数
 // if (day < 0)
 // return *this += -day;
 //
 // //-=负数相当于加等与一个正数
 // //2021.2.5-1=2021.2.4
 // //2021.2.5-30=2021.2-25------>2021.2.-25+31=2021.1.6
 // //首先是更新天数
 // _d -= day;
 // while (_d <= 0)
 // {
 // //用上一个月的天数回补
 // --_m;
 // //判断月份是否溢出(向下)
 // if (_m == 0)
 // {
 // //需要回退到上一年的12月份
 // _y–;
 // _m = 12;
 // }
 // _d += getDay(_y, _m);
 // }
 // return *this;
 // }
 // //Date -int
 // Date operator-(int day)
 // //使用拷贝构造
 // {
 // //这种就是表示的是比较简洁的代码;
 // /*Date tmp = this;
 // return tmp += day;/
 //
 // Date copy(*this);
 // *this -= day;
 // return copy;
 // }
 前置和后置–表示的就是区别就是最后的参数后置加int。
 // //–Date
 // Date& operator–()
 // {
 // //使用的是引用 就是日期类对象本身
 // return *this -= 1;
 // }
 // //Date–
 // Date operator–(int)
 // {
 // Date copy(*this);
 // *this -= 1;
 // return copy;
 // }
 //
 // //==
 // bool operator==(const Date& d)
 // {
 // return _y == d._y
 // && _m == d._m
 // && _d == d._d;
 //
 // }
 //
 // //!=
 // bool operator!=(const Date& d)
 // {
 // //这里的this指针指向的就是左操作数的情况。
 // return !(*this == d);
 // }
 // //>
 // bool operator>(const Date& d)
 // {
 // //简单逻辑
 // if (_y > d._y)
 // return true;
 // else if (_y == d._y)
 // {
 // if (_m > d._m)
 // return true;
 // else if (_m == d._m)
 // {
 // if (_d > d._d)
 // return true;
 // }
 // }
 // return false;
 // }
 // //>=
 // bool operator>=(const Date& d)
 // {
 //
 // return *this > d || *this == d;
 //
 // }
 //
 // //<
 // //简单逻辑
 // bool operator<(const Date& d)
 // {
 // /if (_y < d._y)
 // return true;
 // else if (_y == d._y)
 // {
 // if (_m < d._m)
 // return true;
 // else if (_m == d._m)
 // {
 // if (_d < d._d)
 // return true;
 // }
 // }
 // return false;/
 // return !(*this >= d);
 // }
 //
 // //<=
 // bool operator<=(const Date& d)
 // {
 // return !(*this > d);
 //
 // }
 //
 // //日期相减 返回的是差值 Date-Date
 // int operator-(const Date& d)
 // {
 // //分情况 思路得是清楚的
 // //计算比较小的日期 经过多少次自加的运算 可以和比较大的日期相同
 // //自加的次数就是相差的天数
 // //这里使用的是拷贝构造
 // Date max = this;
 // Date min = d;
 // //flag表示的是两个到底是哪个大,要是this值大那就是flag=1;
 // int flag = 1;
 // if (max < min)
 // {
 // max = d;
 // min = *this;
 // flag = -1;
 // }
 // int day = 0;
 // while (min < max)
 // {
 // ++min;
 // ++day;
 // }
 // return flag * day;
 // }
 //
 //
 //private:
 // int _y;
 // int _m;
 // int _d;
 //};
 逻辑运算符的操作。
 void test()
 {
 Date d1(2021, 2, 4);
 Date d2(2021, 2, 4);
 Date d3(2021, 2, 4);
 Date d4(2021, 2, 4);
 //d1.operator+=(1);
 //d2 += 30;
 //d3 += 90;
 //d4 += 360;
 //d3 = d4 + 90;
 //d4 = ++d3;
 后置++
 //d4 = d3.operator++(10);
 前置
 //d4 = d3.operator++();
 
 d1 -= 1;
 d2 -= 30;
 d3 -= 90;
 d4 -= -360;
 
 d2 += -30;
 d3 += -90;
 d4 += -360;
 
 
 d3 = d4 - 30;
 d3 = --d4;
 d3 = d4–;
 }
 表示的是运算符的操作数
 void test1()
 {
 Date d1(2021, 2, 5);
 Date d2(2021, 2, 8);
 Date d3(2021, 2, 12);
 Date d4(2021, 3, 8);
 
 bool ret = d1 == d2;
 ret = d1 > d4;
 ret = d1 >= d4;
 ret =d1 < d3;
 ret = d1 <= d3;
 ret = d1 != d3;
 
 }
 这里表示的是日期的相减
 //void test1()
 //{
 // Date d1(2021, 2, 5);
 // Date d2 = d1 + 335;
 // int day = d1 - d2;
 //}
 //
 //int main()
 // {
 // //test();
 // test1();
 // return 0;
 // }
 const 成员函数//#include
 //using namespace std;
 //class A
 //{
 //public:
 // int setI(int i)
 // {
 // _i = i;
 // //非const成员函数中,可以调用const成员函数
 // }
 // //const 只修饰this指针;this指针指向的对象内容不能变
 // int getI() const //const this:const A* const—>现在表示的就是this指向是不发生变化,而且他所指向内容也是不能发生变化的。
 // {
 // //const成员函数中,不能调用非const成员函数
 // //setI(10);
 // return _i;
 // }
 //private:
 // int _i;
 //};
 //const之修饰成员函数,普通函数是没有const操作的。
 void fun1(int a)
 {
 a = 100;
 
 }
 void test()
 {
 //
 A a;
 a.setI(10);
 a.getI();
 //
 //const A b; //只看权限的情况
 const 对象可以调用非const成员函数 不行 因为const权限小于非const,但是可以调用const函数。
 //b.setI(10);
 //b.getI();
 
 }
 //
 //void test()
 //{
 // int a = 10;
 // //pa:指向和内容都可以改
 // int* pa = &a;
 // //cpa:内容可以改,指向不能改
 // int* const cpa = &a;
 // //ccpa:指向和内容都不能改
 // const int* const ccpa = &a;
 //
 // int b;
 // pa = &b;
 // *pa = 100;
 //
 // //cpa = &b;
 // //*cpa = 1000;
 //
 // //ccpa = &b;
 // //*ccpa = 10000;
 //}
 //int main()
 //{
 // test();
 // return 0;
 //}
 //const 取地址#include
 using namespace std;
 class A
 {
 public:
 int setI(int i)
 {
 _i = i;
 //非const成员函数中,可以调用const成员函数
 }
 //const 只修饰this指针;this指针指向的对象内容不能变
 int getI() const //const this:const A* const—>现在表示的就是this指向是不发生变化,而且他所指向内容也是不能发生变化的。
 {
 //const成员函数中,不能调用非const成员函数
 //setI(10);
 return _i;
 }
 //由于是单目运算符,就是一个参数,由于是成员函数,那么就是成员函数中是没有参数,就是只有一个参数是this
 //这里也就是取地址运算符的操作 因为是自定义类型,和之前的运算符重载是一样的,就是也必须进行取地址运算符的重载。
 //this:A* const
 A* operator& ()
 {
 	return  this;
}
 //this:const A* constconst A* operator& () const
 {
 	return  this;
}
 private:int _i;
 };
 void test()
 {
 A obja;
 //取地址运算符重载函数
 A* pa = &obja;
 //就是表示的是指着指向setI函数,就是调用setI函数。
 pa->setI(10);
 const A objb;
const A* pb = &objb;
 }int main()
 {
 test();
 return 0;
 }
 |