c++重载运算符@

                                            c++重载运算符@

  刚刚将c++的重载运算符学完,虽然也不是挺会,但也能有小小心得吧!得意

   重载运算符有友元和静态两种方式使用吧!
   友元:
   
class FeetInches
{
private:
	int feet; // 英尺
	int inch; // 英寸
public:
	FeetInches(int f = 0, int i = 0)//构造函数
	{
		feet = f; inch = i;
	}
	friend FeetInches hanshu(FeetInches& a);
};
FeetInches hanshu(FeetInches& a)
{
	cout << a.feet << " " << a.inch << endl;
}

友元的意思是,一个类的朋友,它能够使用类里的任何元素,相当于没了权限一样。但是它只是朋友而已,所以并不要用FeetInches::hanshu()这样。这也是许多初学者较难想明白的地方吧!还有的是要注意友元是没有隐含的this指针的,所以你想要友元函数用class中的元素必须要有参数在()内,且要是类的类型,为什么一定要是类类型,若是不是要用class的元素我又何必要难为自己写个友元的东西,对吧?

静态:

class FeetInches
{
private:
	int feet; // 英尺
	int inch; // 英寸
public:
	FeetInches(int f = 0, int i = 0)//构造函数
	{
		feet = f; inch = i;
	}
	FeetInches hanshu();
};
void FeetInches::hanshu()//(1)
{
	cout << feet << " " << inch << endl;
	cout << this->feet << " " << this->inch << endl;
}

静态就是直接用,不要友元这种方式来表示。与友元不同的是它的函数(1)写在外面是与友元有很大区别的。它写在外面是要用 FeetInches::(::为域的意思)来表示的,要表明它是FeetInches类内的函数。然而函数前面肯定有一个类型的,这里用一个void的类型来表示函数是无返回的类型,就是不用return。还有的是他有一个this指针隐藏起来了。this指的就是你( FeetInches a;a.hanshu();)a的地址值。说了这么多应该明白了吧!现在来谈谈重载了。

class FeetInches
{
private:
	int feet; // 英尺
	int inch; // 英寸
public:
	FeetInches(int f = 0, int i = 0)
	{
		feet = f; inch = i;
	}
	FeetInches operator+(const FeetInches& a);//重载加法
	friend  ostream& operator<<(ostream& out, const FeetInches& a);//重载输出符
	bool operator>(const FeetInches& a);//重载大于号
	FeetInches operator*(const int a);//重载乘号
	FeetInches& operator++();//重载先++
	FeetInches operator++(int);//重载后++
};
FeetInches& FeetInches::operator++()
{
	inch++;
	if (inch >= 12)feet += inch / 12, inch %= 12;
	return *this;
}
FeetInches FeetInches::operator++(int)
{
	FeetInches temp=*this;
	inch++;
	if (inch >= 12)feet += inch / 12, inch %= 12;
	return temp;
}
bool FeetInches::operator>(const FeetInches& a)
{
	if (feet > a.feet)return true;
	if (feet < a.feet)return false;
	return inch>a.inch ? true : false;
}
FeetInches FeetInches::operator+(const FeetInches& a)
{
	feet += a.feet;
	inch += a.inch;
	return *this;
}
FeetInches FeetInches::operator*(const int a)
{
	feet = a*feet;
	inch = a*inch;
	if (inch >= 12)feet += inch / 12, inch %= 12;
	return *this;
}
ostream& operator<<(ostream& out,const FeetInches& a)
{
	out << a.feet << " " << a.inch << endl;
	return out;
}
(2)
FeetInches a(2,4),b(6,8),c,d;
 cout << a + b << endl;
 d=c++;
 cout << c++ << " " << ++c << endl;


重载就是实现与常用的int的类型差不多,就是为了方便与美观而生的,感觉科学家们真伟大啊!
为什么要重载,因为一个类里面我要(2)里面的操作重载就是想int 一样一样的,int a=1+2;a++;一样多方便啊!
但是若不用重载,那我们就要自己写一个获取feet,inch的函数,因为这些元素都是私有的。而实现相加就每次都要写一个函数来调用它如(a+b):
FeetInches add(FeetInches& a,FeetInches& a)
{
<span style="white-space:pre">	</span>b.getfeet() += a.getfeet();
<span style="white-space:pre">	</span>b.getinch() += a.getinch();
<span style="white-space:pre">	</span>return *this;//上面说过
}//getfeet();getinch();为获取私有成员的函数
FeetInches a(1,2),b(3,4);
add(a,b);//每次相加都要写add()的函数调用

看了上面的例子是否觉得重载很有用呢?第一次打这么多的字,累了。看完上面的的解析剩下的重载应该看的懂了吧!




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值