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

本文介绍了一个自定义字符串类String的实现,包括构造函数、复制构造函数、字符串连接、赋值操作以及多种运算符的重载。通过这些功能,可以方便地进行字符串的拼接、比较等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义一个时间类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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值