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

企业网站例子wordpress做人事网站

企业网站例子,wordpress做人事网站,wordpress论坛主题模板,什么是高端网站建设文章目录 前言一、类的6个默认成员函数二、构造函数三、析构函数 前言 #x1f467;个人主页#xff1a;小沈YO. #x1f61a;小编介绍#xff1a;欢迎来到我的乱七八糟小星球#x1f31d; #x1f4cb;专栏#xff1a;C 心愿便利店 #x1f511;本章内容#xff1a;类… 文章目录 前言一、类的6个默认成员函数二、构造函数三、析构函数 前言 个人主页小沈YO. 小编介绍欢迎来到我的乱七八糟小星球 专栏C 心愿便利店 本章内容类和对象 记得 评论 点赞 收藏 关注哦~ 提示以下是本篇文章正文内容下面案例可供参考 一、类的6个默认成员函数 如果一个类中什么成员都没有简称为空类。 空类中真的什么都没有吗并不是任何类在什么都不写时编译器会自动生成以下6个默认成员函数。 默认成员函数用户没有显式实现编译器会生成的成员函数称为默认成员函数 二、构造函数 #mermaid-svg-D24Fh8B0fcKwdc6t {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-D24Fh8B0fcKwdc6t .error-icon{fill:#552222;}#mermaid-svg-D24Fh8B0fcKwdc6t .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-D24Fh8B0fcKwdc6t .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-D24Fh8B0fcKwdc6t .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-D24Fh8B0fcKwdc6t .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-D24Fh8B0fcKwdc6t .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-D24Fh8B0fcKwdc6t .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-D24Fh8B0fcKwdc6t .marker{fill:#333333;stroke:#333333;}#mermaid-svg-D24Fh8B0fcKwdc6t .marker.cross{stroke:#333333;}#mermaid-svg-D24Fh8B0fcKwdc6t svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-D24Fh8B0fcKwdc6t .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-D24Fh8B0fcKwdc6t .cluster-label text{fill:#333;}#mermaid-svg-D24Fh8B0fcKwdc6t .cluster-label span{color:#333;}#mermaid-svg-D24Fh8B0fcKwdc6t .label text,#mermaid-svg-D24Fh8B0fcKwdc6t span{fill:#333;color:#333;}#mermaid-svg-D24Fh8B0fcKwdc6t .node rect,#mermaid-svg-D24Fh8B0fcKwdc6t .node circle,#mermaid-svg-D24Fh8B0fcKwdc6t .node ellipse,#mermaid-svg-D24Fh8B0fcKwdc6t .node polygon,#mermaid-svg-D24Fh8B0fcKwdc6t .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-D24Fh8B0fcKwdc6t .node .label{text-align:center;}#mermaid-svg-D24Fh8B0fcKwdc6t .node.clickable{cursor:pointer;}#mermaid-svg-D24Fh8B0fcKwdc6t .arrowheadPath{fill:#333333;}#mermaid-svg-D24Fh8B0fcKwdc6t .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-D24Fh8B0fcKwdc6t .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-D24Fh8B0fcKwdc6t .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-D24Fh8B0fcKwdc6t .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-D24Fh8B0fcKwdc6t .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-D24Fh8B0fcKwdc6t .cluster text{fill:#333;}#mermaid-svg-D24Fh8B0fcKwdc6t .cluster span{color:#333;}#mermaid-svg-D24Fh8B0fcKwdc6t 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-D24Fh8B0fcKwdc6t :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 1. 构造函数的概念 如下Date类没有初始化打印出来就会是随机值同时对于栈没有初始化就会报错 那如果想能否在创建对象的同时就将信息设置进去呢。因此就有了构造函数。以Date类为例 #includeiostream using namespace std; 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.Print();//没有调用Init初始化函数Date d2;d2.Init(2022, 7, 6);//调用Init初始化函数d2.Print();return 0; }对于Date类可以通过 Init 公有方法给对象设置日期但如果每次创建对象时都调用该方法设置信息未免有点麻烦那能否在对象创建时就将信息设置进去呢 #mermaid-svg-4F7ePWYCkdbbXsz5 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-4F7ePWYCkdbbXsz5 .error-icon{fill:#552222;}#mermaid-svg-4F7ePWYCkdbbXsz5 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-4F7ePWYCkdbbXsz5 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-4F7ePWYCkdbbXsz5 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-4F7ePWYCkdbbXsz5 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-4F7ePWYCkdbbXsz5 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-4F7ePWYCkdbbXsz5 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-4F7ePWYCkdbbXsz5 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-4F7ePWYCkdbbXsz5 .marker.cross{stroke:#333333;}#mermaid-svg-4F7ePWYCkdbbXsz5 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-4F7ePWYCkdbbXsz5 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-4F7ePWYCkdbbXsz5 .cluster-label text{fill:#333;}#mermaid-svg-4F7ePWYCkdbbXsz5 .cluster-label span{color:#333;}#mermaid-svg-4F7ePWYCkdbbXsz5 .label text,#mermaid-svg-4F7ePWYCkdbbXsz5 span{fill:#333;color:#333;}#mermaid-svg-4F7ePWYCkdbbXsz5 .node rect,#mermaid-svg-4F7ePWYCkdbbXsz5 .node circle,#mermaid-svg-4F7ePWYCkdbbXsz5 .node ellipse,#mermaid-svg-4F7ePWYCkdbbXsz5 .node polygon,#mermaid-svg-4F7ePWYCkdbbXsz5 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-4F7ePWYCkdbbXsz5 .node .label{text-align:center;}#mermaid-svg-4F7ePWYCkdbbXsz5 .node.clickable{cursor:pointer;}#mermaid-svg-4F7ePWYCkdbbXsz5 .arrowheadPath{fill:#333333;}#mermaid-svg-4F7ePWYCkdbbXsz5 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-4F7ePWYCkdbbXsz5 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-4F7ePWYCkdbbXsz5 .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-4F7ePWYCkdbbXsz5 .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-4F7ePWYCkdbbXsz5 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-4F7ePWYCkdbbXsz5 .cluster text{fill:#333;}#mermaid-svg-4F7ePWYCkdbbXsz5 .cluster span{color:#333;}#mermaid-svg-4F7ePWYCkdbbXsz5 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-4F7ePWYCkdbbXsz5 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 2. 构造函数的定义 构造函数是一个特殊的成员函数名字与类名相同, 创建类类型对象时由编译器自动调用以保证每个数据成员都有 一个合适的初始值并且在对象整个生命周期内只调用一次。 #mermaid-svg-O1KuR1QMGq13nLQu {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-O1KuR1QMGq13nLQu .error-icon{fill:#552222;}#mermaid-svg-O1KuR1QMGq13nLQu .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-O1KuR1QMGq13nLQu .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-O1KuR1QMGq13nLQu .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-O1KuR1QMGq13nLQu .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-O1KuR1QMGq13nLQu .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-O1KuR1QMGq13nLQu .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-O1KuR1QMGq13nLQu .marker{fill:#333333;stroke:#333333;}#mermaid-svg-O1KuR1QMGq13nLQu .marker.cross{stroke:#333333;}#mermaid-svg-O1KuR1QMGq13nLQu svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-O1KuR1QMGq13nLQu .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-O1KuR1QMGq13nLQu .cluster-label text{fill:#333;}#mermaid-svg-O1KuR1QMGq13nLQu .cluster-label span{color:#333;}#mermaid-svg-O1KuR1QMGq13nLQu .label text,#mermaid-svg-O1KuR1QMGq13nLQu span{fill:#333;color:#333;}#mermaid-svg-O1KuR1QMGq13nLQu .node rect,#mermaid-svg-O1KuR1QMGq13nLQu .node circle,#mermaid-svg-O1KuR1QMGq13nLQu .node ellipse,#mermaid-svg-O1KuR1QMGq13nLQu .node polygon,#mermaid-svg-O1KuR1QMGq13nLQu .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-O1KuR1QMGq13nLQu .node .label{text-align:center;}#mermaid-svg-O1KuR1QMGq13nLQu .node.clickable{cursor:pointer;}#mermaid-svg-O1KuR1QMGq13nLQu .arrowheadPath{fill:#333333;}#mermaid-svg-O1KuR1QMGq13nLQu .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-O1KuR1QMGq13nLQu .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-O1KuR1QMGq13nLQu .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-O1KuR1QMGq13nLQu .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-O1KuR1QMGq13nLQu .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-O1KuR1QMGq13nLQu .cluster text{fill:#333;}#mermaid-svg-O1KuR1QMGq13nLQu .cluster span{color:#333;}#mermaid-svg-O1KuR1QMGq13nLQu 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-O1KuR1QMGq13nLQu :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 3. 构造函数的特性 构造函数是特殊的成员函数需要注意的是构造函数虽然名称叫构造但是构造函数的主要任务并不是开空间创建对象而是初始化对象。 其特征如下 函数名与类名相同。无返回值。不需要写void对象实例化时编译器自动调用对应的构造函数。 对于上述代码所运行后的结果没有初始化d1结果是随机值然后对比下述代码同样没有初始化d1及结果运行结果自动初始化为1/1/1不是随机值且打印了Date()这就说明对象实例化时编译器自动调用对应的构造函数 #includeiostream using namespace std; class Date { public:构造函数Date()函数名与类名相同且无返回值{cout Date() endl;_year 1;_month 1;_day 1;}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.Print();return 0; }构造函数可以重载。(本质可以写多个构造函数提供多种初始化方式) #includeiostream using namespace std; class Date { public:1. 无参的构造函数Date(){}也可以写成下面这种Date()//函数名与类名相同且无返回值{cout Date() endl;_year 1;_month 1;_day 1;}2. 有参的构造函数Date(int year, int month, int day){_year year;_month month;_day day;}3. 全缺省的构造函数Date(int year1, int month1, int day1)无参和全缺省的不能同时存在会存在调用歧义{_year year;_month month;_day day;}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;调用无参的构造函数//Date func();这也可以是一个函数声明所以为了区分不能加()d1.Print();Date d2(2023,8,28);调用带参的构造函数d2.Print();对于全缺省的构造函数使用更灵活可以传一个参数两个等Date d3(2023);d3.Print();Date d4(2023, 8);d4.Print();return 0; }注意如果通过无参构造函数创建对象时对象后面不用跟括号例如Date d1() 是错误的 否则就成了函数声明以下代码的函数声明了d1函数该函数无参返回一个日期类型的对象warning C4930: “Date d1(void)”: 未调用原型函数(是否是有意用变量定义的?) 如果类中没有显式定义构造函数则C编译器会自动生成一个无参的默认构造函数一旦用户显式定义编译器将不再生成。 class Date{public:/*// 如果用户显式定义了构造函数编译器将不再生成Date(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;return 0;}将Date类中构造函数注释后代码可以通过编译因为编译器生成了一个无参的默认构造函数将Date类中构造函数放开代码编译失败因为一旦显式定义任何构造函数编译器将不再生成无参构造函数放开后报错error C2512: “Date”: 没有合适的默认构造函数可用 不实现构造函数的情况下编译器会生成默认的构造函数。但是看起来默认构造函数又没什么用d对象调用了编译器生成的默认构造函数但是d对象_year/_month/_day依旧是随机值。也就说在这里编译器生成的默认构造函数并没有什么用 解答C把类型分成内置类型(基本类型)和自定义类型。内置类型就是语言提供的数据类型如int/char…自定义类型就是我们使用class/struct/union等自己定义的类型所有类型的指针都是内置类型 #includeiostream using namespace std; class Time { public:Time(){cout Time() endl;_hour 0;_minute 0;_second 0;} private:int _hour;int _minute;int _second; }; class Date { private:// 基本类型(内置类型)int _year;int _month;int _day;// 自定义类型Time _t; }; int main() {Date d;return 0; }注意C11 中针对内置类型成员不初始化的缺陷又打了补丁即内置类型成员变量在类中声明时可以给默认值。 #includeiostream using namespace std; class Time { public:Time(){cout Time() endl;_hour 0;_minute 0;_second 0;} private:int _hour;int _minute;int _second; }; class Date { private:// 基本类型(内置类型)这个地方不是初始化而是声明声明给的缺省值默认生成的构造函数就会用这个缺省值初始化int _year 2023;int _month 9;int _day 5;// 自定义类型Time _t; }; int main() {Date d;return 0; }无参的构造函数和全缺省的构造函数都称为默认构造函数并且默认构造函数只能有一个。 注意无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数都可以认为 是默认构造函数。它们都有一个共同的特点可以不用传参。默认构造函数只能有一个前面两个在语法上可以构成函数重载但是在无参调用的时候会发生歧义出现调用不明确。 注意要把默认构造函数和默认成员函数区分清楚默认成员函数是我们不写编译器会自动生成的默认构造函数是不需要传参的构造函数。编译器生成的构造函数既是默认构造函数同时也是默认成员函数。 为什么上述说内置类型用的缺省值 #includeiostream using namespace std; class Date { public:Date(){这里_year没有给值而_month _day给了值打印出来是2023/2/1所以声明那给的是缺省值_month 2;_day 1;}void Print(){cout _year / _month / _day endl;} private:int _year 2023;int _month 9;int _day 5; };int main() {Date d1;d1.Print();return 0; }编译器生成的默认构造的特点 不写才会生成写了任意一个构造函数就不会生成了内置类型也叫基本类型语言用关键字定义的类型内置类型的成员不会处理根据编译器有的会有的不会自定义类型的成员才会处理会去调用这个成员的构造函数 总结 一般情况下都需要我们自己写构造函数决定初始化方式成员变量全是自定义类型可以考虑不写构造函数 无参的构造函数和全缺省的构造函数都称为默认构造函数并且默认构造函数只能有一个多个并存在会存在调用的二义性。注意无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数都可以认为是默认构造函数不传参就可以默认调用的。 三、析构函数 #mermaid-svg-lnPOKAcwbK3FTWVq {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-lnPOKAcwbK3FTWVq .error-icon{fill:#552222;}#mermaid-svg-lnPOKAcwbK3FTWVq .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-lnPOKAcwbK3FTWVq .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-lnPOKAcwbK3FTWVq .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-lnPOKAcwbK3FTWVq .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-lnPOKAcwbK3FTWVq .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-lnPOKAcwbK3FTWVq .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-lnPOKAcwbK3FTWVq .marker{fill:#333333;stroke:#333333;}#mermaid-svg-lnPOKAcwbK3FTWVq .marker.cross{stroke:#333333;}#mermaid-svg-lnPOKAcwbK3FTWVq svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-lnPOKAcwbK3FTWVq .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-lnPOKAcwbK3FTWVq .cluster-label text{fill:#333;}#mermaid-svg-lnPOKAcwbK3FTWVq .cluster-label span{color:#333;}#mermaid-svg-lnPOKAcwbK3FTWVq .label text,#mermaid-svg-lnPOKAcwbK3FTWVq span{fill:#333;color:#333;}#mermaid-svg-lnPOKAcwbK3FTWVq .node rect,#mermaid-svg-lnPOKAcwbK3FTWVq .node circle,#mermaid-svg-lnPOKAcwbK3FTWVq .node ellipse,#mermaid-svg-lnPOKAcwbK3FTWVq .node polygon,#mermaid-svg-lnPOKAcwbK3FTWVq .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-lnPOKAcwbK3FTWVq .node .label{text-align:center;}#mermaid-svg-lnPOKAcwbK3FTWVq .node.clickable{cursor:pointer;}#mermaid-svg-lnPOKAcwbK3FTWVq .arrowheadPath{fill:#333333;}#mermaid-svg-lnPOKAcwbK3FTWVq .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-lnPOKAcwbK3FTWVq .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-lnPOKAcwbK3FTWVq .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-lnPOKAcwbK3FTWVq .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-lnPOKAcwbK3FTWVq .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-lnPOKAcwbK3FTWVq .cluster text{fill:#333;}#mermaid-svg-lnPOKAcwbK3FTWVq .cluster span{color:#333;}#mermaid-svg-lnPOKAcwbK3FTWVq 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-lnPOKAcwbK3FTWVq :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 1. 析构函数的概念 析构函数与构造函数功能相反析构函数不是完成对对象本身的销毁局部对象销毁工作是由编译器完成的。而对象在销毁时会自动调用析构函数完成对象中资源的清理工作。 #mermaid-svg-cJbqHKidUGx0RxS6 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-cJbqHKidUGx0RxS6 .error-icon{fill:#552222;}#mermaid-svg-cJbqHKidUGx0RxS6 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-cJbqHKidUGx0RxS6 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-cJbqHKidUGx0RxS6 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-cJbqHKidUGx0RxS6 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-cJbqHKidUGx0RxS6 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-cJbqHKidUGx0RxS6 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-cJbqHKidUGx0RxS6 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-cJbqHKidUGx0RxS6 .marker.cross{stroke:#333333;}#mermaid-svg-cJbqHKidUGx0RxS6 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-cJbqHKidUGx0RxS6 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-cJbqHKidUGx0RxS6 .cluster-label text{fill:#333;}#mermaid-svg-cJbqHKidUGx0RxS6 .cluster-label span{color:#333;}#mermaid-svg-cJbqHKidUGx0RxS6 .label text,#mermaid-svg-cJbqHKidUGx0RxS6 span{fill:#333;color:#333;}#mermaid-svg-cJbqHKidUGx0RxS6 .node rect,#mermaid-svg-cJbqHKidUGx0RxS6 .node circle,#mermaid-svg-cJbqHKidUGx0RxS6 .node ellipse,#mermaid-svg-cJbqHKidUGx0RxS6 .node polygon,#mermaid-svg-cJbqHKidUGx0RxS6 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-cJbqHKidUGx0RxS6 .node .label{text-align:center;}#mermaid-svg-cJbqHKidUGx0RxS6 .node.clickable{cursor:pointer;}#mermaid-svg-cJbqHKidUGx0RxS6 .arrowheadPath{fill:#333333;}#mermaid-svg-cJbqHKidUGx0RxS6 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-cJbqHKidUGx0RxS6 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-cJbqHKidUGx0RxS6 .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-cJbqHKidUGx0RxS6 .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-cJbqHKidUGx0RxS6 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-cJbqHKidUGx0RxS6 .cluster text{fill:#333;}#mermaid-svg-cJbqHKidUGx0RxS6 .cluster span{color:#333;}#mermaid-svg-cJbqHKidUGx0RxS6 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-cJbqHKidUGx0RxS6 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 2. 析构函数的特性 析构函数是特殊的成员函数其特征如下 析构函数名是在类名前加上字符 ~。无参数无返回值类型。一个类只能有一个析构函数。若未显式定义系统会自动生成默认的析构函数。注意析构函数不能重载对象生命周期结束时C编译系统系统自动调用析构函数 温馨提示析构函数不能重载。 后定义先析构 #includeiostream #includeassert.h using namespace std; class Date { public:Date(int year 1, int month 1, int day 1){_year year;_month month;_day day;}~Date(){cout Date() endl;} private:int _year;int _month;int _day; }; class Stack { public:Stack(size_t n4){cout Stack(size_t n4) endl;if (n 0){a nullptr;top capacity 0;}else{a (int*)malloc(sizeof(int) * n);if (a nullptr){perror(realloc fail);exit(-1);}top 0;capacity n;}}void Init(){a nullptr;top capacity 0;}void Push(int x){if (top capacity){size_t newcapacity capacity 0 ? 4 : capacity * 2;int*tmp (int*)realloc(a,sizeof(int) * newcapacity);if (tmp nullptr){perror(realloc fail);exit(-1);}if (tmp a){cout capacity 原地扩容 endl;}else{cout capacity 异地扩容 endl;}a tmp;capacity newcapacity;}a[top] x;}~Stack(){cout ~Stack() endl;free(a);a nullptr;top capacity 0;}int Top(){return a[top - 1];}void Pop(){assert(top 0);--top;}void Destroy(){free(a);a nullptr;top capacity 0;}bool Empty(){return top 0;} private:int* a;int top;int capacity; }; int main() {Date d1;Stack st1;Stack st2;//后定义的先析构return 0; }Stack中的成员变量a、capacity、top都是内置类型对象st1生命周期结束要销毁的时候a和capacity和top是在栈上不需要资源清理最后由系统将其内存回收而a指向的空间是在堆区上申请的这块空间不会随着对象生命周期的结束而自动释放所以会造成内存泄漏因此在对象销毁前要通过析构函数去释放成员变量a指向的空间这就是析构函数的作用。 关于编译器自动生成的析构函数是否会完成一些事情呢 下面的程序我们会看到编译器生成的默认析构函数对自定类型成员调用它的析构函数 #includeiostream using namespace std; class Time { public:~Time(){cout ~Time() endl;} private:int _hour;int _minute;int _second; }; class Date { private:// 基本类型(内置类型)int _year 1970;int _month 1;int _day 1;// 自定义类型Time _t; }; int main() {Date d;return 0; }程序运行结束后输出~Time() 在main方法中根本没有直接创建Time类的对象为什么最后会调用Time类的析构函数 因为main方法中创建了Date对象d而d中包含4个成员变量其中_year, _month, _day三个是内置类型成员销毁时不需要资源清理最后系统直接将其内存回收即可而_t是Time类对象所以在d销毁时要将其内部包含的Time类的_t对象销毁所以要调用Time类的析构函数。 但是main函数中不能直接调用Time类的析构函数实际要释放的是Date类对象所以编译器会调用Date类的析构函数而Date没有显式提供则编译器会给Date类生成一个默认的析构函数目的是在其内部调用Time类的析构函数即当Date对象销毁时要保证其内部每个自定义对象都可以正确销毁main函数中并没有直接调用Time类析构函数而是显式调用编译器为Date类生成的默认析构函数 注意创建哪个类的对象则调用该类的析构函数销毁那个类的对象则调用该类的析构函数如果类中没有申请资源时在堆上申请空间析构函数可以不写直接使用编译器生成的默认析构函数比如Date日期类有资源申请时一定要写否则会造成内存泄漏比如Stack类
http://www.yayakq.cn/news/5875/

相关文章:

  • 闭站保护对网站影响百度装修网站
  • seo网站排名优化服务南京三大软件外包公司
  • 建设网站服务器自营方式网站建设中外链与内链的技巧
  • 用php做网站和go做网站怎么识别网站是用什么语言做的
  • 在线男人和女人做那件事网站软件工程师证书报考要求
  • 网站建设流程体会wordpress查看文章id
  • php网站开发实例视频教程高德vr全景地图下载
  • 初创公司网站设计苏州分销平台是什么意思
  • 相城做网站的公司织梦网站地图自动更新
  • 被墙网站查询flash互动网站开发
  • 对网站建设的意见建议嘉兴优化网站公司哪家好
  • 闵行交大网站建设网业大全
  • 建网站设公司韩国最新新闻事件
  • 网站设计的总结网站的外链接数
  • 长春网站建设公司排名前十wordpress首页广告
  • 什么是部署php网站西红门做网站
  • 淘宝网站开发费用镇江网站设计哪家好
  • 做国外网站做什么内容好VIP视频网站有得做吗
  • 做网站对公司的作用猎头公司联系方式
  • 科技公司网站制作模板wordpress中文版兼容性问题
  • 龙华网站建设设计公司大连网站制作团队
  • 平原县网站seo优化排名郑州好的网站建设公司排名
  • 教育视频网站开发软件项目管理工具
  • 广东微信网站推广哪家专业wordpress 七牛云图床
  • 网站建设和管理是教什么科目杭州服装设计公司
  • 好的网站搭建公司沈阳免费seo关键词优化排名
  • 驾校视频网站模板哪家公司网站做得好
  • 网站建设作业做一个简单的网站成品网站 售卖
  • 泰安可信的网站建设wordpress表单制作
  • 中国国际贸易网站西安手机网站案例