C++学习笔记(三)

 运算符重载

热身:

电子时钟的运算符重载:

在主函数main()中指定开始时间和结束时间,并调用相应成员函数,显示从开始时间到结束时间之间所有时间对象的值,其格式见示例输出。

输入

输入6个整数,之间用一个空格间隔;分别表示开始时间的时、分、秒和结束时间的时、分、秒的值

输出

从开始时间到结束时间之间所有时间对象的值;每个值占一行,格式为hh:mm:ss

示例输入

01 01 01 01 01 10

示例输出

01:01:01
01:01:02
01:01:03
01:01:04
01:01:05
01:01:06
01:01:07
01:01:08
01:01:09
01:01:10

提示

输入
11 10 12 10 12 56
输出
The begin time is not earlier than the end time!
#include <iostream>

using namespace std;

class Time   //设计一个Time类
{
    private:
        int hour;
        int minute;
        int second;
    public:
        Time() {}
        Time(int hour,int minute,int second);
        Time operator ++ ();
        void disPlay();
        int compare(Time &);
};


Time :: Time(int hour,int minute,int second)   //构造函数
{
    this->hour = hour;
    this->minute = minute;
    this->second = second;
}
Time Time::operator++ ( )    // 重载了++ ,使其++类时,类内部的属性按照时分秒逐步增加的规则增加
{
        if(++second >= 60)
        {
            second -= 60;

            if( ++minute >=  60)
            {
                minute -= 60;
                ++hour;
            }
        }
}
void Time::disPlay()     //输出时分秒的方法
{
    if(hour <10) cout<<"0"<<hour<<":";
    else  cout<<hour<<":";
    if(minute < 10) cout<<"0"<<minute<<":";
    else cout<<minute<<":";
    if(second <10) cout<<"0"<<second;
    else cout<<second;
    cout<<endl;
}
//判断第一个对象的时分秒,与第二个时分秒的关系,第一个时分秒在第二个时分秒之前则输出true,否则输出false
int Time::compare(Time & t)    
{
    if(hour > t.hour)
    {
        return false;
    }else if(hour== t.hour&& minute > t.minute)
    {
        return false;
    }else if(hour == t.hour && minute == t.minute && second > t.second)
    {
        return false;
    }else
    {
        return true;
    }
}


int main()
{
    int h1,h2,m1,m2,s1,s2;
    cin>>h1>>m1>>s1;
    cin>>h2>>m2>>s2;
    Time time1(h1,m1,s1);
    Time time2(h2,m2,s2);
  
    if(! time1.compare(time2) )    // 判断两个对象的关系,是否符合第一各对象的时间在第二各对象的时间前面
    {
        cout<<"The begin time is not earlier than the end time!"<<endl;
    }else
    {


        while(time1.compare(time2))  //判断两个对象的关系,作为结束条件
        {
            time1.disPlay();
            ++time1;
        }
    }
    return 0;
}
运算符重载
运算符重载实质是函数重载
函数类型  operator 运算符名称 (形参列表)
{ 对运算符的重载处理 }
运算符重载后其原有功能仍存在,并未消失。
规则:
(1).c++不允许用户自己定义新的运算符,只能对已有的运算符重载。
(2).不能重载的运算符"."(成员运算符) ".*"(成员指针访问运算符) " :: "(域运算符) "sizeof"(长度运算符) " ?: "(条件运算符)
(3).不能改变运算符运算对象
(4).不能改变运算符优先级
(5).不能改变运算符的结合性
(6).重载运算符的函数不能有默认参数。
(7).重载的运算符必须和用户定义的自定义类型一起使用,其参数至少应是一个类的对象。
(8).运算符= 和 &不必用户重载。=:
重载流插入运算符和流提取运算符
istream  & operator >> (istream &,自定义 &);
ostream & operator << (ostream &,自定义 &);
只能将他们定义为友元函数或普通函数,而不能将它们定义为成员函数。
例:
#include <iostream>

using namespace std;

class Complex
{
    private:
        double real;
        double imag;
    public:
        Complex() {}
        Complex(double real,double imag);
        Complex operator + (Complex &c);    //重载运算符+,可让Compex的对象相加
        friend ostream & operator << (ostream &,Complex &);  //重载《《流输出运算符,输出Complex对象
        friend istream & operator >> (istream &,Complex &);   //重载》》流输入运算符,输入Complex对象
        
};

Complex::Complex(double real,double imag)
{
    this->real = real;
    this->imag = imag;
}

Complex Complex::operator + (Complex& c)
{
    Complex c1;
    c1.real = real + c.real;
    c1.imag = imag + c.imag;
    return c1;
}


ostream & operator <<(ostream &output,Complex & c)
{
    output<<"real:"<<c.real;
    output<<"imag"<<c.imag<<endl;

    return output;
}

istream & operator >> (istream &input,Complex &c)
{
    cout<<"input real part and imaginary part of complex number"<<endl;
    input>>c.real>>c.imag;

    return input;
}
int main()
{
    Complex c1,c2,c3;
    cin>>c1>>c2;
    cout<<c1<<c2;
    c3 = c1 + c2;
    cout<<c3<<endl;
    return 0;
}
不同类型数据见的转换
转换构造函数进行类型转换
#include <iostream>

using namespace std;

class Complex
{
    private:
        double real;
        double imag;
    public:
        Complex(double num)    //转换构造汉函数
        {
            real = num;
            imag = 0;
        }

        Complex(int real,int imag)  // 实现初始化的构造函数
        {
            this->real = real;
            this->imag = imag;
        }
        Complex() { }    // 默认构造函数
        void getComplex()
        {
            cout<<"real:="<<real<<"     imag="<<imag<<endl;
        }

        friend Complex operator + (Complex c1,Complex c2)  //重载运算符 +
        {
            Complex c;
            c.real = c1.real + c2.real;
            c.imag = c1.imag + c2.imag;
            return c;
        }
};
int main()
{
    Complex c1(1,2),c2,c3;
    c2 = c1 + Complex(10);
    /*
        Complex(10),是建立了一个Complex的临时对象,调用
        转换构造函数建立对象
    */
    c3 = c1 + 3.5;
/*
c1 + 3.5 编译系统把它解释为operator + (c1,3,5),由于2.5不是Complex类对象,系统先调用转换构造函数Complex(3.5)
类临时对象,其值为(3.5,0i),相当于operator(c1,Complex(3.5)).
*/
    cout<<"c1="<<endl;
    c1.getComplex();

    cout<<"c2="<<endl;
    c2.getComplex();

    cout<<"c3="<<endl;
    c3.getComplex();
    return 0;
}
转换构造函数也是一种构造函数,只有一个参数。
类型转换函数
#include <iostream>

using namespace std;

class Complex
{
    private:
        double real;
        double imag;
    public:
        Complex(int real,int imag)  // 实现初始化的构造函数
        {
            this->real = real;
            this->imag = imag;
        }
        Complex() { }    // 默认构造函数
        operator double()
        {
            return real;
        }
};
int main()
{
    Complex c1(1,2);
    double d;
    d =  c1 + 3.4 ;   //将一个double类型的数据与Complex类对象相加
    cout<<d<<endl;
    return 0;
}
类型装换函数是将一个类的对象转换成另一各数据类型。
operator  类型名()
{类型转换语句}
在函数名前不能指定函数类型,函数没有参数。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值