C++学习笔记(三)

2014-11-24 03:04:36 · 作者: · 浏览: 1

运算符重载

热身:

电子时钟的运算符重载:

在主函数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 
  
   

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

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:"<
       
        > (istream &input,Complex &c) { cout<<"input real part and imaginary part of complex number"<
        
         >c.real>>c.imag; return input; } int main() { Complex c1,c2,c3; cin>>c1>>c2; cout<