日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++--day05

發(fā)布時間:2023/12/6 c/c++ 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++--day05 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

目錄:?
1. C的提高 1-131P 時間七天?
2. C++的基礎(chǔ) 132-286P 時間八天?
3. C++的提高 287-378P 時間五天?
4. C/C++的數(shù)據(jù)結(jié)構(gòu) 379-482P 時間五天?
5. C/C++的設(shè)計模式基礎(chǔ) 483-540P 時間三天

?

視頻資料:https://www.bilibili.com/video/av27904891?from=search&seid=10891514449061956870


P171 構(gòu)造和析構(gòu)的基礎(chǔ)知識

構(gòu)造函數(shù)

1、構(gòu)造函數(shù)定義及調(diào)用

  1)C++中的類可以定義與類名相同的特殊成員函數(shù),這種與類名相同的成員函數(shù)叫做構(gòu)造函數(shù);

  2)構(gòu)造函數(shù)在定義時可以有參數(shù);

  3)沒有任何返回類型的聲明。

2、構(gòu)造函數(shù)的調(diào)用

  自動調(diào)用:一般情況下C++編譯器會自動調(diào)用構(gòu)造函數(shù)

  手動調(diào)用:在一些情況下則需要手工調(diào)用構(gòu)造函數(shù)

?

析構(gòu)函數(shù)

1、析構(gòu)函數(shù)定義及調(diào)用

  1)C++中的類可以定義一個特殊的成員函數(shù)清理對象,這個特殊的成員函數(shù)叫做析構(gòu)函數(shù)

    語法:~ClassName()

  2)析構(gòu)函數(shù)沒有參數(shù)也沒有任何返回類型的聲明

  3)析構(gòu)函數(shù)在對象銷毀時自動被調(diào)用

  4)析構(gòu)函數(shù)調(diào)用機制

2、C++編譯器自動調(diào)用

?

#include<iostream> using namespace std;class Test { public:Test()//無參數(shù) 構(gòu)造函數(shù) {cout<<"我是構(gòu)造函數(shù)"<<endl;}~Test()//析構(gòu)函數(shù) {cout<<"我是析構(gòu)函數(shù)"<<endl;} private: };//給對象搭建一個舞臺,研究對象的行為 void objplay() {//先創(chuàng)建的對象后釋放 Test t1;Test t2; }void main() {objplay();system("pause"); }

?


?P172? 構(gòu)造和析構(gòu)的用途演示

#include<iostream> using namespace std;class Test { public:Test()//無參數(shù) 構(gòu)造函數(shù) {a=10;//作用:完成對屬性的初始化工作p=(char *)malloc(100);strcpy(p,"aaa");cout<<"我是構(gòu)造函數(shù)"<<endl;}void printfP(){cout<<p<<endl;cout<<a<<endl;}~Test()//析構(gòu)函數(shù) {if (p!=NULL){free(p);}cout<<"我是析構(gòu)函數(shù)"<<endl;} private:int a;char *p; };//給對象搭建一個舞臺,研究對象的行為 void objplay() {//先創(chuàng)建的對象后釋放 Test t1;t1.printfP();printf("分隔符\n");Test t2;t2.printfP(); }void main() {objplay();system("pause"); }

?

?輸出結(jié)果

?


?P173? 構(gòu)造函數(shù)的調(diào)用(無參數(shù)和有參調(diào)用)

#include<iostream> using namespace std;class Test2 { public:Test2() //無參數(shù)構(gòu)造函數(shù) {m_a=0;m_b=0;cout<<"無參數(shù)構(gòu)造函數(shù)"<<endl;}Test2(int a)//有參數(shù)構(gòu)造函數(shù) {m_a=a;m_b=0;cout<<"a:"<<m_a<<endl;printf("這是等號構(gòu)造函數(shù)\n");}Test2(int a,int b)//有參數(shù)構(gòu)造函數(shù) {m_a=a;m_b=b;cout<<"有參數(shù)構(gòu)造函數(shù)"<<endl;}//賦值構(gòu)造函數(shù)(copy構(gòu)造函數(shù))Test2(const Test2 &obj){cout<<"我也是構(gòu)造函數(shù)"<<endl;} public:void printT(){cout<<"普通成員函數(shù)"<<endl;} private:int m_a;int m_b; };//調(diào)用有參數(shù)構(gòu)造函數(shù)3種方法 void main() {//Test2 t1;//調(diào)用無參數(shù)構(gòu)造函數(shù)//1. 括號法Test2 t1(1,2);//C++編譯器自動的調(diào)用構(gòu)造函數(shù) t1.printT();//2. c++對=操作符進(jìn)行了功能增強 Test2 t2=(3,4,5);Test2 t3=5;//3. 直接調(diào)用構(gòu)造函數(shù) 手動的調(diào)用構(gòu)造函數(shù)Test2 t4=Test2(1,2);//匿名對象(匿名對象的去和留) system("pause"); }

?

?輸出結(jié)果:

?


P175? 為什么需要構(gòu)造和析構(gòu)函數(shù)

1、構(gòu)造函數(shù)的調(diào)用方法:自動調(diào)用(按照規(guī)則調(diào)用)

2、顯示的初始化類的屬性或其他資源

#include<iostream> using namespace std;class Test3 { public:void init(int _a,int _b){a=_a;b=_b;} protected: private:int a;int b; };void main() {//類沒有提供構(gòu)造函數(shù) C++編譯器會自動給你提供一個默認(rèn)的構(gòu)造函數(shù)//類沒有提供copy構(gòu)造函數(shù) C++編譯器會自動給你提供一個默認(rèn)的copy構(gòu)造函數(shù) Test3 t1;t1.init(2,3);Test3 tArray[3];tArray[0].init(1,2);tArray[1].init(1,2);tArray[2].init(1,2);//在這種場景之下 顯示的初識化方案顯得很蹩腳Test3 t21;t21.init(1,2);Test3 t22;t22.init(1,2);Test3 t23;t23.init(1,2);Test3 tArray2[3]={t21,t22,t23};//在這種場景下 滿足不了編程需要//Test3 tArray2[1999]={t21,t22,t23}; system("pause"); }

?


?P176? copy構(gòu)造函數(shù)調(diào)用時機1和2

#include<iostream> using namespace std;class Test4 { public:Test4() //無參數(shù)構(gòu)造函數(shù) {m_a=0;m_b=0;cout<<"無參數(shù)構(gòu)造函數(shù)"<<endl;}Test4(int a)//有參數(shù)構(gòu)造函數(shù) {m_a=a;m_b=0;cout<<"a:"<<m_a<<endl;printf("這是等號構(gòu)造函數(shù)\n");}Test4(int a,int b)//有參數(shù)構(gòu)造函數(shù) {m_a=a;m_b=b;cout<<"有參數(shù)構(gòu)造函數(shù)"<<endl;}//賦值構(gòu)造函數(shù)(copy構(gòu)造函數(shù))Test4(const Test4 &obj){cout<<"我也是構(gòu)造函數(shù)"<<endl;m_b=obj.m_b+100;m_a=obj.m_a+100;} public:void printT(){cout<<"普通成員函數(shù)"<<endl;cout<<"m_a:"<<m_a<<endl;} private:int m_a;int m_b; };//1. 賦值構(gòu)造函數(shù) 用1個對象去初始化另外一個對象 void main_number1() {Test4 t1(1,2);Test4 t0(1,2);t0=t1;//用t1給t0 賦值操作和初始化是兩個不同的概念//第一種 調(diào)用方法Test4 t2=t1;//用t1初始化t2 t2.printT();system("pause"); } //2. 第二種調(diào)用時機 void main() {Test4 t1(1,2);Test4 t0(1,2);Test4 t2(t1);//用t1對象初始化t2對象 t2.printT(); }

?


?P178? ?copy構(gòu)造函數(shù)調(diào)用時機3

#include<iostream> using namespace std;class Location { public:Location( int xx = 0 , int yy = 0 ) { X = xx ; Y = yy ; cout << "Constructor Object.\n"<<endl ; }Location( const Location & p ) //拷貝構(gòu)造函數(shù) 完成對象的初始化 { X = p.X ; Y = p.Y ; cout << "Copy_constructor called." << endl ; }~Location() { cout << X << "," << Y << " Object destroyed." << endl ; }int GetX () { return X ; } int GetY () { return Y ; } private : int X , Y ; } ;//業(yè)務(wù)函數(shù) 形參是一個元素 void f ( Location p ) { cout << "Funtion:" << p.GetX() << "," << p.GetY() << endl ; }void mainobjplay() { Location a(1,2);Location b=a; //形參是一個元素,函數(shù)調(diào)用,會執(zhí)行實參變量初始化形參變量 f(b);//b實參去初始化形參p,會調(diào)用copy構(gòu)造函數(shù) } void main() { mainobjplay();system("pause"); }

?輸出結(jié)果:

?

轉(zhuǎn)載于:https://www.cnblogs.com/yangyuqing/p/10402295.html

總結(jié)

以上是生活随笔為你收集整理的C++--day05的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。