IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> c++面向对象8.3 -> 正文阅读

[C++知识库]c++面向对象8.3

// 面向对象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* const
const 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;
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-04 11:00:48  更:2021-08-04 11:03:28 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/10 6:28:09-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码