c++类与对象重载

#include<iostream>
using namespace std;
class a{
	private:
		int n;
	public:
		a():n(0){};
		a & operator+(const a& x){
		//	cout<<this->getn()<<endl;
			n+=x.n;
		}
		int getn(){
			return n;
		} 
		void setn(int n){
			this->n=n;
		}
		a & operator++(){
			++n;
			return *this;
		}
		a operator++(int){
			a t = *this;
			n++;
			return t;
		}
		const int&operator*()const{return n;}
		int&operator*(){return n;}
}; 
	void display(const int &rk){
	
	cout<<oct<<rk<<endl;
}
	
int main(){
	
	a b;
	a k;
	b.setn(10);
	k.setn(11);
	b+k;
	//n.getn();
	b.setn(10);
	k.setn(11);
	b.operator++();
	k.operator++();
	cout<<k.getn()<<endl;
    int m=2018;
    display(m);
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_40955914/article/details/80778899