zoukankan      html  css  js  c++  java
  • 嘀嘀咕(3)

    1.this

    class A
    {
        public:
            int get( ) const        //成员函数尾部出现 const,修饰的是this指针
            {
                this->a = 100;    //err
            } 
    
            A &add(int b)            //返回对象本身
            {
                this->a += b;
                return *this;
            }
        private:
            int a;
    }
    

    2.友元

    2.1:友元函数

    可以直接访问类的私有成员的非成员函数。
    它是定义在类外的普通函数,它不属于任何类,但需要在类的定义中加以声明,声明时只需在友元的名称前加上  关键字  friend。
    
    class  Point
    {
        public:
            Point(double  xx,  double  yy)
            {
                x  =  xx;
                y  =  yy;
            }
            void  Getxy();
            friend  double  Distance(Point  &a,  Point  &b);
        private:
            double  x,  y;
    };
    
    double  Distance(Point  &a,  Point  &b)
    {
        double  dx  =  a.x  -  b.x;
        double  dy  =  a.y  -  b.y;
        return  sqrt(dx*dx  +  dy*dy);
    }
    

    2.2:友元类

    友元类的所有成员函数都是另一个类的友元函数,都可以访问另一个类中的隐藏信息(包括私有成员和保护成员)
    
    class  A
    {
        
        public:
            friend  class  B;        //类  B 的所有成员函数都是类  A 的友元函数,能存取类 A的私有成员和保护成员
           
    };
    

    3:操作符重载

    Complex operator+(Complex &a, Complex &b)
    {
        Complex tmp(a.x+b.x, a.y+b.y);
        return tmp;
    }
    

    3.1:重载+=

    class Complex
    {
    public:
        Complex(int a, int b)
        {
            this->a = a;
            this->b = b;
        }
    
        void printComplex()
        {
            cout << "( " << this->a << ", " << this->b << "i )" << endl;
        }
    
        friend Complex & operator+=(Complex &c1, Complex &c2);
      
        //方式2
        Complex &operator+=(Complex &another)
        {
            this->a += another.a;
            this->b += another.b;
    
            return *this;
        }
    private:
        int a;//实数
        int b;//虚数
    };
    
    //方式1:全局
    #if 0
    Complex & operator+=(Complex &c1, Complex &c2)
    {
        c1.a += c2.a;
        c1.b += c2.b;
    
        return c1;
    }
    #endif
    
    //(a += b) += c;
    a.operator+=(b).operator+=(c);
    

    3.2:重载++

    class Complex
    {
    public:
        Complex(int a, int b)
        {
            this->a = a;
            this->b = b;
        }
    
        void printComplex()
        {
            cout << "( " << this->a << ", " << this->b << "i )" << endl;
        }
    
        //friend Complex & operator++(Complex &c);
        //friend const Complex operator++(Complex &c1, int);
    
        Complex &operator++()
        {
            this->a++;
            this->b++;
    
            return *this;
        }
    
        const Complex operator++(int)//亚元
        {
            Complex temp(this->a, this->b);
            this->a++;
            this->b++;
            return temp;
        }
    
    
    private:
        int a;//实数
        int b;//虚数
    };
    
    #if 0
    //重载的是前++运算符
    Complex & operator++(Complex &c)
    {
        c.a++;
        c.b++;
        return c;
    }
    #endif
    
    //重载的是后++运算符
    #if 0
    const Complex operator++(Complex &c1, int)
    {
        Complex temp(c1.a, c1.b);
    
        c1.a++;
        c1.b++;
    
        return temp;
    }
    #endif
    

    3.3:重载<<、>>

    class Complex
    {
    public:
        Complex(int a, int b)
        {
            this->a = a;
            this->b = b;
        }
    
        void printComplex()
        {
            cout << "( " << this->a << ", " << this->b << "i )" << endl;
        }
    
        friend ostream& operator<<(ostream & os, Complex &c);
        friend istream & operator>>(istream &is, Complex &c);
    
        //<<操作符只能写在全局,不能够写在成员方法中。否则调用的顺序会变,c1<<cout;
    #if 0
        ostream& operator<<(ostream &os) //c1.operator<<(cout)
        {
            os << "( " << this->a << ", " << this->b << "i )";
            return os;
        }
    #endif
    
    private:
        int a;//实数
        int b;//虚数
    };
    
    #if 1
    ostream& operator<<(ostream & os, Complex &c)        //cout<<c; ===>第一个操作符类型ostream,第二个操作符类型Complex
    {
        os << "( " << c.a << ", " << c.b << "i )";
    
        return os;
    }
    
    istream & operator>>(istream &is, Complex &c)
    {
        cout << "a:";
        is >> c.a;
        cout << "b:";
        is >> c.b;
    
        return is;
    }
    #endif
    

    3.4:练习

    //练习操作符重载
    自定义数组,实现<<操作符 实现取值操作符[]
    
       array[i] = 10;
       cout <<array<<endl;
       cin>>array;
       cout <<array[i] <<endl;
    
       ==
       !=
       array1 == array2
       array1 != array2
    
  • 相关阅读:
    Xamarin.Forms项目无法添加服务引用
    Xamarin Android长度单位区别
    21IC菜农研究的HotWC3超级CRC运算器
    Delphi天气预报查询
    超外差接收机工作原理?
    ARM汇编指令的特点和速查表
    序列号的设计,不重复的实现一机一码
    iOS第一个简单APP
    GetEnvironmentVariable 获取常用系统变量(转)
    Delphi版的Base64转换函数(修改版)
  • 原文地址:https://www.cnblogs.com/EngineerZhang/p/9851927.html
Copyright © 2011-2022 走看看