运算符重载之先加和后加++以及【】的运用

#include"aa.h"
#include<iostream.h>
#include<stdio.h>
#include<string.h>


class bb
{
public:
char *i;

bb ()
{this->i=0;
cout<<("no pram!\n");
}
bb (bb &b)//
{this->i=new char[10];
strcpy(this->i,b.i);
cout<<("深度复制!\n");
}


bb (char *i)//深度复制
{this->i=new char[10];
strcpy(this->i,i);
cout<<("with pram!\n");
}
~bb()//析构函数
{
if(0!=this->i)
delete this->i;
cout<<("destry!\n");
}
bb & operator=(bb &b)//对于要不要反悔值,是看如果在main中a=a1,就不要返回会也可以,如果是这样的a=a1=a2,就则就


定要反回值
{
this->i=new char[10];
strcpy(this->i,b.i);
return *this;
}
void operator ++()
{
(*this->i)++;
}
};


ostream & operator<<(ostream &b1,bb &b2)
{
b1<<b2.i<<endl;
return b1;
}


void operator>>(istream &b1,bb &b2)
{
b2.i=new char[10];
b1>>b2.i;


}


int main()
{


bb a1;
// a1=a;
// bb a2=a1;
 cin>>a1;
 a1++;
cout<<a1;


return 0;
}
其实++这里就是函数,在c里++,就是+1,
如果这里输入aa,则执行后就变成ba,




#include"aa.h"
#include<iostream.h>
#include<stdio.h>
#include<string.h>


class bb
{
public:
char *i;
int j;
bb ()
{this->j=0;
this->i=0;
cout<<("no pram!\n");
}
bb (bb &b)//
{this->i=new char[10];
strcpy(this->i,b.i);
cout<<("深度复制!\n");
}


bb (char *i)//深度复制
{this->i=new char[10];
strcpy(this->i,i);
cout<<("with pram!\n");
}
~bb()//析构函数
{
if(0!=this->i)
delete this->i;
cout<<("destry!\n");
}
bb & operator=(bb &b)//对于要不要反悔值,是看如果在main中a=a1,就不要返回会也可以,如果是这样的a=a1=a2,就则就


定要反回值
{
this->i=new char[10];
strcpy(this->i,b.i);
return *this;
}
int  operator ++()//前缀
{
return this->j++;
}
int  operator ++(int)//后缀
{
int temp=this->j;
this->j++;
return  temp;
}

};


ostream & operator<<(ostream &b1,bb &b2)
{
b1<<b2.i<<endl;
return b1;
}


void operator>>(istream &b1,bb &b2)
{
b2.i=new char[10];
b1>>b2.i;


}


int main()
{
int j;
bb a1;


 j=++a1;//前缀
cout<<j<<endl;
 j=a1++;//后缀
 cout<<j<<endl;
  
return 0;






}


[]重载
#include"aa.h"
#include<iostream.h>
#include<stdio.h>
#include<string.h>


class bb
{
public:
char *i;
int j;
bb ()
{this->j=0;
this->i=0;
cout<<("no pram!\n");
}
bb (bb &b)//
{this->i=new char[10];
strcpy(this->i,b.i);
cout<<("深度复制!\n");
}


bb (char *i)//深度复制
{this->i=new char[10];
strcpy(this->i,i);
cout<<("with pram!\n");
}
~bb()//析构函数
{
if(0!=this->i)
delete this->i;
cout<<("destry!\n");
}
bb & operator=(bb &b)//对于要不要反回值,是看如果在main中a=a1,就不要返回会也可以,如果是这样的a=a1=a2,就则就定要反回值
{
this->i=new char[10];
strcpy(this->i,b.i);
return *this;
}
int  operator ++()//前缀
{
return this->j++;
}
int  operator ++(int)//后缀
{
int temp=this->j;
this->j++;
return  temp;
}
char operator [](int i1)
{


return  this->i[i1];
}

};


ostream & operator<<(ostream &b1,bb &b2)
{
b1<<b2.i<<endl;
return b1;
}


void operator>>(istream &b1,bb &b2)
{
b2.i=new char[10];
b1>>b2.i;


}


int main()
{


bb a1;
cin>>a1;
char ch=a1[2];


cout<<ch;


  
return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值