C++学习笔记 写个能够关联的整数类

1 篇文章 0 订阅

题目:

 
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就正常使用的就好。但是这里是实现的关联依然不具有传递性,如果想要具有传递性,恐怕还要有一个函数将所有关联的变量同步,依然很简单,就不再赘述了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值