1. 程式人生 > >C++學習筆記 寫個能夠關聯的整數類

C++學習筆記 寫個能夠關聯的整數類

題目:

 
int main() { 
    Integer a, b; 
    cin >> a >> b; 
    cout << a << " " << b << endl; 
    link(a, b); 
    a -= 100; 
    cout << a << " " << b << endl; 
    b += a; 
    cout << a << " " << b << endl; 
} 

以上是主程式,需要寫個名為Integer的類使得main函式能正常執行。

EXAMPLE INPUT

1000 2000
EXAMPLE OUTPUT
1000 2000
900 1900
1800 2800

解析:

本題主要考察兩個點,一個是對於幾個操作符的過載,另一個就是對於link函式的實現。我們這裡的link先考慮如果只用一個指標來實現,也就是說每個Integer物件只能和一個相關聯。

#include <iostream>
#include <vector>

using namespace std;


class Integer{
private:
	int value;
	int iflinked;
	Integer* linked;
public:
	Integer(){
		value = 0;
		iflinked  = 0;
	}

    int getValue() const{
        return this->value;
    }

    void setValue(int x){
        this->value = x;
    }

	void operator -=(int n){
		this->value -= n;
		if(iflinked)
            this->linked->value -= n;
	}

	void operator +=(Integer & x){
		this->value += x.value;
		if(iflinked)
			this->linked->value += x.value;
	}
    friend void link(Integer & a, Integer & b);

};

void link(Integer & a, Integer & b){
    a.linked = &b;
    a.iflinked = 1;
    b.linked = &a;
    b.iflinked = 1;
}

istream & operator >> (istream & in, Integer & x){
	int n;
	in >> n;
	x.setValue(n);
	return in;
}
ostream & operator << (ostream & out, const Integer & x){
	out << x.getValue();
	return out;
}

這裡的set函式和get函式是為了類外面的輸入輸出操作符能夠訪問private變數而存在的;link宣告為友元函式也是為了寫在外面能夠訪問private變數。

另外,輸入輸出操作符在operator前必須加引用&,這是c++的規定,用std::ifstream,std::ofstream作為函式引數傳遞時,必須通過引用傳遞,因為其copy方法被私有化,從而保證物件的唯一性。

不然會報錯:

error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private。

拓展:

那麼能否實現可以關聯多個變數呢,這裡就用到了存放指標的vector。

#include <iostream>
#include <vector>

using namespace std;


class Integer{
private:
	int value;
	int linkedNumber;
	vector<Integer*> linked;
public:
	Integer(){
		value = 0;
		linkedNumber  = 0;
	}

    int getValue() const{
        return this->value;
    }

    void setValue(int x){
        this->value = x;
    }

	void operator -=(int n){
		this->value -= n;
		if(linkedNumber){
			int i;
			for(i = 0; i < linkedNumber; i++)
				this->linked[i]->value -= n;
		}

	}

	void operator +=(Integer & x){
		this->value += x.value;
		if(linkedNumber){
			int i;
			for(i = 0; i < linkedNumber; i++)
				this->linked[i]->value += x.value;
		}
	}

    friend void link(Integer & a, Integer & b);

};

void link(Integer & a, Integer & b){
    a.linked.push_back(&b);
    a.linkedNumber++;
    b.linked.push_back(&a);
    b.linkedNumber++;
}


istream & operator >> (istream & in, Integer & x){
int n;
in >> n;
x.setValue(n);
return in;
}
ostream & operator << (ostream & out, Integer & x){
out << x.getValue();
return out;
}


int main() {
    Integer a, b, c;
    cin >> a >> b >> c;
    cout << a << " " << b << " " << c << endl;
    link(a, b);
    link(a, c);
    a -= 100;
    cout << a << " " << b << " " << c <<endl;
    b += a;
    cout << a << " " << b << " " << c <<endl;
}

存放指標的vector就正常使用的就好。但是這裡是實現的關聯依然不具有傳遞性,如果想要具有傳遞性,恐怕還要有一個函式將所有關聯的變數同步,依然很簡單,就不再贅述了。