c++函数符重载


/********************************/
/*            c++函数符重载           */
/********************************/
#include<iostream>
using namespace std;
class FeetInches
{
private:
 int feet; // 英尺
 int inch; // 英寸
public:
 FeetInches(int f = 0, int i = 0)//构造函数
 {
  feet = f; inch = i;
 }
 void Printf()
 {
  cout << feet << " " << inch << endl;
 }
 //友元函数&重载
 friend FeetInches operator+(FeetInches& a, FeetInches& b);
 friend FeetInches operator*(FeetInches& a, int b);
 friend bool operator>(FeetInches& a, FeetInches& b);
};
FeetInches operator+(FeetInches& a, FeetInches& b)//重载加法
{
 int yc, yz;
 yz = a.feet + b.feet;
 yc = a.inch + b.inch;
 if (yc >= 12) yz = yc / 12, yc %= 12;
 return FeetInches(yz, yc);
}
FeetInches operator*(FeetInches& a,int b)//重载乘法
{
     a.inch = a.inch*b;
  a.feet = a.feet*b;
 if (a.inch >= 12)a.feet += a.inch / 12, a.inch %= 12;
 return FeetInches(a.feet, a.inch);
}
bool operator>(FeetInches& a, FeetInches& b)//大于号重载
{
 if (a.feet > b.feet)return true;
 else if (a.feet < b.feet)return false;
 return a.inch>b.inch ? true : false;
}
int main()
{
 FeetInches m(1,2),n(3,4),i(10,20),j(12,23),t;
 t = m + n;
 t.Printf();
 t = i + j;
 t.Printf();
 t = i * 2;
 t.Printf();
 if (m > n)
 {
  cout << "YES" << endl;
 }
 else
 {
  cout << "NO" << endl;
 }
 return 0;
}


#include<iostream>
using namespace std;
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;
}
int main()
{
 FeetInches a(2,4),b(6,8),c,d;
 cout << a + b << endl;
 d=c++;
 cout << c++ << " " << ++c << endl;
 return 0;
}

#include<iostream>
using namespace std;
class List
{
private:
	int* elem;
	int capacity;
	int n;
public:
	List(int c = 100)
	{
		capacity = c;
		n = 0;
		elem = new int[c];//不赋初值会使b【i】=1;的不能够正常输出,因为n值为0
		for(int i=0;i<c;++i)
		*(elem+i)=NULL;
	}
	bool add(int e)
	{
		if (n >= capacity)
		{
			int* a = new int[capacity + 100];
			if (a == NULL) return false;
			for (int i = 0; i < n; i++) a[i] = elem[i];
			delete[] elem;
			elem = a;
			capacity += 100;
		}
		elem[n++] = e;
		return true;
	}
	friend ostream& operator<<(ostream& out, const List& a);
	// 重载=运算符
	List& operator=(const List& a);
	int& operator[](int i);
	// 重载[]运算符
};
ostream& operator<<(ostream& out, const List& a)
{
	for (int i = 0; i < a.n; ++i)
		out << a.elem[i] << " ";
	out << endl;
	return out;
}
List& List::operator=(const List& a)
{
	n=a.n;
	capacity=a.capacity;
	for (int i=0;i<a.n;++i)
	{
		elem[i]=a.elem[i];
	}
	return *this;
}
int& List::operator[](int i)
{
	if(elem[i]==NULL)n++;
	return elem[i];
}
//并采用以下代码测试
// 重载<<运算符
int main()
{
	List a;
	a.add(4);  a.add(5);   a.add(2);
	cout << "a=" << a << endl;
	a[0] = 3;
	cout << "a=" << a << endl;
	List b;
	b[0] = 1; b[1] = 2; b[2] = 3;
	cout << "b=" << b << endl;
	b = a;
	b[0] = 10;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值