C++运算符的重载——时间类Time

定义一个时间类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;
}
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中的时间运算符重载可以通过重载的成员函数或者友元函数来实现。下面是一个示例代码,展示了如何重载时间的加法运算符(+)和流插入运算符(<<): ```cpp #include <iostream> class Time { private: int hours; int minutes; public: Time(int h = 0, int m = 0) { hours = h; minutes = m; } Time operator+(const Time& t) const { Time sum; sum.minutes = minutes + t.minutes; sum.hours = hours + t.hours + sum.minutes / 60; sum.minutes %= 60; return sum; } friend std::ostream& operator<<(std::ostream& os, const Time& t) { os << t.hours << " hours, " << t.minutes << " minutes"; return os; } }; int main() { Time t1(2, 30); Time t2(1, 45); Time t3 = t1 + t2; std::cout << "Time 1: " << t1 << std::endl; std::cout << "Time 2: " << t2 << std::endl; std::cout << "Time 1 + Time 2: " << t3 << std::endl; return 0; } ``` 在上面的代码中,我们定义了一个名为Time时间。该具有hours和minutes两个私有成员变量,并且定义了一个构造函数和两个运算符重载函数。 在运算符重载函数中,我们重载了+运算符实现两个时间对象的相加操作。在重载函数中,我们通过创建一个新的Time对象来保存两个时间对象的和,并确保小时与分钟的进位被正确处理。 另外,我们还通过友元函数重载了流插入运算符(<<),以便能够直接使用std::cout输出Time对象。 在主函数中,我们创建了两个Time对象t1和t2,并将它们相加得到t3。然后,我们使用std::cout输出了t1、t2和t3的值。 这只是一个示例,你可以根据实际需求重载其他的运算符,如减法运算符(-)、赋值运算符(=)等等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值