/********************************/
/* 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;
}