天天看點

【自考】C++程式設計—析構函數案例案例1 案例2 案例3案例4案例5

目錄

案例1 

案例2 

案例3

案例4

案例5

案例1 

#include <iostream>
using namespace std;
class Based
{
     int i;
public:
     Based(int n)
     {
        i=n;
           cout<<"Based 構造函數\n";
     }
     ~Based(){
         cout<<"Based 析構函數\n";
     }
     int geti(){
        return i;
     }
};
int sqr_it(Based b)
{
     return b.geti()*b.geti();//3、有傳回,先析構臨時對象,
}
int main( )
{
     Based x(10);//1、構造函數
     cout<<x.geti()<<endl;//2、輸出x的值
     cout<<sqr_it(x)<<endl;//4、輸出sqr_it(x)的值。
      return 0;//5、析構x對象

  
       //第2種
     Based x(10);//1、構造函數
     cout<<x.geti()<<endl;//2、輸出x的值
     cout<<sqr_it(x)<<endl;//4、輸出sqr_it(x)的值。
      cout<<"…………………………………………"<<endl;//5、輸出………………
     cout<<x.geti()<<endl;//6、輸出x的值
      return 0;//7、析構x對象
}
           
【自考】C++程式設計—析構函數案例案例1 案例2 案例3案例4案例5

案例2 

#include <iostream>
using namespace std;
class Based
{
     int i;
public:
     Based(int n)
     {
        i=n;
           cout<<"Based 構造函數\n";
     }
     ~Based(){
         cout<<"Based 析構函數\n";
     }
     int geti(){
        return i;
     }
};
void sqr_it(Based b)
{
     cout<< b.geti()*b.geti()<<endl;//3、輸出值;4、析構臨時變量b
}
int main( )
{
     Based x(10);//1、構造函數
     cout<<x.geti()<<endl;//2、輸出x的值
     sqr_it(x);//這裡實際是要先調用拷貝構造函數,但程式中未定義拷貝構造函數,是以不輸出。
      return 0;//5、析構x
}
           
【自考】C++程式設計—析構函數案例案例1 案例2 案例3案例4案例5

案例3

//201804真題
#include <iostream>
using namespace std;
class example
{
public:
     example(int n){
           i=n;
           cout<<"Constructing";
     }
     ~example(){
           cout<<"Destructing";
     }
     int get_i(){
           return i;
     }
private:
     int i;
};
int sqr_it(example o){
     return o.get_i()*o.get_i();
}
int main(){
     example x(10);
     cout<<x.get_i()<<endl;
     cout<<sqr_it(x)<<endl;
     return 0;
}
           
【自考】C++程式設計—析構函數案例案例1 案例2 案例3案例4案例5

案例4

#include <iostream>
using namespace std;
class base1{
public:
     base1(){
           cout<<"構造函數"<<endl;
     }
      ~base1(){
        cout<<"析構函數"<<endl;
     }
};
class base2{
public:
     base2(){
           cout<<"構造函數"<<endl;
     }
     ~base2(){
           cout<<"析構函數"<<endl;
     }
};
class base3{
public:
     base3(){
           cout<<"構造函數"<<endl;
     }
     ~base3(){
           cout<<"析構函數"<<endl;
     }
};
class base4{
public:
     base4(){
           cout<<"構造函數"<<endl;
     }
     ~base4(){
           cout<<"析構函數"<<endl; 
     }
};
class derived:public base3,public base2,public base1   //構造先看繼承的順序,再看成員
{
private:
     base4 memobj4;
};
int  main()
{
     derived d;
    
     return 0;
}
           
【自考】C++程式設計—析構函數案例案例1 案例2 案例3案例4案例5

案例5

#include <iostream>
using namespace std;
class B{
private:
     int data,name;
public:
     B(){
           cout<<"B 預設構造函數"<<endl;
     }
     B(int m,int n):data(m),name(n){
           cout<<"B 2個參數,構造函數"<<endl;
           
     }
     ~B(){
           cout<<"B 析構函數"<<endl;
     }
};
class D
{
private:
     int price;
     B b;
public:
     D(){
        cout<<"D 預設構造函數"<<endl;
     }
     D(int p,int te,int w):price(p),b(te,w){
        cout<<"D 3參數,構造函數"<<endl;
     }
     ~D(){
           cout<<"D 析構函數"<<endl;
     }
};
int main()
{  
     D();//類單獨構造和析構。
     D db;   //類的對象,先統一構造,然後統一析構
     D gb(1,2,3);
     return 0;
}
           
【自考】C++程式設計—析構函數案例案例1 案例2 案例3案例4案例5

繼續閱讀