当前位置: 首页 > news >正文

做演讲和做演讲视频网站php主做哪种类型网站

做演讲和做演讲视频网站,php主做哪种类型网站,WordPress怎么去掉底部,网站的营销与推广类的默认成员函数 类的六个默认成员函数构造函数构造函数的概念构造函数的特性 析构函数析构函数的概念析构函数的特性 构造函数与析构函数的调用顺序拷贝构造拷贝构造的概念拷贝构造的特性赋值运算符重载运算符重载赋值运算符重载前置与后置重载输入输出流重载 const修饰成员实… 类的默认成员函数 类的六个默认成员函数构造函数构造函数的概念构造函数的特性 析构函数析构函数的概念析构函数的特性 构造函数与析构函数的调用顺序拷贝构造拷贝构造的概念拷贝构造的特性赋值运算符重载运算符重载赋值运算符重载前置与后置重载输入输出流重载 const修饰成员实现完整的日期系统取地址操作符重载const取地址操作符重载 类的六个默认成员函数 当一个类中什么成员都没有时被称为空类。 空类即任何类在什么都不写时编译器会自动生成6个默认成员函数。 默认成员函数用户没有显式实现编译器会自动生成的成员函数称为默认成员函数。 #mermaid-svg-FG2kqgaz2nqbBzjR {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-FG2kqgaz2nqbBzjR .error-icon{fill:#552222;}#mermaid-svg-FG2kqgaz2nqbBzjR .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-FG2kqgaz2nqbBzjR .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-FG2kqgaz2nqbBzjR .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-FG2kqgaz2nqbBzjR .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-FG2kqgaz2nqbBzjR .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-FG2kqgaz2nqbBzjR .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-FG2kqgaz2nqbBzjR .marker{fill:#333333;stroke:#333333;}#mermaid-svg-FG2kqgaz2nqbBzjR .marker.cross{stroke:#333333;}#mermaid-svg-FG2kqgaz2nqbBzjR svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-FG2kqgaz2nqbBzjR .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-FG2kqgaz2nqbBzjR .cluster-label text{fill:#333;}#mermaid-svg-FG2kqgaz2nqbBzjR .cluster-label span{color:#333;}#mermaid-svg-FG2kqgaz2nqbBzjR .label text,#mermaid-svg-FG2kqgaz2nqbBzjR span{fill:#333;color:#333;}#mermaid-svg-FG2kqgaz2nqbBzjR .node rect,#mermaid-svg-FG2kqgaz2nqbBzjR .node circle,#mermaid-svg-FG2kqgaz2nqbBzjR .node ellipse,#mermaid-svg-FG2kqgaz2nqbBzjR .node polygon,#mermaid-svg-FG2kqgaz2nqbBzjR .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-FG2kqgaz2nqbBzjR .node .label{text-align:center;}#mermaid-svg-FG2kqgaz2nqbBzjR .node.clickable{cursor:pointer;}#mermaid-svg-FG2kqgaz2nqbBzjR .arrowheadPath{fill:#333333;}#mermaid-svg-FG2kqgaz2nqbBzjR .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-FG2kqgaz2nqbBzjR .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-FG2kqgaz2nqbBzjR .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-FG2kqgaz2nqbBzjR .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-FG2kqgaz2nqbBzjR .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-FG2kqgaz2nqbBzjR .cluster text{fill:#333;}#mermaid-svg-FG2kqgaz2nqbBzjR .cluster span{color:#333;}#mermaid-svg-FG2kqgaz2nqbBzjR div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-FG2kqgaz2nqbBzjR :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 默认成员函数 初始化和清理 构造函数主要完成初始化工作 析构函数主要完成清理工作 拷贝赋值 拷贝构造是使用同类对象初始化创建对象 赋值重载主要是把一个对象赋值给另一个对象 取地址重载 主要是普通对象和const对象取地址,这俩个很少会自己实现 构造函数 构造函数的概念 class Data { public:void Init(int year 2000, int month 1, int day 1){_year year;_month month;_day day;}void Print(){cout _year / _month / _day endl;} private:int _year;int _month;int _day; };int main(void) {Data d1;d1.Init();d1.Print();Data d2;d2.Init(2024,6,8);d2.Print();return 0; }每次创建代码时都需有初始化代码使得工作量加大引入构造函数可以进行初始化工作。 构造函数是一个特殊的成员函数名字与类名相同创建类类型对象时由编译器自动调用以保证每个数据成员都有一个合适的初始值并且在对象的整个生命周期内只使用一次。 构造函数的特性 构造函数是特殊的成员函数需要注意的是构造函数虽然名称叫构造但是构造函数的主要任务并不是开空间创建对象而是初始化对象。 其特征如下 1.函数名与类名相同 2.无返回值也不需要写void 3.对象实例化时编译器会自动调用对应的构造函数 class Data { public:Data(int year 2000, int month 1, int day 1){_year year;_month month;_day day;} private:int _year;int _month;int _day; };int main(void) {Data d1();return 0; } 4.构造函数可以重载 class Data { public://带参数的构造函数Data(int year, int month, int day){_year year;_month month;_day day;}//不带参数的构造函数Data(){_year 2000;_month 1;_day 1;} private:int _year;int _month;int _day; };int main(void) {//调用带参数的构造函数Data d1(2024,6,8);//调用不带参数的构造函数Data d2;return 0; }构造函数支持重载的原因是可以存在不同的初始化情况。 【注意】如果公共无参构造函数创建对象时对象后面不用接括号否则会变成函数声明。 5.如果类中没有显式定义构造函数则C编译器会自动生成一个无参的默认构造函数一旦用户定义编译器将不再生成。 class Data { public:void Print(){cout _year / _month / _day endl;} private:int _year;int _month;int _day; };int main(void) {Data d1;d1.Print();return 0; }编译器默认初始化成随机值 这里需要说明一下 C里将类型分成俩类 1.内置类型 内置类型属于基本类型是语言本身定义的基础类型例如int、char、double、指针等等 2.自定义类型 用struct、calss等定义的类型 编译器自动默认生成构造函数内置类型不做处理自定义类型会去调用其默认构造。这里需要注意的是不同的编译器有不同的处理方法有些编译器也会处理但是至少部分编译器个性化处理不是所有的编译器都会处理 总结 1.一般情况下有内置类型成员的就需要自己写构造函数不能让编译器自己生成 2.全部都是自定义类型成员可以考虑让编译器自己生成。一个经典的算法题使用俩个栈实现队列就可以使用默认构造函数 6.C11中在成员声明的时候可以给缺省值 class Data { public:void Print(){cout _year / _month / _day endl;} private:int _year 2000;int _month 1;int _day 1; };【注意】这里不是初始化 由于这里只能存在成员变量的声明没有开空间这里给的是默认的缺省值给编译器生成默认构造函数用。 7.无参的构造函数和全缺省的构造函数都称为默认构造函数、并且默认构造函数只能有一个。 【注意】无参构造函数、全缺省默认构造函数、如果我们自己没写编译器默认生成的构造函数都可以认为是默认构造函数。 class Data { public://无参构造函数Data(){_year 2000;_month 1;_day 1;}//全参构造函数Data(int year 2000,int month 1, int day 1){_year year;_month month;_day day;} private:int _year;int _month;int _day; }; int main(void) {Data d1;return 0; }不传参就可以调用的就是默认构造函数。 析构函数 析构函数的概念 析构函数是特殊的成员函数析构函数与构造函数的功能相反析构函数不是完全对对象本身的销毁局部对象销毁工作时编译器完成的而对象在销毁时会自动调用析构函数完成对象中资源的清理工作。 析构函数的特性 1.析构函数名实在类名前面加上字符~ 2.无参数无返回值 class Data { public:Data(int year 2000, int month 1, int day 1){this-_year year;this-_month month;this-_day day;}~Data(){_year 0;_month 0;_day 0;} private:int _year;int _month;int _day; };3.一个类只能有一个析构函数若未显式定义系统会自动生成默认的析构函数。【注意】析构函数不能重载。 4。对象声明周期结束时C编译器系统自动调用析构函数。 #includeiostream using namespace std; class Data { public:Data(int year 2000, int month 1, int day 1){cout Data endl;this-_year year;this-_month month;this-_day day;}~Data(){cout ~Data endl;_year 0;_month 0;_day 0;} private:int _year;int _month;int _day; }; int main(void) {Data d1;return 0; }5.系统自动生成的默认构造函数 (1).内置类型成员不做处理。 (2).自定义类型会去调用他的析构函数。 下面这段代码是本人实现的 class Stack { public:Stack(int capacity 4){_arr (int*)malloc(sizeof(int) * capacity);if (_arr nullptr){perror(malloc fail);return;}_capacity capacity;_size 1;}~Stack(){free(_arr);_arr nullptr;_size 1;_capacity 0;}void Push(int x){if (this-_capacity this-_size){int* arr (int*)realloc(this-_arr, sizeof(int) * this-_capacity * 2);if (arr nullptr){perror(realloc fail);return;}this-_arr arr;this-_capacity * 2;}this-_arr[this-_size - 1] x;this-_size;}void Pop(){this-_arr[this-_size - 1] 0;this-_size--;} private:int* _arr;int _size;int _capacity; }; int main(void) {Stack s1;s1.Push(1);s1.Push(2);s1.Push(3);s1.Push(4);s1.Pop();return 0; }这里可以发现手动实现的析构函数可以根据自己的要求满足实现。 class Stack { public:Stack(int capacity 4){_arr (int*)malloc(sizeof(int) * capacity);if (_arr nullptr){perror(malloc fail);return;}_capacity capacity;_size 1;}void Push(int x){if (this-_capacity this-_size){int* arr (int*)realloc(this-_arr, sizeof(int) * this-_capacity * 2);if (arr nullptr){perror(realloc fail);return;}this-_arr arr;this-_capacity * 2;}this-_arr[this-_size - 1] x;this-_size;}void Pop(){this-_arr[this-_size - 1] 0;this-_size--;} private:int* _arr;int _size;int _capacity; }; int main(void) {Stack s1;s1.Push(1);s1.Push(2);s1.Push(3);s1.Push(4);s1.Pop();return 0; }所以一般情况下如果没有动态内存申请析构函数可以不写例如Data类但是如果有动态内存申请就需要显式写析构函数释放资源否则会造成内存泄漏。例如栈的实现。 构造函数与析构函数的调用顺序 类的析构函数调用一般按照构造函数调用的相反顺序调用但是需要注意static对象的存在因为static改变了对象的生存作用域需要等待程序结束后才可析构释放对象。 全局对象先于局部对象进行构造。 局部对象按照顺序进行构造无论是否为static对象。 static修饰的对象会在局部对象析构后进行析构。 拷贝构造 拷贝构造的概念 创建对象时需要创建一个与已存在对象一模一样的新对象就需要用到拷贝构造。 拷贝构造只有单个形参该形参是对本类型对象的引用一般常用const修饰在用已存在的类型对象创建新对象时由编译器自动调用。 拷贝构造的特性 拷贝构造函数也是特殊的成员函数其特征如下 1.拷贝构造函数时构造函数的一个重载形式所有书写格式与构造函数类似但是参数类型不同。 2.拷贝构造函数的参数只有一个且必须是类类型对象的引用使用传值方式编译器之间报错因为会引发无穷递归调用。 观察下面代码 class Data { public:Data(int year 2000, int month 1, int day 1){cout Data endl;this-_year year;this-_month month;this-_day day;}~Data(){cout ~Data endl;_year 0;_month 0;_day 0;}//拷贝构造函数//传值Data(Data d) {_year d._year;_month d._month;_day d._day;} private:int _year;int _month;int _day; }; int main(void) {Data d1;Data d2(d1);return 0; }首先编译器会自动报错原因是 当我们以传值方式传参时需要先建立一个临时拷贝而在建立临时拷贝也需要传参再以传值方式传参时又需要建立一个临时拷贝… 这种无穷递归编译器会强制检查解决的办法有俩种 1.使用指针内置类型 2.使用引用推荐 【注意】使用引用时需要注意赋值方向d._year _year;就是错误代码。 由于传引用的值不用改变所有可以使用const缩小权限。 正确代码如下 class Data { public:Data(int year 2000, int month 1, int day 1){this-_year year;this-_month month;this-_day day;}~Data(){_year 0;_month 0;_day 0;}//拷贝构造函数//传值Data(const Data d){_year d._year;_month d._month;_day d._day;} private:int _year;int _month;int _day; }; int main(void) {Data d1;Data d2(d1);return 0; }3.如果没有显式定义编译器会生成默认的拷贝构造函数默认的拷贝构造函数对象按内存存储按字节序完成拷贝这种拷贝叫浅拷贝或者值拷贝。 1内置类型成员完成值拷贝、浅拷贝。 2自定义类型成员会调用他的拷贝构造。 观察代码 class Stack { public:Stack(int capacity 4){_arr (int*)malloc(sizeof(int) * capacity);if (_arr nullptr){perror(malloc fail);return;}_capacity capacity;_size 1;}void Push(int x){if (this-_capacity this-_size){int* arr (int*)realloc(this-_arr, sizeof(int) * this-_capacity * 2);if (arr nullptr){perror(realloc fail);return;}this-_arr arr;this-_capacity * 2;}this-_arr[this-_size - 1] x;this-_size;}void Pop(){this-_arr[this-_size - 1] 0;this-_size--;} private:int* _arr;int _size;int _capacity; }; int main(void) {Stack s1;s1.Push(1);s1.Push(2);s1.Push(3);s1.Push(4);s1.Pop();Stack s2(s1);return 0; }以栈为例栈不可以使用默认拷贝构造栈默认生成的拷贝构造会将俩个指针指向同一个栈在析构时后面拷贝的指针会先析构而前面被拷贝的指针会后析构一个堆区被析构俩次编译器会报错同时如果修改其中一个值会影响另一个。 栈拷贝构造的正确代码是 Stack(const Stack s){_arr (int*)malloc(sizeof(int) * s._capacity);if (_arr nullptr){perror(malloc fail);return;}memcpy(_arr, s._arr, sizeof(int) * s._size);_size s._size;_capacity s._capacity;}需要动态开辟的都需要自己实现深拷贝而像日期类可以不写拷贝构造默认生成的拷贝构造就可以用。 赋值运算符重载 运算符重载 C为了增强代码的可读性引入了运算符重载运算符重载是具有特殊函数名的函数也具有返回值类型函数名字以及参数列表其返回值类型与参数列表与普通函数类似。 函数名字为关键字operator后面接需要重载的运算符符号。 函数原型返回值类型 operator操作符(参数列表) 内置类型可以通过编译器计算而自定义类型也可以向内置类型一样需要通过运算符重载进行加、减、比较等。 以Date日期类举例是否使用重载运算符是观察这个运算符对这个类是否有意义例如日期相减可以计算相差天数而日期相加却没有意义。 下面演示日期比较大小 class Date { public://Date构造Date(int year 2000,int month 1, int day 1){_year year;_month month;_day day;}//Date默认析构默认拷贝// //private:int _year;int _month;int _day; };bool operator(const Date d1, const Date d2) {if (d1._year d2._year){return true;}else if (d1._year d2._year d1._month d2._month){return true;}else if (d1._year d2._year d1._month d2._month d1._day d2._day){return true;}return false; }int main(void) {Date d1(2024, 6, 10);Date d2(2024, 5, 20);if (d1 d2){cout d1 d2 endl;}else{cout d1 d2 endl;}return 0; }这里需要注意的是编译器在编译期间将d1d2转换成operator(d1,d2)所以将d1d2写成operator(d1,d1)是相同的第二种方式相当于调用函数。 int main(void) {Date d1(2024, 6, 10);Date d2(2024, 5, 20);//第一种方式if (d1 d2){cout d1 d2 endl;}else{cout d1 d2 endl;}//第二种方式if (operator(d1,d2)){cout d1 d2 endl;}else{cout d1 d2 endl;}return 0; }在反汇编的角度观察 本质上二者都是相同的编译器在编译期间将第一种方式转换为第二种方式然后进行call 但是观察刚才的代码如果在类中将成员变量改为私有那么编译器会报错在类外的函数是无法访问私有private和保护protected解决的办法有俩种1.使用友元建议能不用就不用友元会破坏封装2放在类内将其视为成员函数。 下面演示将运算符重载视为成员函数 class Date { public://Date构造Date(int year 2000, int month 1, int day 1){_year year;_month month;_day day;}//Date默认析构默认拷贝//比较大小的运算符重载bool operator(const Date d){if (_year d._year){return true;}else if (_year d._year _month d._month){return true;}else if (_year d._year _month d._month _day d._day){return true;}return false;} private:int _year;int _month;int _day; };int main(void) {Date d1(2024, 6, 10);Date d2(2024, 5, 20);//第一种方式if (d1 d2){cout d1 d2 endl;}else{cout d1 d2 endl;}//第二种方式if (d1.operator(d2)){cout d1 d2 endl;}else{cout d1 d2 endl;}return 0; }将运算符重载函数放入类内时参数只有一个另外一个时隐藏的this。同时d1d2准换为函数为d1.operator(d2)和使用成员函数时相同。 值得注意的是写出成员函数更加方便也更加容易理解。 【注意】 1.不能通过连接其他符号来创建新的操作符例如operator 2.重载操作符必须有一个类类型参数 3.用于内置类型的运算符其含义不能改变例如内置的整数不能改变其含义 4.作为类成员函数重载时其形参看起来比操作数少1因为成员函数的第一个参数为隐藏的this 5. “.*” “::” “sizeof” “?:” .注意以上五个运算符不能重载。 6.操作符时几个操作数重载函数就有几个参数。 赋值运算符重载 如何在类中实现一个赋值运算符重载 //实现赋值运算符重载void operator(const Date d){_year d._year;_month d._month;_day d._day;}在这里可以发现赋值运算符重载与拷贝构造类似但是值得注意的是二者有本质上的区别我们需要理一理二者的概念 拷贝构造函数函数用一个已经存在的对象初始化另一个对象 运算符重载函数已经存在的俩个对象之间的复制拷贝 通过理解二者的概念那么通过哪种方式调用呢 int main(void) {//调用拷贝构造函数Date d1(2024, 6, 10);Date d2 d1;//调用运算符重载函数Date d3(2024, 5, 20);Date d4(2003, 2, 3);d3 d4;return 0; }初步认识赋值运算符函数那么赋值运算符是如何使用的呢 int i , j , k;i j k 10;赋值运算符可以进行连续赋值 所以我们可以将代码进一步更改 //实现赋值运算符重载Date operator(const Date d){_year d._year;_month d._month;_day d._day;return *this;}this指针不可以再形参出现但是可以在成员函数内部出现。 但是在使用Date作为返回值时每次赋值都会调用拷贝构造所以建议直接使用引用返回。 代码如下 Date operator(const Date d){_year d._year;_month d._month;_day d._day;return *this;}因为传值返回值在函数结束后会销毁返回的是值的拷贝而传引用返回返回的是别名是*this这个指针在未被销毁时所在地址的值的别名。 this是参数也是进行传参的this的生命周期实在函数调用结束后销毁相当于在函数建立时先进行push压栈然后函数在结束后销毁this也会被pop虽然this这个指针被销毁了但是this这个指针所指向的值没有被销毁。 会不会存在一种情况是自己给自己赋值 d1 d2;所以为了防止这种情况需要对代码进行更改 //实现赋值运算符重载Date operator(const Date d){if (this ! d){_year d._year;_month d._month;_day d._day;}return *this;}总结 1.赋值运算符重载格式 参数类型const T 传递引用可以提高传参效率返回值类型T 返回引用可以提高返回的效率有返回值的目的是为了支持连续赋值检查是否自己给自己赋值返回*this要符合连续赋值的含义 2.赋值运算符只能重载成类的成员函数不能重载成全局函数因为类中存在默认赋值运算符重载会与之冲突。 3.用户没有显式实现时编译器会生成一个默认赋值运算符重载以值得方式逐字节拷贝。 【注意】默认生成赋值重载跟拷贝构造行为一样内置类型成员仅值拷贝浅拷贝自定义类型成员会去调用其赋值重载。 例如Date日期类、Myqueue不需要自己实现赋值重载stack需要自己实现因为默认生成的是浅拷贝。 前置与后置重载 //前置Date operator(){_day _day 1;return *this;}【注意】使用前置时需要注意前置时返回以后的对象。 //后置Date operator(int){Date tmp(*this);_day _day 1;return tmp;}前置与后置都是一元运算符为了让让前置与后置形参正确重载C规定后置重载时多增加一个int类型的参数但调用函数时该参数不用传递编译器会自动传递。 【注意】使用后置时需要注意后置返回以前的对象所以需要tmp保存一份tmp是临时变量返回时必须需要进行拷贝构造。 输入输出流重载 int i 10;printf(%d, i);我们知道printf可以打印内置类型但是不可以打印自定义类型。 如果不使用调用函数的方式是如何实现打印自定义函数呢 在C中支持使用流插入流提取运算符重载的方式进行打印自定义类型。 在Creference中介绍cout是ostream类型的对象cin是istream类型的对象。 在C中内置类型是初创C时使用流插入和流提取定义的成员函数。 运算符重载是为了让自定义类型支持运算符 1.可以直接支持内置类型的是库里实现的 2.可以直接支持自定义识别类型是因为函数重载 实现流插入自定义 //自定义流插入 void Date::operator(ostream out) {out _year 年 _month 月 _day 日 endl; }void TestDate2() {int i 10;cout i endl;Date d1(2024,6,12);d1 cout; }从我们初步实现流插入可以发现我们所实现的运算符不符合运算重载。 cout d1;由于cout是终端、控制台要实现只能在库里实现流插入不能写成成员函数因为Date类对象默认占用第一个参数就是主操作数这种写法写出来只能是d1cout但是这不符合使用习惯。 而全局函数不会占用第一个参数没有默认参数但是访问不了私有问题解决办法有俩个 1.写成公有的成员函数 class Date { public://公有的成员函数int GetYear(){return _year;}int GetMonth(){return _month;}int GetDay(){return _day;} private:int _year;int _month;int _day; }; //全局的流插入操作符 void operator(ostream out, Date d) {out d.GetYear() 年 d.GetMonth() 月 d.GetDay() 日 endl; }2.使用友元函数后续讲解 class Date { public://友元函数声明friend void operator(ostream out, Date d); private:int _year;int _month;int _day; }; //全局的流插入操作符 void operator(ostream out, Date d) {out d._year 年 d._month 月 d._day 日 endl; }这里使用友元函数比调用公有的函数方便一些。 在实际使用流插入的时候可以进行连续插入 cout d1 d2 d3;则需要更换返回值来进行连续插入 //全局的流插入操作符 ostream operator(ostream out, Date d) {out d._year 年 d._month 月 d._day 日 endl;return out; }同时ostream对象可以使用const修饰因为其在流入控制台过程中没有被修改。 最后整合一下流提取运算符重载函数 class Date { public://友元函数声明friend ostream operator(ostream out, const Date d); private:int _year;int _month;int _day; }; //全局的流插入操作符 ostream operator(ostream out, const Date d) {out d._year 年 d._month 月 d._day 日 ;return out; }通过对流插入的了解我们可以试着写出流提取运算符重载 class Date { public://友元函数声明friend istream operator(istream in, Date d); private:int _year;int _month;int _day; }; //全局的流提取操作符 istream operator(istream in, Date d) {in d._year d._month d._day;return in; }值得注意的是流提取是的参数是不可以加const的。istream不使用const修饰的原因是当我们从输入流中读取数据时流的状态会发生变化例如读取位置会向前移动对象不使用const修饰的原因是输入流是可变的、输入流应该允许被修改。 总的来讲流是支持任何形式的输入与输出。 const修饰成员 Date d1(2022,2,2);d1.print();const Date.d2(2011,10,10);d2.print();假设d1与d2俩个对象都调用print成员函数时 void print();d1可以调用成功而d2却调用不成功。 d1在调用print成员函数时Date的权限平移而d2在调用print成员函数时Date的权限被放大由起初的Date的不可被修改变成可修改时不被允许的。 那么如何可以更改呢 void print(const Date* this);如果可以这样写就可以实现d1调用成员函数但是this指针是不能作为形参出现的。 所以在C中对此做了新的说明 将const修饰的成员函数称之为const成员函数const修饰类成员函数实际修饰该成员函数隐含的this指针表明在该成员函数中不能对类的任何成员进行修改。 void print() const;const修饰的是*this如果成员函数加上const普通与const对象都可以调用但并不是所有的成员函数都可以加上const修饰要修改成员变量的成员函数不可以加只要成员函数内部不修改成员变量都应该加const这样const对象和普通对象都可以调用。 实现完整的日期系统 Date.h #pragma once #includeiostream #includeassert.h using namespace std;class Date { public://全缺省的构造函数Date(int year 2000, int month 1, int day 1);//析构函数~Date();//拷贝构造函数Date(const Date d);//赋值运算符重载Date operator(const Date d);//打印void Print() const{cout _year / _month / _day endl;}//获取某年某月的天数int GetMonthDay(int year, int month) const;//日期天数Date operator(int day);//日期天数Date operator(const int day) const;//日期-天数Date operator-(int day);//日期-天数Date operator-(const int day) const;//前置Date operator();//后置Date operator(int);//前置--Date operator--();//后置--Date operator--(int);//运算符重载bool operator(const Date d) const;//运算符重载bool operator(const Date d) const;//运算符重载bool operator(const Date d) const;//运算符重载bool operator(const Date d) const;//运算符重载bool operator(const Date d) const;//日期-日期int operator-(Date d) const;//友元函数声明friend ostream operator(ostream out, const Date d);//全局的流提取操作符friend istream operator(istream in, Date d); private:int _year;int _month;int _day; };//全局的流插入操作符 ostream operator(ostream out, const Date d); //全局的流提取操作符 istream operator(istream in, Date d); Date.cpp #includeDate.h//全缺省的构造函数 Date::Date(int year, int month, int day) {if (month 0 month 13 day 0 day GetMonthDay(year, month)){_year year;_month month;_day day;}else{cout 日期输入故障 endl;assert(false);} } //析构函数 Date::~Date() {_year 0;_month 0;_day 0; } //拷贝构造函数 Date::Date(const Date d) {_year d._year;_month d._month;_day d._day; } //赋值运算符重载 Date Date::operator(const Date d) {if(this ! d){_year d._year;_month d._month;_day d._day;}return *this; } //获取某年某月的天数 int Date::GetMonthDay(int year, int month) const {int MonthArr[13] { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if (month 2 ((year % 4 0) (year % 100 ! 0)) || (year % 400 0)){return 29;}return MonthArr[month]; } //日期天数 Date Date::operator(int day) {if (day 0){day -day;*this - day;day 0;}_day day;while (_day GetMonthDay(_year, _month)){_day - GetMonthDay(_year, _month);_month;if (_month 12){_year;_month 1;}}return *this; } //日期天数 Date Date::operator(int day) const {Date tmp *this;tmp day;return tmp; } //日期-天数 Date Date::operator-(int day) {if (day 0){day -day;*this day;day 0;}_day - day;while (_day 0){--_month;if (_month 1){--_year;_month 12;}_day GetMonthDay(_year, _month);}return *this; } //日期-天数 Date Date::operator-(int day) const {Date tmp *this;tmp - day;return tmp; }//前置 Date Date::operator() {*this 1;return *this; } //后置 Date Date::operator(int) {Date tmp *this;*this 1;return tmp; } //前置-- Date Date::operator--() {*this - 1;return *this; } //后置-- Date Date::operator--(int) {Date tmp *this;*this - 1;return tmp; } //运算符重载 bool Date::operator(const Date d) const {if (_year d._year){return true;}else if (_year d._year _month d._month){return true;}else if (_year d._year _month d._month _day d._day){return true;}return false; } //运算符重载 bool Date::operator(const Date d) const {if (_year d._year _month d._month _day d._day){return true;}return false; } //运算符重载 bool Date::operator(const Date d) const {if (*this d || *this d){return true;}return false; } //运算符重载 bool Date::operator(const Date d) const {if (!(*this d)){return true;}return false; } //运算符重载 bool Date::operator(const Date d) const {if (!(*this d)){return true;}return false; }//日期-日期 int Date::operator-(Date d) const {Date max *this;Date min d;int flag 1;if (*this d){max d;min 1;flag -1;}int n 0;while (min max){min;n;}return n * flag; }//全局的流插入操作符 ostream operator(ostream out, const Date d) {out d._year 年 d._month 月 d._day 日 ;return out; } //全局的流提取操作符 istream operator(istream in, Date d) {int year d._year;int month d._month;int day d._day;if (month 0 month 13 day 0 day d.GetMonthDay(year, month)){in year month day;}else{cout 日期输入故障 endl;assert(false);}return in; }取地址操作符重载 class Date { public:Date* operator(){return this;} private:int _year;int _month;int _day; };const取地址操作符重载 class Date { public:const Date* operator() const{return this;} private:int _year;int _month;int _day; };取地址与const取地址操作符一般不需要重载使用编译器默认取地址重载即可只有存在默认情况才才需要重载。
http://www.dnsts.com.cn/news/145610.html

相关文章:

  • 燕郊网站制作多少钱用dw做的网页如何上传到网站
  • 做做网站入口怎么注册国外域名
  • 在线做数据图的网站有哪些免费建站平台排行榜
  • 免费的自建视频网站wordpress版权文字
  • 汇鑫网站建设方便手机提取网页视频app
  • 广州做网站的网络公司排名合川集团网站建设
  • 央企直招出国劳务网站上海网站建设找哪家
  • 微网站的案例学互联网需要什么学历
  • 海洋网站建设网络wordpress 谷歌登陆不了
  • 微信平台可以做微网站吗德阳百度网站建设
  • 免费学校网站模板html汉化主题做网站
  • 爱站工具包的主要功能wordpress评论邮件
  • 怎么用源码做网站视频可爱风格网站
  • 网站开发php是什么意思整站排名服务
  • 电子商务网站栏目wordpress导航制作
  • 网站建设与维护考试网页设计与制作课程代码
  • 好的专题网站包装设计师
  • 网站类型分类高端网站建设个人
  • 兼职网站建设策划书小杨哥直播带货平台
  • 如何设计微商城网站建设做组织架构图的网站
  • 中国能源建设招标网站我想做社区团购怎么做
  • 成都html5网站建设为什么打开Wordpress很慢
  • 口腔网站设计图建立什么船籍港
  • 做公司网站图片算是商用吗哈尔滨网站制作公司电话
  • 网站设计赚钱吗企业搭建pc端网站
  • 做招聘网站怎么运作潍坊市建设厅网站
  • 青岛正规网站设计公司郑州做网站公司中
  • 淘客网站模板网站开发技术最新技术
  • 南阳网站关键词建设网站的公司兴田德润可以吗
  • 商河网站建设淘宝关键词推广