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

个人域名可以做公司网站么网站排名优化软件有哪些

个人域名可以做公司网站么,网站排名优化软件有哪些,人像写真哔哩哔哩,织梦网站自适应怎么做好的继上期,我们今天带来c类与对象系列的继续学习。 类的6个默认成员函数 如果一个类中什么成员都没有,简称为空类。 空类中真的什么都没有吗?并不是,任何类在什么都不写时,编译器会自动生成以下6个默认成员 函数。 …

好的继上期,我们今天带来c++类与对象系列的继续学习。

类的6个默认成员函数

如果一个类中什么成员都没有,简称为空类。
空类中真的什么都没有吗?并不是,任何类在什么都不写时,编译器会自动生成以下6个默认成员
函数。
默认成员函数:用户没有显式实现,编译器会生成的成员函数称为默认成员函数。

 六个默认成员函数会实现6个功能,我们先来看第一个:

构造函数

上一段代码:

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Date
{
public:void Init(int year, int month, int day){_year = year;_month = month;_day = day;}void Printf(){cout << _year << '/' << _month << '/' << _day << endl;}
private:int _year;int _month;int _day;
};
int main()
{Date d1;d1.Printf();return 0;
}

大家在刚开始写代码的时候,比如说这里写了一个date类,我们忘记了初始化,直接调用printf函数,由于没有初始化,三个变量的值就会使三个随机数:

 这是不是就麻烦了,这个事情不仅仅是我们经常忘记,c++祖师爷也经常忘记,所以祖师爷就想给c++多个功能,自己初始化,所以祖师爷就搞了一个新的东西:构造函数

构造函数的特性:

构造函数是特殊的成员函数,需要注意的是,构造函数虽然名称叫构造,但是构造函数的主要任
并不是开空间创建对象,而是初始化对象

其特征如下:
1. 函数名与类名相同。
2. 无返回值。 (不需要写void)
3. 对象实例化时编译器自动调用对应的构造函数。
4. 构造函数可以重载。 (可以有多个构造函数,多种初始化方式)
我们给上边日期类写一个构造函数试试,看看是否真的可以初始化:
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Date
{
public://构造函数Date(){_year = 2023;_month = 8;_day = 30;cout << "构造函数" << endl;}void Printf(){cout << _year << '/' << _month << '/' << _day << endl;}
private:int _year;int _month;int _day;
};
int main()
{Date d1;d1.Printf();return 0;
}

很好,我们可以发现该类就是调用了我们写构造函数进行了初始化日期,并且我们无需调用,省下了很多时间
现在的初始化没有传参,是规定好的,就是我们构建无数次日期类的变量,都是这个时间,如果我们想自己在外部(主函数)自己传一个日期进行初始化,该怎么办?
非常简单,再写一个带参的构造函数就可以了
上代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Date
{
public://构造函数Date(){_year = 2023;_month = 8;_day = 30;cout << "构造函数" << endl;}//有参构造函数Date(int year,int month,int day){_year = year;_month = month;_day = day;cout << "有参构造函数" << endl;}void Printf(){cout << _year << '/' << _month << '/' << _day << endl;}
private:int _year;int _month;int _day;
};
int main()
{Date d1;d1.Printf();Date d2(2023,1,1);d2.Printf();return 0;
}

可以看到,我们另外又写了一个构造函数,是有参数的构造函数,两个构造函数构成函数重载,如果我们没传参,就自动调用第一个无参构造函数,如果又传参,就按照我们自己的想法调用第二个有参构造函数进行初始化,是不是和我们前两期函数重载的知识点结合起来了!另外,大家要注意一下d2的传参数方式,很新很方便,比调用init要方便的多,这个大家记住就行了。
运行截图论证:
再回想一下我们第二期讲的内容,是不是有一个缺省参数,好好好,那么是不是可以根据这个知识点把两个构造参数合二为一
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Date
{
public:Date(int year=2023,int month=8,int day=30){_year = year;_month = month;_day = day;cout << "有参构造函数" << endl;}void Printf(){cout << _year << '/' << _month << '/' << _day << endl;}
private:int _year;int _month;int _day;
};
int main()
{Date d1;d1.Printf();Date d2(2023,1,1);d2.Printf();return 0;
}

 这样不单单就简介了,还更灵活了,看不懂的同学自觉去看前两期内容

两次都是调用的这个构造函数,这就是缺省的魅力

是不是还可以这么玩:
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Date
{
public:Date(int year=2023,int month=8,int day=30){_year = year;_month = month;_day = day;cout << "有参构造函数" << endl;}void Printf(){cout << _year << '/' << _month << '/' << _day << endl;}
private:int _year;int _month;int _day;
};
int main()
{Date d1;d1.Printf();Date d2(2023,1,1);d2.Printf();Date d3(2023);d3.Printf();Date d4(2023, 12);d4.Printf();return 0;
}

好好好,缺省参数被我们玩坏了

刚刚我们看的是日期类的构造函数,相对来说比较简单,现在我们来进阶一下,看看栈的构造函数:
class Stack
{
public:
    Stack()
    {
        a = nullptr;
        top = capacity = 0;
    }
    void Push(int x)
    {
        if (top == capacity)
        {
            cout << capacity << "扩容" << endl;
            size_t newcapacity = capacity == 0 ? 4:capacity * 2;
            a = (int*)realloc(a, sizeof(int) * newcapacity);
            capacity = newcapacity;
        }
        a[top++] = x;
    }
private:
    int *a;
    int top;
    int capacity;
    int size;
};
众所周知,构造函数也叫默认成员函数,默认什么意思,也就是说,我们不去写构造函数,编译器也会自动生成
构造函数,也是默认成员函数,我们不写,编译器会自动生成编译生成的默认构造的特点:
1、我们不写才会生成,我们写了就不会生成了
2、内置类型的成员不会处理
3、自定义类型的成员才会处理,会去调用这个成员的构造函数
像是我们之前写的那个日期类,如果我们没写构造函数,他也不会自动生成,因为三个成员都是内置类型
一般情况下,都需要我们自己写构造函数,如果类变量都是自定义变量,可以有选择不写

析构函数

通过前面构造函数的学习,我们知道一个对象是怎么来的,那一个对象又是怎么没呢的? 析构函数:与构造函数功能相反,析构函数不是完成对对象本身的销毁,局部对象销毁工作是由 编译器完成的。而对象在销毁时会自动调用析构函数,完成对象中资源的清理工作。

析构函数的特性

析构函数是特殊的成员函数,其特征如下:

1. 析构函数名是在类名前加上字符 ~。

2. 无参数无返回值类型。

3. 一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。注意:析构 函数不能重载

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

上代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Date
{
public:Date(int year = 2023, int month = 8, int day = 30){_year = year;_month = month;_day = day;cout << "有参构造函数" << endl;}void Printf(){cout << _year << '/' << _month << '/' << _day << endl;}~Date(){cout << "析构函数" << endl;}
private:int _year;int _month;int _day;
};
int main()
{Date d1;d1.Printf();Date d2(2023, 1, 1);d2.Printf();return 0;
}

看,我们写的析构函数自动被调用了 

日期类的析构函数我们看不出什么来,来试试栈的析构函数

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Stack
{
public:Stack(){cout << "构造函数" << endl;a = nullptr;top = capacity = 0;}void Push(int x){if (top == capacity){cout << capacity << "扩容" << endl;size_t newcapacity = capacity == 0 ? 4 : capacity * 2;a = (int*)realloc(a, sizeof(int) * newcapacity);capacity = newcapacity;}a[top++] = x;}~Stack(){cout << "析构函数" << endl;free(a);a = nullptr;top = capacity = 0;}
private:int* a;int top;int capacity;int size;
};
int main()
{Stack s1;return 0;
}

自动调用,无论写多少个,所以这就生下了我们很多初始化和删除的时间

拷贝构造函数

上代码:

#include<iostream>
using namespace std
class Date
{
public:Date(int year = 1900, int month = 1, int day = 1){_year = year;_month = month;_day = day;}// Date(const Date& d)   // 正确写法Date(const Date& d)   // 错误写法:编译报错,会引发无穷递归{_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;
}

直接拷贝的d1给d2赋值

拷贝构造的特征:

拷贝构造函数也是特殊的成员函数,其特征如下:
1. 拷贝构造函数是构造函数的一个重载形式
2. 拷贝构造函数的参数只有一个必须是类类型对象的引用,使用传值方式编译器直接报错
因为会引发无穷递归调用。

赋值运算符重载

上代码:

#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Date
{
public:Date(int year = 2023, int month = 8, int day = 30){_year = year;_month = month;_day = day;cout << "有参构造函数" << endl;}void Printf(){cout << _year << '/' << _month << '/' << _day << endl;}~Date(){cout << "析构函数" << endl;}
private:int _year;int _month;int _day;
};
int main()
{Date d1(2023, 1, 1);Date d2(2023, 1, 2);d1 < d2;return 0;
}

大家看看这样比较两个日期类的变量行不行

答案是当然不行

 但是这样写

int i,j;

i<j;

这样比较,就可以,这是因为编译器知道int变量的存储模式和比较方式

那么我们可不可以让编译器也可以直接比较date

这就涉及到这个课题内容运算符重载

#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Date
{
public:Date(int year = 2023, int month = 8, int day = 30){_year = year;_month = month;_day = day;cout << "有参构造函数" << endl;}void Printf(){cout << _year << '/' << _month << '/' << _day << endl;}~Date(){cout << "析构函数" << endl;}//private:int _year;int _month;int _day;
};
bool operator<(const Date& x1, const Date& x2)
{if (x1._year < x2._year){return true;}else if (x1._year == x2._year && x1._month < x2._month){return true;}else if (x1._year == x2._year && x1._month == x2._month && x1._day < x2._day){return true;}else{return false;}
}
int main()
{Date d1(2023, 1, 1);Date d2(2023, 1, 2);cout<<(d1 < d2)<<endl;return 0;
}

有函数重载,就有运算符重载,上边就是使用规则,大家记住就行了

那有<运算符,就有<=,>,>=,=,+,-,+=,-=

博主在这里一一实现了

class Date
{
public:Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}void Print(){cout << _year << "/" << _month << "/" << _day << endl;}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;}else{return false;}}bool operator==(const Date& d){return _year == d._year&& _month == d._month&& _day == d._day;}// d1 <= d2bool operator<=(const Date& d){return *this < d || *this == d;}bool operator>(const Date& d){return !(*this <= d);}bool operator>=(const Date& d){return !(*this < d);}bool operator!=(const Date& d){return !(*this == d);}int GetMonthDay(int year, int month){int monthArray[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 monthArray[month];}Date& operator+=(int day){_day += day;while (_day > GetMonthDay(_year, _month)){// 月进位_day -= GetMonthDay(_year, _month);++_month;// 月满了if (_month == 13){++_year;_month = 1;}}return *this;}Date operator+(int day){Date tmp(*this);tmp += day;return tmp;//tmp._day += day;//while (tmp._day > GetMonthDay(tmp._year, tmp._month))//{//	// 月进位//	tmp._day -= GetMonthDay(tmp._year, tmp._month);//	++_month;//	// 月满了//	if (tmp._month == 13)//	{//		++tmp._year;//		tmp._month = 1;//	}//}//return tmp;}private:// 内置类型int _year;int _month;int _day;
};

 

关于日期各种加减乘除运算的逻辑实现是没有任何难度的,就是年完月,月完日,进位加

大家要理解的是重载的操作

注意:
不能通过连接其他符号来创建新的操作符:比如operator@
重载操作符必须有一个类类型参数
用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不 能改变其含义
作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐
藏的this
.* :: sizeof ?: . 注意以上5个运算符不能重载。这个经常在笔试选择题中出
现。

好的类和对象的中篇就到这里,对于默认成岩函数基本就接好玩了,期待三连


文章转载自:
http://dinnconookery.ssfq.cn
http://dinncoaspirate.ssfq.cn
http://dinncobetted.ssfq.cn
http://dinncowhich.ssfq.cn
http://dinncoiupap.ssfq.cn
http://dinncowordmongering.ssfq.cn
http://dinncoobovate.ssfq.cn
http://dinncopedalo.ssfq.cn
http://dinncoknockwurst.ssfq.cn
http://dinncoahd.ssfq.cn
http://dinncopizza.ssfq.cn
http://dinncoappanage.ssfq.cn
http://dinncoslavism.ssfq.cn
http://dinncoturbosupercharged.ssfq.cn
http://dinncospelldown.ssfq.cn
http://dinncoheiau.ssfq.cn
http://dinncogeophilous.ssfq.cn
http://dinncoepilimnion.ssfq.cn
http://dinncohydrant.ssfq.cn
http://dinncorockstaff.ssfq.cn
http://dinncoswissair.ssfq.cn
http://dinncojetfoil.ssfq.cn
http://dinncojinricksha.ssfq.cn
http://dinncotalcose.ssfq.cn
http://dinncoenrich.ssfq.cn
http://dinncotcbm.ssfq.cn
http://dinncolorgnette.ssfq.cn
http://dinncogwyn.ssfq.cn
http://dinncograil.ssfq.cn
http://dinncopekinese.ssfq.cn
http://dinncodeafferented.ssfq.cn
http://dinncoleguminous.ssfq.cn
http://dinncoirrepressibly.ssfq.cn
http://dinncorichen.ssfq.cn
http://dinncocorridor.ssfq.cn
http://dinncoabaddon.ssfq.cn
http://dinncoheartiness.ssfq.cn
http://dinncotalliate.ssfq.cn
http://dinncoditch.ssfq.cn
http://dinncobursar.ssfq.cn
http://dinncotypecast.ssfq.cn
http://dinncopeckish.ssfq.cn
http://dinnconewsmonger.ssfq.cn
http://dinncoseine.ssfq.cn
http://dinncodemilune.ssfq.cn
http://dinncorawin.ssfq.cn
http://dinncokoorajong.ssfq.cn
http://dinncoagnosia.ssfq.cn
http://dinncofusibility.ssfq.cn
http://dinncosverdlovsk.ssfq.cn
http://dinncocapper.ssfq.cn
http://dinnconaturopathy.ssfq.cn
http://dinncobardic.ssfq.cn
http://dinncovasodilator.ssfq.cn
http://dinncobeagle.ssfq.cn
http://dinncoopponent.ssfq.cn
http://dinncofrap.ssfq.cn
http://dinncogossipist.ssfq.cn
http://dinncohardiness.ssfq.cn
http://dinncohepburnian.ssfq.cn
http://dinncotetter.ssfq.cn
http://dinncosabbath.ssfq.cn
http://dinncosmithiantha.ssfq.cn
http://dinncosunbow.ssfq.cn
http://dinncorooinek.ssfq.cn
http://dinncoinveterately.ssfq.cn
http://dinncosubtilin.ssfq.cn
http://dinncosexcapade.ssfq.cn
http://dinncomesial.ssfq.cn
http://dinncoplasticated.ssfq.cn
http://dinncobeside.ssfq.cn
http://dinncoferromanganese.ssfq.cn
http://dinncoshadowgraph.ssfq.cn
http://dinncoobole.ssfq.cn
http://dinncobemaze.ssfq.cn
http://dinncoilluminance.ssfq.cn
http://dinncobadly.ssfq.cn
http://dinncotriol.ssfq.cn
http://dinncobottomry.ssfq.cn
http://dinncoultramundane.ssfq.cn
http://dinncopiperaceous.ssfq.cn
http://dinncowikiup.ssfq.cn
http://dinncobouillon.ssfq.cn
http://dinncoshipowner.ssfq.cn
http://dinncotzar.ssfq.cn
http://dinncomalinger.ssfq.cn
http://dinncobarony.ssfq.cn
http://dinncofiredog.ssfq.cn
http://dinncoalembic.ssfq.cn
http://dinncowestwards.ssfq.cn
http://dinncoilluminist.ssfq.cn
http://dinncostraitly.ssfq.cn
http://dinncooffish.ssfq.cn
http://dinncoproturan.ssfq.cn
http://dinncohippiedom.ssfq.cn
http://dinncounjoined.ssfq.cn
http://dinncowidthwise.ssfq.cn
http://dinncoheadforemost.ssfq.cn
http://dinncoaurous.ssfq.cn
http://dinncometaboly.ssfq.cn
http://www.dinnco.com/news/148429.html

相关文章:

  • 企业档案网站建设搜索引擎营销的原理是什么
  • 网站开发应聘信息seo优化自学
  • wordpress申请子站邢台市seo服务
  • wordpress如何解压seo外链工具源码
  • 网站的公关和广告活动怎么做seo建站平台哪家好
  • 做网站完整视频网站域名综合查询
  • phpmyadmin 备份 wordpressseo推广百度百科
  • 网站建设公司发展规划网络营销案例ppt
  • wordpress如何自动采集网站图片深圳网站制作哪家好
  • 网络推广怎么能做好seoyoon
  • 网站建设的规划创建网站需要多少资金
  • 深圳网站建站建设seo实战培训学校
  • 浙江平台网站建设找哪家站长之家查询网站
  • 网站建设流程时间表2021最近最火的关键词
  • web网站开发的特点广州网络推广选择
  • 合肥公司做网站看网站搜什么关键词
  • 52麻将官方网站做代理淘宝seo排名优化的方法
  • 设计师个人网站欣赏企业网站代运营
  • 惠通网站建设百度sem
  • 动态网站开发的技术seo推广网站
  • 建设宣传网站的必要性seo外链
  • 网站建设属于什么会计科目百度一下首页网页
  • 山东神华网站建设北京网络优化
  • 网站制作公司哪里好北京网站优化怎么样
  • 河北网页制作苏州整站优化
  • 图片站手机网站怎么做seo学堂
  • 滨海做网站的公司百度指数明星搜索排名
  • 音视频网站建设可行性报告什么是网络软文营销
  • 西安微信网站建设公司想要网站推广版
  • 网站调用优酷视频去广告免费建立一个网站