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

做网站的企业文化怎么写《网页设计与网站建设》

做网站的企业文化怎么写,《网页设计与网站建设》,途牛旅游线路网站建设分析,深圳建网站seo日期类的拓展 c语言中的printf函数只能打印内置类型#xff0c;为了弥补这一不足#xff0c;c利用运算符重载可以打印自定义类型。 void operator(ostreamout);//声明在date.h中void Date::operator(ostream out)//定义在date.cpp中 {out为了弥补这一不足c利用运算符重载可以打印自定义类型。 void operator(ostreamout);//声明在date.h中void Date::operator(ostream out)//定义在date.cpp中 {outthis-_year 年 this-_month月 this-_day日;}int main()//.cpp中 {Date d1(2024, 3, 30);d1.operator(cout);}如果我们修改成这样呢.cpp int main() {Date d1(2024, 3, 30);//d1.operator(cout);cout d1;}由于我们的运算符重载是定义在日期类里面的所以默认*this是左操作数所以类定义的对象必须放在左边。修改如下 int main() {Date d1(2024, 3, 30);//d1.operator(cout);d1cout; }如果我们就要使类对象在右边呢 所以我们必须将该函数放在全局但是放在全局的话类对象的变量都是私有就不能访问到我们先改成共有 void operator(ostream out, Date d);//声明放全局在date.h中void operator(ostream out, Date d)//定义在date.cpp中 {out d._year 年 d._month 月 d._day 日;}int main()//在.cpp中 {Date d1(2024, 3, 30);//d1.operator(cout);coutd1;}如果我们不将变量改成共有我们全局函数怎么才能访问到类成员变量呢 friend void operator(ostream out, Date d);在声明中加上这个使全局的函数成为类的友元函数就可以访问类的变量了。 我们知道变量可以连续赋值这里可以支持连续输出吗 赋值运算符从右向左先把q的值赋值为10然后再把q的值赋给p,然后把p的值赋给i,实现连续赋值因此需要返回值 ostream operator(ostream out, Date d)//定义 {out d._year 年 d._month 月 d._day 日;return out;}ostream operator(ostream out, Date d);//声明int main() {Date d1 (2002, 3, 11);Date d2 (2012, 3, 11);cout d1 d2;}既然能输出自定义类型那怎么使用运算符重载函数实现自定义类型输入呢 实现如下 istream operator(istream in, Date d);//声明在date.h全局istream operator(istream in, Date d)//date.cpp定义 {ind._year d._month d._day ;return in;}int main() {Date d1 ;Date d2 ;cin d1 d2;//给输入cout d1 d2;//输出}日期类的安全性修改 比如如果输入不合法就提示我们需要写一个检测的函数 修改后的日期类 date.h #pragma once #includeiostream using namespace std; class Date{public:// 获取某年某月的天数int GetMonthDay(int year, int month);// 全缺省的构造函数Date(int year 1900, int month 1, int day 1);// 拷贝构造函数// d2(d1)Date(const Date d);// 赋值运算符重载// d2 d3 - d2.operator(d2, d3)Date operator(const Date d);// 析构函数~Date();// 日期天数Date operator(int day);// 日期天数Date operator(int day);// 日期-天数Date operator-(int day);// 日期-天数Date operator-(int day);// 前置Date operator();// 后置Date operator(int);// 后置--Date operator--(int);// 前置--Date operator--();// 运算符重载bool operator(const Date d);// 运算符重载bool operator(const Date d);// 运算符重载bool operator (const Date d);// 运算符重载bool operator (const Date d);// 运算符重载bool operator (const Date d);// !运算符重载bool operator ! (const Date d);bool isvalid(Date d);// 日期-日期 返回天数int operator-(const Date d);void print();/*void operator(ostreamout);*/friend ostream operator(ostream out, Date d);friend istream operator(istream in, Date d); private:int _year;int _month;int _day;}; ostream operator(ostream out, Date d); istream operator(istream in, Date d); date.cpp #include date.h Date::Date(int year, int month, int day) {_year year;_month month;_day day;if (!(this-isvalid(*this))){cout 初始化非法 endl;}} void Date:: print() {cout _year . _month . _dayendl; } Date::Date(const Date d) {_year d._year;_month d._month;_day d._day;} Date::~Date() {_year 0;_month 0;_day 0;} int Date:: GetMonthDay(int year, int month) {int arr[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 arr[month];} Date Date:: operator(const Date d) {if (this ! d){_year d._year;_month d._month;_day d._day;}return *this;} Date Date::operator(int day) {_day day;while (_day GetMonthDay(_year,_month)){_day - GetMonthDay(_year, _month);_month;if (_month 13){_month 0;}}return *this;} Date Date:: operator(int day) {Date tmp *this;tmp._day day;while (tmp._day GetMonthDay(tmp._year, tmp._month)){tmp._day - GetMonthDay(tmp._year, tmp._month);tmp._month;if (tmp._month 13){tmp._month 0;}}return tmp;} Date Date:: operator-(int day) {_day - day;while (_day 0){_month--;if (_month 0){_year--;_month 12;}_day GetMonthDay(_year, _month);}return *this;} Date Date::operator-(int day) {Date tmp *this;tmp - (day);return tmp;} bool Date:: operator(const Date d) {if (_year d._year){return true;}else{if (_year d._year){if (_month d._month){return true;}else{if (_month d._month){if (_day d._day)return true;}}}}return false;} bool Date::operator(const Date d) {return (_year d._year) (_month d._month) ( _day d._day);} bool Date:: operator (const Date d) {return *this (d) || *this (d);}bool Date:: operator (const Date d) {return !(*this (d));} bool Date::operator (const Date d) {return !(*this(d));} bool Date:: operator ! (const Date d) {return !(*this(d));}Date Date::operator() {*this *this 1;return *this;}Date Date:: operator(int) {Date tmp *this;*this*this 1;return tmp;}Date Date::operator--(int) {Date tmp *this;*this*this-1;return tmp;} Date Date:: operator--() {*this *this - 1;return *this;}int Date::operator-(const Date d) {Date max *this;Date min d;int flag 1;if (min (max)){max d;min *this;flag -1;}int n 0;while (max ! min){n;min;}return flag*n;} bool Date::isvalid(Date d) {if (_year 0 || _month 0 || _month12 || _day1 || _dayGetMonthDay(_year, _month)){return false;}true;} ostream operator(ostream out, Date d) {out d._year 年 d._month 月 d._day 日;return out;} istream operator(istream in, Date d) {while (1){cout 请输入时间 endl;in d._year d._month d._day;if (!d.isvalid(d)){cout 输入不合法 endl;}else{break;}}return in; } 权限问题 首先看一个问题 我们这里调用print函数发现调用不动 应该怎么修改呢 在printhan函数的声明和定义后面加上const void Date:: print() const//定义 {cout _year . _month . _dayendl; }void print() const;//声明此时该函数中*this指向的内容不能被修改。现在调用相当于权限平移就可以了。 此时我们的print函数时加了const int main() {Date d1 ;Date d2(2003,3,3);const Date d3(2002,1,2);d3.print();d2.print();}d2是非const的类对象也能调用const函数print,因为权限缩小了所以可以。 总结 成员函数如果是一个对成员变量只进行读访问的函数建议函数const,这样const对象和非const对象都可以使用 如果一个对成员变量要进行读写访问的函数不能加const,否则不能修改成员变量 const对象可以调用非const成员函数吗 权限放大非const对象可以调用const成员函数吗 权限缩小const成员函数内可以调用其它的非const成员函数吗权限放大非const成员函数内可以调用其它的const成员函数吗权限缩小 权限放大和缩小和平移适用于指针和引用 取地址及const取地址操作符重载 这两个默认成员函数一般不用重新定义 编译器默认会生成。 class Date { public :Date* operator(){return this ;}const Date* operator()const{return this ;} private :int _year ; // 年int _month ; // 月int _day ; // 日 };这两个运算符一般不需要重载使用编译器生成的默认取地址的重载即可只有特殊情况才需要重载比如想让别人获取到指定的内容 初始化列表 在创建对象时编译器通过调用构造函数给对象中各个成员变量一个合适的初始值。 class Date { public: Date(int year, int month, int day){_year year;_month month;_day day;_year1;} private: int _year; int _month; int _day; };虽然上述构造函数调用之后对象中已经有了一个初始值但是不能将其称为对对象中成员变量的初始化构造函数体中的语句只能将其称为赋初值而不能称作初始化。因为初始化只能初始化一次而构造函数体内可以多次赋值。 初始化列表以一个冒号开始接着是一个以逗号分隔的数据成员列表每个成员变量后面跟一个放在括号中的初始值或表达式。 class Date { public: Date(int year, int month, int day): _year(year), _month(month), _day(day){} private: int _year; int _month; int _day; };因为我们的常量引用必须在定义的时候初始化。 #includeiostream using namespace std; class Date { public:Date(int year, int month, int day){_year year;_month month;_day day;_year 1;n 1;} private:int _year;int _month;int _day;const int n; };int main(){Date d1(2003, 3, 11);}所以我们常量初始化一定要放在初始化列表中。 #includeiostream using namespace std; class Date { public:Date(int year, int month, int day):n(1), _year(year), _month(month), _day(day){} private:int _year;int _month;int _day;const int n; };int main(){Date d1(2003, 3, 11);}引用类型必须在初始化时候给初值 #includeiostream using namespace std; class Date { public:Date(int year, int month, int day,intp):n(3), _year(year), _month(month), _day(day),x(p){} private:int _year;int _month;int _day;const int n1;int x; };int main(){int num 5;Date d1(2003, 3, 11,num);}p是num的别名而x又是p的别名。 3.当我们类成员变量里面有自定义类型的话并且我们没有在初始化列表初始化初始化列表会给他调用他自己的默认构造函数完成初始化。这里的默认构造是我们写的全缺省构造函数 #includeiostream using namespace std; class A { public:A(int a3):_a(a){cout 调用 endl;} private:int _a; }; class Date { public:Date(int year, int month, int day,intp):n(3), _year(year), _month(month), _day(day),x(p){} private:int _year;int _month;int _day;const int n1;int x;A cou; };int main(){int num 5;Date d1(2003, 3, 11,num);}如果A dou对象没有默认构造呢默认构造包括全缺省无参系统生成的这里构造有参所以没有默认构造 我们可以在初始化列表给自定义类型初始化。 #includeiostream using namespace std; class A { public:A(int a):_a(a){cout 调用 endl;} private:int _a; }; class Date { public:Date(int year, int month, int day,intp):n(3), _year(year), _month(month), _day(day),x(p),cou(5){} private:int _year;int _month;int _day;const int n1;int x;A cou; };int main(){int num 5;Date d1(2003, 3, 11,num);}【注意】 每个成员变量在初始化列表中只能出现一次(初始化只能初始化一次)类中包含以下成员必须放在初始化列表位置进行初始化 引用成员变量const成员变量自定义类型成员(且该类没有默认构造函数时) 同时也可以在初始化列表给指针初始化可以在初始化列表中检查空间是否申请 #includeiostream #includestdlib.h using namespace std; class A { public:A(int a):_a(a){cout 调用 endl;} private:int _a; }; class Date { public:Date(int year, int month, int day,intp):n(3), _year(year), _month(month), _day(day),x(p),cou(5),ptr((int*)malloc(sizeof(int))){if (ptr NULL){perror(malloc fail);}} private:int _year;int _month;int _day;const int n1;int x;A cou;int* ptr; };int main(){int num 5;Date d1(2003, 3, 11,num);}总结声明的缺省参数是给初始化列表的如果初始化列表没有给一个变量初始化这个变量就拿的是缺省参数的值自定义类型如果初始化列表没有初始化就会去调用自己的默认构造函数来进行初始化如果没有默认构造函数的话就要看对象实例化的时候有没有给自己写的构造函数有没有传值如果没有的话就是随机值如果初始化列表给自定义类型初始化了就没有上面的一系列操作了初始化列表后还可以在括号里面给变量赋值除了那3个相当于构造函数。 单参数构造函数支持隐式类型转换 #includeiostream #includestdlib.h using namespace std; class C { public:C(int x 0):_x(x){}C(const C cc){cout C(const C cc) endl;}private:int _x; }; int main() {C cc2 2;}编译器优化连续步骤的·构造一般会合二为一 和隐式类型转化差不多 如果我们的自定义的对象没有在初始化列表中初始化并且也没有自己的默认构造函数的话我们要给缺省值给自定义对象赋值我们必须在全局实例化一个类对象然后将该类对象拷贝构造给里面的自定义变量。 #includeiostream #includestdlib.h using namespace std; class C { public:C(int x 0):_x(x){}C(const C cc){cout C(const C cc) endl;}private:int _x; }; class Stack { public:void Push(const C c){} };int main() {Stack st;C cc3(3);st.Push(cc3);st.Push(3);return 0; }这里如果我们要入一个C类的对cc3并且cc3类成员变量初始化为3本来我们需要先给cc3对象赋值然后再将C类对象cc3入栈现在支持单参数构造函数支持隐式类型转换就可以入栈一个类成员变量初始化为3并且是C类对象。 c11支持多参数构造函数支持隐式类型转换 #includeiostream #includestdlib.h using namespace std; class A { public:A(int a1, int a2):_a1(a1),_a2(a2){}private:int _a1;int _a2; };int main() {A aa1 { 1, 2 };return 0; }explicit关键字 #includeiostream #includestdlib.h using namespace std; class A { public:explicit A(int a1, int a2):_a1(a1),_a2(a2){}private:int _a1;int _a2; };int main() {A aa1 { 1, 2 };return 0; }用explicit修饰构造函数将会禁止构造函数的隐式转换。 成员变量在类中声明次序就是其在初始化列表中的初始化顺序与其在初始化列表中的先后次序无关 class A { public:A(int a):_a1(a),_a2(_a1){}void Print() {cout_a1 _a2endl;} private:int _a2;int _a1; }; int main() {A aa(1);aa.Print(); }上述代码会出现什么情况D A. 输出1 1 B.程序崩溃 C.编译不通过 D.输出1 随机值 因为是按照声明次序初始化所以_a2初始化的时候_a1还没有初始化所以_a2是随机值然后_a1按照初始化列表初始化为1 static成员 概念 声明为static的类成员称为类的静态成员用static修饰的成员变量称之为静态成员变量用static修饰的成员函数称之为静态成员函数。静态成员变量一定要在类外进行初始化 class A { public:A(){}A(const A aa){}private:int a 0; };A Func() {A aa;return aa; }int main() {A aa1;A aa2;Func();return 0; }实现一个类计算程序中创建出了多少个类对象 1.我们定义一个全局变量统计构造函数和拷贝构造函数调用了多少次 #includeiostream #includestdlib.h using namespace std; int n 0; class A { public:A(){n;}A(const A aa){n;}private:int a 0; };A Func() {A aa;return aa; }int main() {A aa1;A aa2;Func();cout n endl;return 0; }2.由于全局变量可以在任意位置被修改我们将n定义在类中但是类中的n是私有的我们可以使用静态变量static,然后去掉私有但是static 必须在类外面定义初始化这时候n就可以在每个类对象中使用了突破类域使用n #includeiostream #includestdlib.h using namespace std;class A { public:A(){n;}A(const A aa){n;}int a 0;static int n; };int A::n 0;A Func() {A aa;return aa; }int main() {A aa1;A aa2;Func();cout aa1.n endl;cout aa2.n endl;cout A::n endl;return 0; }3.使用静态函数返回静态变量静态函数中没有this指针只能操作静态变量。 #includeiostream #includestdlib.h using namespace std;class A { public:A(){n;}A(const A aa){n;}static int GetN(){return n;}private:int a 0;static int n; };int A::n 0;A Func() {A aa;return aa; }int main() {A aa1;A aa2;Func();cout aa1.GetN() endl;cout A::GetN() endl;return 0; }特性 静态成员为所有类对象所共享不属于某个具体的对象存放在静态区静态成员变量必须在类外定义定义时不添加static关键字类中只是声明类静态成员即可用 类名::静态成员 或者 对象.静态成员 来访问静态成员函数没有隐藏的this指针不能访问任何非静态成员静态成员也是类的成员受public、protected、private 访问限定符的限制
http://www.zqtcl.cn/news/303887/

相关文章:

  • 网站与网页之间的区别是什么意思通过微信发布诱导分享的美文或者集赞活动属于哪种网络营销方式
  • 可信网站代码想学做网站从哪里入手
  • 做公众号选择图片的网站wordpress怎么看代码
  • 个人 中小企业公司网站建设方案百度网页版链接地址
  • 青岛网站推广方案网线制作心得与体会
  • 杭州网站优化公司哈尔滨企业网站模板建站
  • 洛阳免费网站建设自己做网站最新视频教程
  • 网站备案查询 美橙网开发app需要的技术
  • 软件产品如何做网站推广昆山外贸网站建设推广
  • 景德镇市城市建设规划网站wordpress用不了了
  • 网站及新媒体建设宣传片wordpress 无法编辑主题
  • 东莞设计网站重庆做腋臭骑士网站
  • 什么软件可以搜索关键词精准网站信息优化的方式
  • 购物网站排名前十名山东泰安建筑工程集团有限公司
  • 源码下载站用vs网站开发
  • 自己做网站seo彩票的网站怎么做
  • 如何在网站后台找到死链接网站内页权重查询
  • 专业做国际网站网站开发的编程软件
  • 如何运营垂直网站网页工具大全
  • 如何让自己做的网站可以播放歌曲做培训网站
  • 做网站的毕业设计网站没备案怎么做淘宝客
  • 百度申诉网站建设银行住房租赁代表品牌是什么
  • 网站初期推广方案虚拟服务器搭建wordpress
  • jeecms可以做网站卖吗山西网络推广专业
  • 2017 如何做网站优化育儿哪个网站做的好
  • 网站制作容易吗青岛网站建设公司报价
  • 淘宝建设网站的好处网站制作结构
  • 网站开发网站建设公司临沂网站建设找谁
  • 咋么做网站在电脑上潍坊免费模板建站
  • 苏州网站建设推广咨询平台做网站的公司图