定义一个时间类Time,通过运算符重载实现时间的比较(关系运算)、时间增加/减少若干秒(+=和-=)、时间增加/减少1秒(++和–)、计算两个时间相差的秒数(-)以及输出时间对象的值(时—分—秒)。
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
class String
{
//运算符的重载
friend String operator+(const String& s1, const String& s2)
{//新建对象,拷贝两个数据,返回新空间
String newstring;
newstring.len = s1.len + s2.len;
newstring.data = new char[newstring.len + 1];
strcpy(newstring.data, s1.data);
strcat(newstring.data, s2.data);
return newstring;
}
friend ostream& operator<<(ostream& os, const String& obj);
friend istream& operator>>(istream& is, String& obj);
friend bool operator>(const String& s1, const String& s2);
friend bool operator>=(const String& s1, const String& s2);
friend bool operator==(const String& s1, const String& s2);
friend bool operator!=(const String& s1, const String& s2);
friend bool operator<(const String& s1, const String& s2);
friend bool operator<=(const String& s1, const String& s2);
private:
int len;//String 的长度
char* data;//指向字符串的指针
public:
String(const char* s = NULL);//构造函数
String(const String& other);//复制构造函数
String& operator+=(const String& other);//字符串连接
//String& operator+(const String& other);
String& operator=(const String& other);//字符串赋值
char& operator[](int index);//下标的操作,返回引用
~String() {};//析构函数
};
String::String(const char* s)//构造函数
{
if (s == NULL)
data = new char[1];
else
{
len = strlen(s);
data = new char[len + 1];
strcpy(data, s);
}
}
String::String(const String& other)//复制构造函数
{
len = other.len;
data = new char[len + 1];
strcpy(data, other.data);
}
String& String::operator+=(const String& other)
{
//重新分配空间,拷贝两个数据,删除自己原有的空间,赋值到新的空间,返回引用
len = other.len + len;
char* newdata = new char[len + 1];
strcpy(newdata, data);
strcat(newdata, other.data);
delete[] data;
data = newdata;
return *this;
}
String& String::operator=(const String& other)
{
if (this == &other)
return *this;
delete[] data;//删除原有的数据
len = other.len;//拷贝
data = new char[len + 1];
strcpy(data, other.data);
return *this;
}
char& String::operator[](int index)//把char的下标去掉
{
if (index > len)
return data[len - 1];
else
return data[len];
}
bool operator>(const String& s1, const String& s2)
{
if (s1.len >=s2.len)
return true;
else
return false;
}
bool operator<(const String& s1, const String& s2)
{
if (s1.len < s2.len)
return true;
else
return false;
}
bool operator==(const String& s1, const String& s2)
{
if (strcmp(s1.data, s2.data) == 0)
return true;
else
return false;
}
bool operator!=(const String& s1, const String& s2)
{
if (strcmp(s1.data, s2.data) != 0)
return true;
else
return false;
}
bool operator>=(const String& s1, const String& s2)
{
if (s1.len >= s2.len)
return true;
else
return false;
}
bool operator<=(const String& s1, const String& s2)
{
if (s1.len <= s2.len)
return true;
else
return false;
}
ostream& operator<<(ostream& os, const String& obj)
{
os << obj.data;
return os;
}
istream& operator>>(istream& is, String& obj)
{
char a[10000];
is >> a;
delete[] obj.data;
obj.len = strlen(a);
obj.data= new char[obj.len + 1];
strcpy(obj.data, a);
return is;
}
int main()
{
String s1, s2;
cout << "输入第一个String:" << endl;
cin >> s1;
cout << "输入第二个String:" << endl;
cin >> s2;
String temp;
temp = s1;
cout << "将两个String相加之一:" << endl;
cout << s1+s2 << endl;
cout << "将两个String相加之二:" << endl;
s1 += s2;
cout << s1 << endl;
s1 = temp;
cout << "现在把s2的值赋值给s1" << endl;
s1 = s2;
cout << s1 << endl;
s1 = temp;
cout << "现在来比较字符串" << endl;
if (s1 == s2)
cout << "s1==s2" << endl;
if (s1 != s2)
cout << "s1!=s2" << endl;
if (s1 > s2)
cout << "s1>s2" << endl;
if (s1 < s2)
cout << "s1<s2" << endl;
if (s1 >= s2)
cout << "s1>=s2" << endl;
if (s1 <= s2)
cout << "s1<=s2" << endl;
}