1. 程式人生 > 實用技巧 >【C++學習教程09】過載

【C++學習教程09】過載

目錄

參考

  • 範磊C++(第11課時)
  • VS 2015

筆記

實現成員的自加

  • 通過編寫成員函式
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
class num
{
public:
	num() { n = 1; }
	~num() {}
	int get() const { return n; }
	void set(int x) { n = x; }
	void add() { ++n; }//物件的自加功能(實際上是成員變數)
private:
	int n;
};

int main(int argc, const char * argv[]) {
	num i;
	cout << "i:"<<i.get()<<endl;
	i.add();
	cout << "i:" << i.get() << endl;//通過呼叫add函式實現了自加,但是由於還要在類中定義函式比較麻煩。
	system("pause");
	return 0;
}
  • 過載運算子
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
class num
{
public:
	num() { n = 1; }
	~num() {}
	int get() const { return n; }
	void set(int x) { n = x; }
	void add() { ++n; }
	void operator ++() { ++n; }//相當於過載函式,函式體中是實現部分。
private:
	int n;
};

int main(int argc, const char * argv[]) {
	num i;
	cout << "i:"<<i.get()<<endl;
	i.add();
	cout << "i:" << i.get() << endl;//通過呼叫add函式實現了自加,但是由於還要定義函式比較麻煩。
	++i;
	cout << "i:" << i.get() << endl;//過載運算子。
	system("pause");
	return 0;
}

後置自加 :連結: csdn.

  • 建立臨時物件

將自加後的物件傳遞給另一個物件。

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
class num
{
public:
	num() { n = 1; }
	~num() {}
	int get() const { return n; }
	void set(int x) { n = x; }
	void add() { ++n; }
	num operator ++() //返回一個物件
	{
		++n;
		num t;//臨時物件
		t.set(n);
		return t;
	}
private:
	int n;
};

int main(int argc, const char * argv[]) {
	num i;
	cout << "i:"<<i.get()<<endl;
	i.add();
	cout << "i:" << i.get() << endl;//通過呼叫add函式實現了自加,但是由於還要定義函式比較麻煩。
	++i;
	cout << "i:" << i.get() << endl;//過載運算子。
	num n = ++i;//傳遞給一個物件 //返回num類的臨時物件,然後初始化給n
	cout << "n:" << n.get() << endl;
	system("pause");
	return 0;
}
  • 建立無名臨時物件【有缺陷】
    不用建立臨時物件,用帶引數的建構函式。
#include <iostream>
using namespace std;
class num
{
public:
    num() { n = 1;cout<<"建構函式執行"<<endl; }
    num(int i){n=i;cout<<"帶引數的建構函式執行"<<endl;}
    ~num() {cout<<"解構函式執行"<<endl;}
    int get() const { return n; }
    void set(int x) { n = x; }
    void add() { ++n; }
    num operator ++() //返回一個物件
    {
        ++n;
        return num(n);//呼叫並且返回帶引數的建構函式建立的臨時物件,並且踏實沒有名字的。//這種方法仍然不可取,因為物件的建立和析構都會浪費時間
    }
private:
    int n;
};

int main(int argc, const char * argv[]) {
    num i;
    cout << "i:"<<i.get()<<endl;
    i.add();
    cout << "i:" << i.get() << endl;//通過呼叫add函式實現了自加,但是由於還要定義函式比較麻煩。
    ++i;
    cout << "i:" << i.get() << endl;//過載運算子。
    num n = ++i;//傳遞給一個物件 //返回num類的臨時物件,然後初始化給n
    cout << "n:" << n.get() << endl;
    return 0;
}

輸出結果:

  • 取消建立臨時物件
    建立一個物件需要犧牲一定的速度和一定的記憶體空間,但是我們只是要實現成員變數的自加。
#include <iostream>
using namespace std;
class num
{
public:
    num() { n = 1;cout<<"建構函式執行"<<endl; }
    num(int i){n=i;cout<<"帶引數的建構函式執行"<<endl;}
    num(const num&s){this->n=s.n;cout<<"複製造函式執行"<<endl;}
    ~num() {cout<<"解構函式執行"<<endl;}
    int get() const { return n; }
    void set(int x) { n = x; }
    void add() { ++n; }
    num operator ++() //返回一個物件
    {
        ++n;
        return *this;//用this來替換創造臨時物件
        //返回的是原始物件而不是臨時物件,不用呼叫帶引數的建構函式
        //把返回的原始物件返回給新建的物件
        //i->相當於複製了
    }
private:
    int n;
};

int main(int argc, const char * argv[]) {
    num i;
    cout << "i:"<<i.get()<<endl;
    i.add();
    cout << "i:" << i.get() << endl;//通過呼叫add函式實現了自加,但是由於還要定義函式比較麻煩。
   // ++i;
    //cout << "i:" << i.get() << endl;//過載運算子。
    num n = ++i;//傳遞給一個物件 //返回num類的臨時物件,然後初始化給n
    cout << "n:" << n.get() << endl;
    return 0;
}

輸出物件:

過載運算子

  • 加法運算子
#include <iostream>
using namespace std;
class num
{
public:
    num() { n = 1;cout<<"建構函式執行"<<endl; }
    num(int i){n=i;cout<<"帶引數的建構函式執行"<<endl;}
    num(const num&s){this->n=s.n;cout<<"複製造函式執行"<<endl;}
    ~num() {cout<<"解構函式執行"<<endl;}
    int get() const { return n; }
    void set(int x) { n = x; }
    num add(const num&r) {
        return num(n+r.get());
        
    }
    num operator ++() //返回一個物件
    {
        ++n;
        return *this;//用this來替換創造臨時物件
        //返回的是原始物件而不是臨時物件,不用呼叫帶引數的建構函式
        //把返回的原始物件返回給新建的物件
        //i->相當於複製了
    }
private:
    int n;
};

int main(int argc, const char * argv[]) {
    num one(1), two(2), three;
    three=one.add(two);
    cout<<"one:"<<one.get()<<endl;
    cout<<"two:"<<two.get()<<endl;
    cout<<"three:"<<three.get()<<endl;
    return 0;
}

輸出結果:

這種加法不提倡使用
課件出現損壞無法觀看

  • 過載賦值運算子
    課件損壞
  • 轉換型別運算子
#include <iostream>
using namespace std;
class A
{
public://建構函式只有一個引數,與下面的對應!
    A(int x){i=x;cout<<"建構函式執行"<<i<<endl;}
    ~A(){cout<<"解構函式執行"<<i<<endl;}
    void get(){cout<<i<<endl;}
private:
    int i;
};

int main(int argc, const char * argv[]) {
    A a(33);
    a.get();
    a=1000;//這時候就會創造臨時物件(帶引數的建構函式)
    a.get();
    a=A(2);//強制型別表示式//也是創造臨時物件
    a.get();
    return 0;
}

輸出結果:

此後三節課件均損壞

過載的限制: CSDN.