Integer.h
#pragma once
class Integer
{
public:
Integer(int n);
Integer& operator++();//前置++
Integer operator++(int n);//后置++
~Integer(void);
// friend Integer& operator++(Integer& i);//友元前置++
// friend Integer operator++(Integer& i,int n);//友元后置++
void Display() const;
private:
int n_;
};
Integer.cpp
#include "Integer.h"
#include<iostream>
using namespace std;
Integer::Integer(int n):n_(n)
{
}
Integer::~Integer(void)
{
}
//前置++
Integer& Integer::operator++(){
cout<<"Integer& Interger::opertor++()"<<endl;
++n_;
return *this;
}
//后置++
Integer Integer::operator++(int i){
cout<<"后置++Integer Interger::opertor++()"<<endl;
Integer temp(n_);
n_++;
return temp;
}
//友元前置++
/*
Integer& operator++(Integer& i){
cout<<"友元前置++ Integer& operator++(Integer& i)"<<endl;
++(i.n_);
return i;
}
*/
//友元后置++
/*
Integer operator++(Integer& i,int n){
cout<<"友元后置++Integer Interger::opertor++()"<<endl;
Integer temp(i.n_);
i.n_++;
return temp;
}*/
void Integer::Display() const{
cout<<n_<<endl;
}
/main.cpp
#include<iostream>
#include<vector>
using namespace std;
#include "Integer.h"
int main(){
Integer iteger1(10);
iteger1.Display();
Integer iteger2=++iteger1;
iteger1.Display();
iteger2.Display();
Integer iterger3=iteger1++;
iteger1.Display();
iterger3.Display();
cout<<"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"<<endl;
//打印10、9、8、7、。。。。1
vector<int> ivec;
int cnt=10;
while (cnt>0)
{
ivec.push_back(cnt--);
}
vector<int>::iterator iter1=ivec.begin();
while (iter1!=ivec.end())
{
cout<<*iter1++<<endl;
}
return 0;
}