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

哈密市建设局网站市场调研与分析

哈密市建设局网站,市场调研与分析,网站测试的目的和意义,wordpress设计漂亮的页面目录 一、类的6个默认成员函数 1、构造函数 2、析构函数 3、拷贝构造函数 4、赋值重载函数 二、赋值运算符重载 一、类的6个默认成员函数 注意:默认成员函数不能在类外面定义成全局函数。因为类里没有的话会自动生成,就会产生冲突。 1、构造函数…

目录

一、类的6个默认成员函数

1、构造函数 

2、析构函数

3、拷贝构造函数 

4、赋值重载函数

二、赋值运算符重载 


一、类的6个默认成员函数

注意:默认成员函数不能在类外面定义成全局函数。因为类里没有的话会自动生成,就会产生冲突。 

1、构造函数 

对于下面这个程序,可以看到,每次创建对象时,我们都需要手动调用Init函数,这样是不是有些麻烦呢?

class Date
{
public:void Init(int year,int month,int day){_year = year;_month = month;_day = day;}void Print(){cout << _year << "-" << _month << "-" << _day << endl;}
private:int _year;int _month;int _day;
};
int main()
{Date d1;d1.Init(2023, 5, 1);d1.Print();Date d2;d2.Init(2023, 5, 2);d2.Print();return 0;
}

构造函数就可以很好地解决这个问题。

构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象是由编译器自动调用,以保证每个成员都有一个合适的初始值,并且在对象整个生命周期只调用一次。

其主要任务并不是开空间创建对象,而是初始化对象。

特征:

1、函数名与类名相同。

2、无返回值。(也不需要void)

3、对象实例化时编译器会自动调用对应的构造函数。

4、构造函数可以重载。

使用举例: 

class Date
{
public://无参构造函数Date(){}//带参构造函数Date(int year, int month, int day){_year = year;_month = month;_day = day;}
private:int _year;int _month;int _day;
};
int main()
{Date d1;//调用无参构造函数Date d2(2023, 5, 8);//调用有参的构造函数return 0;
}

注意:

1、如果通过无参构造函数创建对象时,对象后不用跟括号,否则就成了函数声明。

2、如果类中没有显示定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显示定义,编译器将不再生成。

3、编译器自动生成的默认构造函数,对内置类型不做处理(有些编译器可能会),对自定义类型成员则会调用它的默认构造。

一般情况下,有内置类型时,就需要自己写构造函数,如果全是自定义类型成员,就可以考虑让编译器自己生成。

在C++11中针对内置类型成员不初始化的缺陷,又打了补丁,即,内置类型成员变量在类中声明时可以给默认值。

4、无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认构造函数。

2、析构函数

(1)概念

析构函数:是特殊的成员函数。与构造函数功能相反,析构函数不是完成对对象本身的销毁,局部对象的销毁工作是由编译器完成的。而对象在销毁时会自动调用析构函数,完成对象中的资源管理

(2)特性 

1、析构函数名是在类名前加 ~

2、无参数无返回类型

3、一个类只能有一个析构函数,若未显示定义,系统自动生成默认的析构函数。

4、析构函数不能重载

5、对象生命周期结束时,C++编译系统自动调用析构函数。

1、一般情况下,有动态申请资源,就需要显示写析构函数释放资源

2、没有动态申请的资源,就不需要写析构

3、如果需要释放的资源的成员类型都是自定义类型,则不需要写析构 

3、拷贝构造函数 

(1)概念

拷贝构造函数:是特殊的成员函数,只有单个形参,该形参是对本类类型对象的引用(一般用const修饰),在用已存在的类类型对象创建新对象时由编译器自动调用。

(2)特征 

1、拷贝构造函数是构造函数的一个重载形式。

2、拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器会直接报错因为会引发无穷递归调用。

3、若未显示定义,编译器会生成默认的拷贝构造函数。默认拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝。

class Date
{
public:Date(int year=1900, int month = 1, int day = 1){_year = year;_month = month;_day = day;}Date(const Date& d){//this->_year = d._year;(d2的_year = d1的_year)_year = d._year;_month = d._month;_day = d._day;}
private:int _year;int _month;int _day;
};
int main()
{Date d1;Date d2(d1);return 0;
}

4、赋值重载函数

d1=d2;//d1.operator =(d2);

已经存在的两个对象之间复制拷贝。

注意:与拷贝构造函数不同的是,拷贝构造函数是用一个对象初始化另一个对象

 下面这个关于日期类的赋值重载函数有没有什么问题?

	void operator =(const Date& d){_year = d._year;_month = d._month;_day = d._day;}
int main()
{Date d1(2023, 5, 8);//带参构造函数Date d2(d1);//拷贝构造函数,用一个已经存在的对象初始化另一个对象Date d3(d2);//拷贝构造函数//将d2赋值给d1,已经存在的两个对象的拷贝复制d1 = d2 = d3;return 0;
}

 在赋值时,如果是连续赋值,上面的函数就会有问题。

d1=d2=d3;

d3赋值给了d2后,应该有一个返回值,而我们上面写的函数的返回值是空。

	Date operator =(const Date& d){_year = d._year;_month = d._month;_day = d._day;return *this;//传值返回,返回的是对象的拷贝,不好}

如果像上面这样写,是传值返回,就需要再调用拷贝构造函数

因此,为了提高效率,我们可以将返回值改为引用。

	Date& operator =(const Date& d){_year = d._year;_month = d._month;_day = d._day;return *this;//因为出了作用域*this还在,所以可以用引用返回。}

上面的代码对于d1=d1;这样的代码是可以编译通过的,如果不想,可以按照如下写法:

	Date& operator =(const Date& d){if(*this != &d){_year = d._year;_month = d._month;_day = d._day;} return *this;//因为出了作用域*this还在,所以可以用引用返回。}

默认生成的赋值重载函数跟拷贝构造一样:

1、内置类型成员:值拷贝/浅拷贝。

2、自定义类型成员会去调用它的赋值重载函数。 

二、赋值运算符重载 

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。

函数名字:关键字operator后面接需要重载的运算符符号。

函数原型:返回值类型operator操作符(参数列表)

注意:

(1)不能通过其他连接符号来创建新的操作符。

(2)重载操作符必须有一个类类型参数。

(3)用于内置类型的运算符,其含义不能改变,如+

(4)作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this

(5)(.*)(::)(sizeof)(?:)(.)注意以上5个运算符不能重载,笔试选择题中经常出现。

class Date
{
public:void Init(int year, int month, int day){_year = year;_month = month;_day = day;}void Print(){cout << _year << "-" << _month << "-" << _day << endl;}//bool operator == (Date* this,const Date& d2)bool operator ==(const Date& d2){return _year == d2._year && _month == d2._month && _day == d2._day;}
private:int _year;int _month;int _day;
};
int main()
{Date d1, d2;d1.Init(2023, 5, 1);d2.Init(2023, 5, 2);if (d1 == d2){printf("相等");}else{printf("不相等");}return 0;
}

三、日期类的实现 

#include <iostream>
#include <functional>
#include<cassert>
using namespace std;
class Date
{//友元函数friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);public:Date(int year = 1, int month = 1, int day = 1);//带参构造函数//如果这样写://1、Date d1(2023, 5, 1);//2、const Date d2(2004, 3, 5);//d1.Print(Date* this);//可以//不可以,因为从const Date*this到Date *this权限放大了//d2.Print(Date* this);//void Print()//{//	cout << _year << "-" << _month << "-" << _day << endl;//}//如果将Print的参数改成const Date *this//对于1来说是权限缩小,对于2来说是平移,都是允许的//只要不改变对象成员变量的函数都应该const,这样const对象和普通对象就都可以调用了void Print()const{cout << _year << "-" << _month << "-" << _day << endl;}bool operator <(const Date& x);bool operator >(const Date& x);bool operator ==(const Date& x);bool operator <= (const Date& x);bool operator >= (const Date& x);bool operator !=(const Date& x);int GetMonthDay(int year, int month);Date& operator += (int day);Date operator + (int day);Date& operator++();Date operator++(int);//后置++Date& operator -= (int day);Date& operator - (int day);Date& operator--();Date operator--(int);//后置++int operator-(const Date& x);
private:int _year;int _month;int _day;
};
#include"Date.h"
Date::Date(int year, int month, int day)//带参构造函数
{if (year > 0 && month > 0 && month < 13 && day>0 && day <= GetMonthDay(year, month)){_year = year;_month = month;_day = day;}else{cout << "日期非法" << endl;assert(false);}
}
bool Date::operator <(const Date& x)
{if (_year < x._year)return true;else if (_year == x._year && _month < x._month)return true;else if (_year == x._year && _month == x._month && _day < x._day)return true;else return false;
}
bool Date :: operator ==(const Date& x)
{return _year == x._year && _month == x._month && _day == x._day;
}
bool Date:: operator <= (const Date& x)
{return *this < x || *this == x;
}
bool Date :: operator >(const Date& x)
{return !(*this <= x);
}
bool Date :: operator >= (const Date& x)
{return *this > x || *this == x;
}
bool Date :: operator!=(const Date& x)
{return !(*this == x);
}int Date::GetMonthDay(int year, int month)
{int daysArr[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 daysArr[month];
}
Date& Date :: operator+=(int day)
{if (_day < 0){return *this -= (-day);}_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month == 13){++_year;_month = 1;}}return *this;
}
Date Date:: operator+(int day)
{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 -= (int day)
{if (_day < 0){return *this += -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;
}Date& Date:: operator--()
{*this -= 1;return *this;
}
Date Date:: operator--(int)//后置++
{Date tmp(*this);*this -= 1;return tmp;
}
int Date:: operator-(const Date& x)
{Date max = *this;Date min = x;int flag = 1;if (*this<x){max = x;min = *this;flag = -1;}int n = 0;while (min != max){++min;++n;}return n * flag;
}
//void Date:: operator <<(ostream& out)
//{
//	out << _year << "年" << _month << "月" << _day << "日" << endl;
//}ostream& operator <<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}
istream& operator>>(istream& in, Date& d)
{//写个检查in >> d._year >> d._month >> d._day;return in;
}

文章转载自:
http://dinncostillroom.ydfr.cn
http://dinncobimorphemic.ydfr.cn
http://dinncobegetter.ydfr.cn
http://dinncopreservable.ydfr.cn
http://dinncoguttle.ydfr.cn
http://dinnconarcoma.ydfr.cn
http://dinncocdd.ydfr.cn
http://dinncofoliolate.ydfr.cn
http://dinncoavowry.ydfr.cn
http://dinncoglost.ydfr.cn
http://dinncoparcelgilt.ydfr.cn
http://dinncoeris.ydfr.cn
http://dinncoherring.ydfr.cn
http://dinncounglove.ydfr.cn
http://dinncobuttermilk.ydfr.cn
http://dinncobushido.ydfr.cn
http://dinncoamygdalotomy.ydfr.cn
http://dinncoforgery.ydfr.cn
http://dinncoerin.ydfr.cn
http://dinncoretravirus.ydfr.cn
http://dinncofigeater.ydfr.cn
http://dinncodithionic.ydfr.cn
http://dinncounsaved.ydfr.cn
http://dinncoslabby.ydfr.cn
http://dinncotrockenbeerenauslese.ydfr.cn
http://dinncocrissum.ydfr.cn
http://dinncocrystallite.ydfr.cn
http://dinncosyndicalist.ydfr.cn
http://dinncotatbeb.ydfr.cn
http://dinncocancan.ydfr.cn
http://dinncotripodic.ydfr.cn
http://dinncoinvisibility.ydfr.cn
http://dinncointerminable.ydfr.cn
http://dinncosink.ydfr.cn
http://dinncotaa.ydfr.cn
http://dinncoportwine.ydfr.cn
http://dinnconominate.ydfr.cn
http://dinncocopestone.ydfr.cn
http://dinncomilking.ydfr.cn
http://dinncosuboptimal.ydfr.cn
http://dinncoaffected.ydfr.cn
http://dinncolaborsaving.ydfr.cn
http://dinncolandstream.ydfr.cn
http://dinncopopie.ydfr.cn
http://dinncoobloquy.ydfr.cn
http://dinncomonopolization.ydfr.cn
http://dinncoaraliaceous.ydfr.cn
http://dinncoexcarnation.ydfr.cn
http://dinncofinner.ydfr.cn
http://dinncogoniometer.ydfr.cn
http://dinncoanomy.ydfr.cn
http://dinncohyperglycemia.ydfr.cn
http://dinncomrna.ydfr.cn
http://dinncorecelebration.ydfr.cn
http://dinncooutfield.ydfr.cn
http://dinncojavaite.ydfr.cn
http://dinncoindispensably.ydfr.cn
http://dinncodexiocardia.ydfr.cn
http://dinncosensitive.ydfr.cn
http://dinncoconformability.ydfr.cn
http://dinncoacmeist.ydfr.cn
http://dinncoarbitral.ydfr.cn
http://dinncofelid.ydfr.cn
http://dinncostale.ydfr.cn
http://dinncostowage.ydfr.cn
http://dinncoloth.ydfr.cn
http://dinncobibliograph.ydfr.cn
http://dinncolimn.ydfr.cn
http://dinncoczarina.ydfr.cn
http://dinncoembryulcia.ydfr.cn
http://dinncoassignment.ydfr.cn
http://dinnconegativist.ydfr.cn
http://dinncostanhope.ydfr.cn
http://dinncometrorrhagia.ydfr.cn
http://dinncodistraint.ydfr.cn
http://dinncodiphenylamine.ydfr.cn
http://dinncohandwheel.ydfr.cn
http://dinncodigressively.ydfr.cn
http://dinncobloodstain.ydfr.cn
http://dinncoprometal.ydfr.cn
http://dinncojinker.ydfr.cn
http://dinncocurtis.ydfr.cn
http://dinncolaid.ydfr.cn
http://dinncofascist.ydfr.cn
http://dinncoastronautess.ydfr.cn
http://dinncoarticulatory.ydfr.cn
http://dinncofelicitation.ydfr.cn
http://dinnconumeraire.ydfr.cn
http://dinncoaccentuator.ydfr.cn
http://dinncoquenchless.ydfr.cn
http://dinncocarecloth.ydfr.cn
http://dinncosubdelirium.ydfr.cn
http://dinncotransfuse.ydfr.cn
http://dinnconeurogenic.ydfr.cn
http://dinncogarshuni.ydfr.cn
http://dinncoroundabout.ydfr.cn
http://dinncosagittarius.ydfr.cn
http://dinncopunctuality.ydfr.cn
http://dinncohorsejockey.ydfr.cn
http://dinncobirthday.ydfr.cn
http://www.dinnco.com/news/149317.html

相关文章:

  • 做网站页面的需要哪些技巧郑州seo排名哪有
  • 做房地产要自己开网站免费网页代码大全
  • 企业网站开发到上线的视频百度云盘资源搜索
  • 南和县建设局黄页网站世界排名前十位
  • 最新网站源码下载百度网址提交入口平台
  • 石家庄做网站好的公司推荐企业网站建设方案
  • 网络兼职做网站今日山东新闻头条
  • 泰安市住房和城乡建设厅网站百度竞价怎么操作
  • Seo与网站推广的技术对比软文营销推广
  • 自助搭建网站系统外包优化网站
  • wordpress主要函数优化快速排名教程
  • 公司高端网站设计公司怎样制作网站教程
  • 东莞网站建设流程图无锡做网站的公司
  • 精品课程网站建设的国内外现状佛山网络排名优化
  • 手机在线做ppt模板下载网站百度推广产品
  • 重庆网站制作长沙外贸网站建站和推广
  • 珠海cp网站建设搜索引擎优化指南
  • 崇文网站建设seo网站关键词
  • 有一个箭头的做网站的软件常见的网络营销方法有哪些
  • 企业网站公示怎么做百度平台客服
  • 政府网站建设方案北京百度总部
  • 北京网站优化开户苏州网站制作公司
  • 昆明做整站优化企业qq官网
  • 怎么看网站pr值合肥seo网站排名优化公司
  • 网站建设实训个人总结视频网站推广
  • 网站让百度收录应该怎么做媒体发布平台
  • 手机网站制作时应该注意的问题响应式模版移动优化
  • 南口做网站的公司怎么做一个网站
  • 做校园后勤管理网站得重点难点广州优化网站排名
  • 成都专业网站建设价格网站的推广方法