C++--day08

目录: 

1. C的提高 1-131P 时间七天 
2. C++的基础 132-286P 时间八天 
3. C++的提高 287-378P 时间五天 
4. C/C++的数据结构 379-482P 时间五天 
5. C/C++的设计模式基础 483-540P 时间三天

视频资料:https://www.bilibili.com/video/av27904891?from=search&seid=10891514449061956870


P222  ()运算符重载

() 运算符用于函数调用

重载格式

    类型  类 :: operator()  ( 表达式表  ) ;

例1

  设 x 是类 X 的一个对象,则表达式

  x ( arg1, arg2, … )

  可被解释为

  x . operator () (arg1, arg2, … )

例2:用重载()运算符实现数学函数的抽象

#include <iostream>
class  F
  { public :  

        double  operator ( )  ( double x ,  double  y ) ;

  } ;

double  F :: operator ( )  ( double  x ,  double  y )

   { return   x * x + y * y ; }

void main ( )
{
  F  f  ;
  f.getA();
   cout << f ( 5.2 , 2.5 ) << endl ;  // f . operator() (5.2, 2.5)
}

比较普通成员函数

3  用重载()运算符实现 pk 成员函数

#include <iostream.h>

class  F

  { public :  

        double  memFun ( double x ,  double  y ) ;

  } ;

double  F :: memFun ( double  x ,  double  y )

   { return   x * x + y * y ; }

void main ( )

{
   F  f  ;
    cout << f.memFun ( 5.2 , 2.5 ) << endl ;

}
#include <iostream>
using namespace std;

class F
{
public:
    int operator() (int a, int b)
    {
        return a*a + b*b;
    }
};

class F2
{
public:
    int MemFunc(int a, int b)
    {
        return a*a + b*b;
    }
};
int main()
{
    
    F f;
    f(2, 4);
    //operator() (int a, int b)
    
    F2 f2;
    f2.MemFunc(2, 4);

    cout<<"hello..."<<endl;
    system("pause");
    return 0;
}

P223  逻辑与和逻辑或运算符重载说明

为什么不要重载&&||操作符

理论知识:

  1)&&和||是C++中非常特殊的操作符

  2)&&和||内置实现了短路规则

  3)操作符重载是靠函数重载来完成的

  4)操作数作为函数参数传递

  5)C++的函数参数都会被求值,无法实现短路规则

#include <cstdlib>
#include <iostream>

using namespace std;

class Test
{
    int i;
public:
    Test(int i)
    {
        this->i = i;
    }

    Test operator+ (const Test& obj)
    {
        Test ret(0);

        cout<<"执行+号重载函数"<<endl;
        ret.i = i + obj.i;
        return ret;
    }

    bool operator&& (const Test& obj)
    {
        cout<<"执行&&重载函数"<<endl;

        return i && obj.i;
    }
};

// && 从左向右
void main()
{
    int a1 = 0;
    int a2 = 1;

    cout<<"注意:&&操作符的结合顺序是从左向右"<<endl;

    if( a1 && (a1 + a2) )
    {
        cout<<"有一个是假,则不在执行下一个表达式的计算"<<endl;
    }

    Test t1 = 0;
    Test t2 = 1;

    //if( t1 && (t1 + t2)  )
    //t1  && t1.operator+(t2)
    // t1.operator&&(  t1.operator+(t2) )   

    //1. && || 重载他们 不会产生短路效果
    if(  (t1 + t2) && t1)
    {
        //t1.operator+(t2) && t1;
        //(t1.operator+(t2)).operator&&(t1);

        cout<<"两个函数都被执行了,而且是先执行了+"<<endl;
    }

    //2. && 运算符的结合性
    // 两个逻辑与运算符  在一块的时候, 才去谈运算符的结合性
    // 从左到右    (t1 + t2) && t1 ; 运算结果 && t2)
    //if(  (t1 + t2) && t1 && t2)
    {
        //t1.operator+(t2) && t1;
        //(t1.operator+(t2)).operator&&(t1);

        cout<<"两个函数都被执行了,而且是先执行了+"<<endl;
    }

    system("pause");
    return ;
}

P225-231  MyString案例

MyString.h

#include<iostream>
using namespace std;

//c中没有字符串 字符串类(c风格的字符串)
class MyString
{
    friend ostream& operator<<(ostream &out,MyString &s);
    friend istream& operator>>(istream &in,MyString &s);
public:
    MyString(int len=0);
    MyString(const char *p);
    MyString(const MyString& s);
    ~MyString();

public://重载等号操作符
    MyString& operator=(const char *p);
    MyString& operator=(const MyString &s);
    char& operator[](int index);
public://重载== !=操作符
    bool operator==(const char *p) const;
    bool operator==(const MyString &s) const;
    bool operator!=(const char *p) const;
    bool operator!=(const MyString &s) const;
public://重载< >操作符
    int operator<(const char *p);
    int operator>(const char *p);
    int operator<(const MyString &s);
    int operator>(const MyString &s);
//把类的指针露出来
public:
    char *c_str()
    {
        return m_p;
    }
    const char *c_str2()
    {
        return m_p;
    }
    int length()
    {
        return m_len;
    }

private:
    int m_len;
    char *m_p;
};

 MyString.cpp

#define  _CRT_SECURE_NO_WARNINGS
#include "MyString.h"

istream& operator>>(istream &in,MyString &s)
{
    cin>>s.m_p;
    return in;
}
ostream& operator<<(ostream &out,MyString &s)
{
    out<<s.m_p;
    return out;
}


MyString::MyString(int len)
{
    if (len==0)
    {
        m_len=0;
        m_p=new char[m_len+1];
        strcpy(m_p,"");
    }
    else
    {
        m_len=len;
        m_p=new char[m_len+1];
        memset(m_p,0,m_len);
    }
    
}
MyString::MyString(const char *p)
{
    if (p==NULL)
    {
        m_len=0;
        m_p=new char[m_len+1];
        strcpy(m_p,"");
    }
    else
    {
        m_len=strlen(p);
        m_p=new char[m_len+1];
        strcpy(m_p,p);
    }
}
//拷贝构造函数
MyString::MyString(const MyString& s)
{
    m_len=s.m_len;
    m_p=new char[m_len+1];

    strcpy(m_p,s.m_p);
}
MyString::~MyString()
{
    if (m_p==NULL)
    {
        delete [] m_p;
        m_p=NULL;
        m_len=0;
    }
}

//s4="s22222";
MyString&MyString:: operator=(const char *p)
{
    //1. 旧内存释放掉
    if (m_p!=NULL)
    {
        delete [] m_p;
        m_len=0;
    }
    //2. 根据p分配内存
    if (p==NULL)
    {
        m_len=0;
        m_p=new char[m_len+1];
        strcpy(m_p,"");
    }
    else
    {
        m_len=strlen(p);
        m_p=new char[m_len+1];
        strcpy(m_p,p);
    }
    return *this ;
    
}
//s4=s2;
MyString& MyString::operator=(const MyString &s)
{
    //1. 旧内存释放掉
    if (m_p!=NULL)
    {
        delete [] m_p;
        m_len=0;
    }
    //2. 根据s分配内存
    m_len=s.m_len;
    m_p=new char[m_len+1];
    strcpy(m_p,s.m_p);
    return *this ;
}

char& MyString::operator[](int index)
{
    return m_p[index];
}

//s2=="s22"
bool  MyString::operator==(const char *p) const
{
    if (p==NULL)
    {
        if (m_len==0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        if (m_len==strlen(p))
        {
            return strcmp(m_p,p);
        }
        else
        {
            return false;
        }
    }
    return true;
}
bool  MyString::operator!=(const char *p) const
{
    return !(*this==p);
}
//s3==s2
bool  MyString::operator==(const MyString &s) const
{
    if (m_len!=s.m_len)
    {
        return false;
    }
    return !strcmp(m_p,s.m_p);

}

bool  MyString::operator!=(const MyString &s) const
{
    return !(*this==s);
}

//s4<"bbbb"
int  MyString::operator<(const char *p)
{
    return strcmp(this->m_p,p);
}
int  MyString::operator>(const char *p)
{
    return strcmp(p,this->m_p);
}
int  MyString::operator<(const MyString &s)
{
    return strcmp(this->m_p,s.m_p);
}
int  MyString::operator>(const MyString &s)
{
    return strcmp(s.m_p,this->m_p);
}

 MyString_Test.cpp

#include<iostream>
#include "MyString.h"
#define  _CRT_SECURE_NO_WARNINGS
using namespace std;

void main()
{
    MyString s1;
    MyString s2("s2");
    MyString s2_2=NULL;
    MyString s3=s2;
    MyString s4="s444444";
    //测试运算符重载和重载[]
    //等号
    //MyString& operator=(const char *p);
    //MyString& operator=(const MyString &s);
    s4=s2;
    s4="s22222";
    //char& operator[](int index);
    s4[1]='4';
    printf("%c
",s4[1]);

    //ostream& operator<<(ostream &out,MyString &s);
    cout<<s4<<endl;

    //bool operator==(const char *p)
    //bool operator==(const MyString &s)
    //bool operator!=(const char *p)
    //bool operator!=(const MyString &s)
    if (s2=="s22")
    {
        printf("相等
");
    }
    else
    {
        printf("不相等
");
    }

    if (s3==s2)
    {
        printf("相等
");
    }
    else
    {
        printf("不相等
");
    }

    //int operator<(const char *p);
    //int operator>(const char *p);
    //int operator<(const MyString &s);
    //int operator>(const MyString &s);
    if (s4<"bbbb")
    {
        printf("s4小于bbbb
");
    }
    else
    {
        printf("s4大于bbbb
");
    }

    if (s4<s2)
    {
        printf("s4小于s2
");
    }
    else
    {
        printf("s4大于s2
");
    }


    MyString s5="aaaaffff";
    strcpy(s5.c_str(),"a111");//MFC
    cout<<s5<<endl;

    MyString s6(128);
    cout<<"
请输入字符串(回车结束)";
    //istream& operator>>(istream &in,MyString &s)
    cin>>s1;
    cout<<s1<<endl;

    system("pause");
}

原文地址:https://www.cnblogs.com/yangyuqing/p/10445540.html