星球的眼睛 发表于 2025-3-12 18:47:18

C++【类和对象】(超具体!!!)

1.运算符重载

(1).C++规定类范例运算符使用时,必须转换成调用运算符重载。
(2).运算符重载是具有特殊名字的函数,名字即是operator加需要使用的运算符,具有返回范例和参数列表及函数体。
(3).重载运算符函数的参数个数和该运算符作用的运算对象的个数保持一致,一元运算符有一个参数,二元运算符有两个参数,二元运算符左边的传给第一个参数,右边的传给第二个参数。
(4).假如一个重载运算符函数是成员函数,那么它的第一个函数参数是this指针,以是显示出来就少一个参数。
(5).运算符重载以后,其优先级和结合性与对应的内置范例运算符保持⼀致。
(6).不能通过毗连语法中没有的符号来创建新的操作符:比如operator@
(7).
https://i-blog.csdnimg.cn/direct/0b1ab37dd43a4a0fac0f38530b75e8ff.png这五个运算符不能重载。
(8).C++为了区分前置++和后置++运算符重载函数,规定前置为operator++(),后置++为operator++(int)
#include<iostream>
using namespace std;
class Date
{
public:
        Date(int year, int month, int day)
        {
                _year = year;
                _month = month;
                _day = day;
        }
        bool operator == (const Date& d)
        {
                return _year == d._year
                        && _month == d._month
                        && _day == d._day;
        }

//d1-d2
int        operator-(const Date& d);
//日期-天数
Date operator-(int day);
private:
        int _year;
        int _month;
        int _day;


};
int main()
{
        Date d1(2024, 3, 20);
        Date d2(2025, 3, 18);
        if (d1 == d2)
        {
                cout << "相等" << endl;
        }
        else
                cout << "不相等" << endl;
        return 0;
}
2.赋值运算符重载

赋值运算符重载是一个默认成员函数(不能重载到全局),用于完成两个已经存在的对象直接的拷贝赋值。
赋值运算符重载的特点:
(1).赋值运算符重载是一个函数重载,规定重载必须为成员函数,赋值运算重载的参数发起写成const 当前类范例引用,否则会传值传参会有拷贝。
(2).有返回值,且发起写成当前类范例引⽤,引⽤返回可以提高效率,有返回值目标是为了支持连续赋值场景。
(3).没有显式实现时,编译器会自动生成⼀个默认赋值运算符重载,默认赋值运算符重载行为跟默认拷贝构造函数类似,对内置范例成员变量会完成值拷贝/浅拷贝(⼀个字节⼀个字节的拷贝),对自界说范例成员变量会调用他的赋值重载函数。
(4).假如一个类显示实现析构并开释了资源,就需要我们显示实现赋值运算符重载,否则就不需要。
#include<iostream>
using namespace std;
class Date
{
public:
        Date(int year, int month, int day)
        {
                _year = year;
                _month = month;
                _day = day;
        }
        Date(const Date& d)
        {
                _year = d._year;
                _month = d._month;
                _day = d._day;
        }
        Date& operator=(const Date& d)
        {
                if (this != &d)
                {

                        _year = d._year;
                        _month =d._month;
                        _day = d._day;

                }
                return*this;

        }
        void Print()
        {
                cout << _year << "-" << _month << "-" << _day << endl;
        }
private:
        int _year;
        int _month;
        int _day;
};
int main()
{
        Date d1(2004,8,18);
        Date d2(d1);
        d1.Print();
        d2.Print();

        return 0;
}
3.日期类的实现

//Date.h
#include<iostream>
using namespace std;
class Date
{
public:
        Date(int year, int month, int day)
        {
                _year = year;
                _month = month;
                _day = day;
        }
        int GetMonthDay(int year,int month)
        {
                int monthDayArray = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
                if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
                {
                        return 29;
                }
                else
                        return monthDayArray;
        }
        Date& operator+=(int day);
        Date operator+(int day);
        Date& operator-=(int day);
        Date operator-(int day);

        // ++d1 ->d1.operator++()
        Date& operator++();
        // d1++ ->d1.operator++(1);
        Date operator++(int);

        Date& operator--();
        Date operator--(int);

        bool operator<(const Date& d);
        bool operator<=(const Date& d);
        bool operator>(const Date& d);
        bool operator>=(const Date& d);
        bool operator==(const Date& d);
        bool operator!=(const Date& d);

       
        int operator-(const Date& d);

        void Print();
private:
        int _year;
        int _month;
        int _day;
};

//Date.cpp
#include"Date.h";
Date& Date::operator+=(int day)
{
        _day += day;
        while (_day > GetMonthDay(_year, _month))
        {
                _day -= GetMonthDay(_year, _month);
                ++_month;
                if (_month == 13)
                {
                        _year++;
                        _month = 1;
                }
        }

        return *this;
}

// d1 + 100
Date Date::operator+(int day)
{
        Date tmp(*this);
        tmp += day;

        return tmp;
}

void Date::Print()
{
        cout << _year << "-" << _month << "-" << _day << endl;
}

// ++d1
Date& Date::operator++()
{
        *this += 1;
        return *this;
}

// d1++
Date Date::operator++(int)
{
        Date tmp(*this);
        *this += 1;

        return tmp;
}

// d1 < d2
bool Date::operator<(const Date& d)
{
        if (_year < d._year)
        {
                return true;
        }
        else if (_year == d._year
                && _month < d._month)
        {
                return true;
        }
        else if (_year == d._year
                && _month == d._month
                && _day < d._day)
        {
                return true;
        }
        else
        {
                return false;
        }
}

bool Date::operator==(const Date& d)
{
        return _year == d._year
                && _month == d._month
                && _day == d._day;
}

// d1 <= d2
bool Date::operator<=(const Date& d)
{
        return *this < d || *this == d;
}

bool Date::operator>(const Date& d)
{
        return !(*this <= d);
}

bool Date::operator>=(const Date& d)
{
        return !(*this < d);
}
//test.cpp
#include"Date.h"

void TestDate1()
{
        Date d1(2025, 3, 9);
        d1.Print();

        Date d2 = d1 + 100;
        d2.Print();
        d1.Print();

        Date d3 = d1 += 100;
        d1.Print();
        d3.Print();
}

void TestDate2()
{
        Date d1(2025, 3, 9);
        Date ret1 = ++d1;
        //Date ret1 = d1.operator++();
        d1.Print();
        ret1.Print();

        Date d2(2025, 3, 9);
        Date ret2 = d2++;
        //Date ret2 = d2.operator++(10000);
        d2.Print();
        ret2.Print();
}

int main()
{
        TestDate2();

        return 0;
}
https://i-blog.csdnimg.cn/direct/95613c32ecd9458293a59cddc9253e6c.jpeg

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: C++【类和对象】(超具体!!!)