• 【C++基础】类class


    【意义】

    Class是一种类型type,定义类的格式与struct相似,但能在定义体内添加操作;

    【定义】

    class Date
    {
        int year;    //数据成员
        int month;
        int day;
        
    public:        //成员函数
        void set(int y,int m,int d);//赋值操作
        void isLeapYear();//判断是否为闰年
        void print();//输出日期
    };
        
    Date d;//由类创建的实体,称为对象

    【成员函数定义】定义方式有两种:

    1、成员函数写入类定义中:只要是在类定义中包含的成员函数,就有默认声明内联的性质,这样能获取最佳性能

    class Date
    {
        int year;
        int month;
        int day;
        
    public:
        bool isLeapYear()//判断是否为闰年
        {
            return (year%4==0&&year%100!=0)||(year%400==0);
        }
    };

    2、在类定义外部定义成员函数:

    class Date
    {
        int year;    //数据成员
        int month;
        int day;
        
    public:        //成员函数
        void set(int y,int m,int d);//赋值操作
    };
    
    //成员函数在类外部定义时,在函数名称前应加上相应的类型名前缀和名空间引导符“::”
    void Date::set(int y,int m,int d)
     {
        year=y;
        month=m;
        day=d;
    }

    注意事项:

    函数定义体的花括号对的后面是没有分号的,而类定义体的花括号对的后面一定得有分号。

    【使用对象指针】

    调用成员函数的方式:

    1、“.”操作符:

    ObjectName.memberFunctionName(parameters);

    2、以对象指针简介访问的形式:

    ObjectPointer->memberFunctionName(parameters);

    例子:

    Date* dp=new Date;

    dp->set(2000,12,6);

    3、将对象指针的间访形式用括号括起来,再加点操作符“.”加成员函数:

    (*ObjectPointer).memberFunctionName(parameters);

    注意,不要写成*ObjectPointer.memberFunctionName(parameters);

    因为操作符的优先级,导致编译出错

    【常成员函数:const】

    如果只对对象进行【读操作】,则该成员函数可以设计为常成员函数。

    例子:

    class Date
    {
        int year,month,day;
    public:
        void set(int y,int m,int d);
        bool isLeapYear() const;
        void print() const;
    };

    【重载成员函数】

    成员函数与普通函数的重载识别一样

    class Date
    {
        int year,month,day;
    public:
        void set(int y,int m,int d);
        void set(string& s);
        bool isLeapYear();
        void print();
    };
    
    void Date::set(int y,int m,int d)
    {
        year=y;
        month=m;
        day=y;
    }
    
    void Date::set(string& s)
    {
        year=atoi(s.substr(0,4).c_str());
        month=atoi(s.substr(5,2).c_str());
        day=atoi(s.substr(8,2).c_str());
    }

    【操作符重载】

    1、  性质:

    (1)       拒绝新创:不能创建新的操作符

    (2)       个别重载限制:【::】、【.】、【.*】不能重载,因为它们都特殊地要求第二参数必须为名称;还有                 【:?】、【sizeof】、【typeof】也不允许重载

    (3)       优先级和结合性不变:重载操作符后,其优先级和结合性是不会改变的。

    (4)       操作数个数不变:原来操作符是几目的,重载后的操作符也是几目的

    (5)       专门处理对象:操作符重载只能针对自定义类型,基本上专门用于类对象的操作

    (6)       操作符重载后的意义应该反映操作符的本质

    例子:

    #include<iostream>
    using namespace std;
    
    class Point
    {
        int x,y;
    public:
        void set(int a,int b){x=a,y=b;}
        void print() const{cout<<"("<<x<<", "<<y<<")
    ";}
        friend Point operator+(const Point& a,const Point& b);
        friend Point add(const Point&a,const Point& b);
    };
    
    Point operator+(const Point& a,const Point& b)
    {
        Point s;
        s.set(a.x+b.x,a.y+b.y);
        return s;
    }
    
    Point add(const Point& a,const Point& b)
    {
        Point s;
        s.set(a.x+b.x,a.y+b.y);
        return s;
    }
    
    int main()
    {
        Point a,b;
        a.set(3,2);
        b.set(1,5);
        (a+b).print();//人们习惯的中缀表示法
        operator+(a,b).print();//重载后的调用方法
        add(a,b).print();//add函数与+的作用完全一样,因此操作符不是必须的
    }
    
    //输出结果都是(4,7)
    //operator+和add函数体中都要用Point对象进行私有数据访问,作为普通函数,这是不允//许的,但将Point类中以friend关键字引导函数声明后,即可访问私有数据

    【值返回和引用返回】

    在设计函数时,参数若为类类型,则一般用引用型,若为内部数据类型,则不用引用型

    1、  Point的+操作是两个Point对象相加,相加的结果是另外一个Point值,与两个参数无关,求和过程也不影响两个参数的值,因此,两个参数的类型为const的引用

    2、  “cout<<a;”的结果是cout流,加上操作符的左结合性,所以其<<可以进行”cout<<..<<..;”式的重叠操作,重载流的<<操作时,为了保持cout这一性质,其返回值也必须是流;<<返回的cout是特定某个输出设备的流对象。若<<返回一个非引用型的流,返回时将对cout进行复制,产生一个临时的流,它会随着重叠操作而引起更多不必要的创建流操作,导致内存浪费和性能下降。

    总结:Point的+操作返回Point类的值,<<操作返回流的引用

    【增量操作符】

    1、  一个整型变量的前增量操作的结果与变量值是一致的,而且前增量操作的结果是左值,操作可以连贯:

    如:int a=1,c=1;

    (++a)++;//结果a=3

    ++(++c);//结果c=3

    2、  后增量操作的结果是增量之前的变量值,它是临时变量,当表达式计算完成后,该临时变量随即消失:

    如:int b=1,d=1;

    (b++)++;//结果b=2,(b++)的结果是临时变量,在其加上1随后又抛弃

    ++(d++);//结果d=2,解释如上

    3、  重载定义:

    (1)       前增量操作数与返回值是同一个变量,在反映对象的前增量操作时,要求参数为对象的引用,返回的仍然是对该对象参数的引用:

    如:   X& operator++(X& a);//前增量操作数

    ++a;//等价与operator++(a);匹配上述操作符声明

    (2)       后增量操作符重载,也要求参数为对象的引用,因为在调用的上下文中,实参将发生变化,而返回则为临时对象,所以为非引用的对象值:

    如:   X& operator++(X& a,int b);//前增量操作数

    a++;//等价与operator++(a,1);匹配上述操作符声明

    4、  例子:

    #include<iostream>
    #include<iomanip>
    using namespace std;
    
    class Time
    {
        int hour,minute,second;
    public:
        void set(int h,int m,int s){hour=h,minute=m,second=s;}
        friend Time& operator++(Time& a);//前增量操作符的声明
        friend Time operator++(Time& a,int);//后增量操作符的声明
        friend ostream& operator<<(ostream& o,const Time& t);//输出函数声明
    };
    
    Time& operator++(Time& a)//前增操作符的定义
    {
        if(!(a.second=(a.second+1)%60)&&!(a.minute=(a.minute+1)%60))
            a.hour=(a.hour+1)%24;
        return a;//前增量操作数与返回值是同一个变量,返回值仍然是对该对象参数的引用
    }
    
    Time operator++(Time& a,int)//后增操作符的定义
    {
        Time t(a);//声明一个临时对象,将a的数据成员传入
        if(!(a.second=(a.second+1)%60)&&!(a.minute=(a.minute+1)%60))
            a.hour=(a.hour+1)%24;
        return t;//实参要发生变化,但是返回的位临时对象
    }
    
    ostream& operator<<(ostream& o,const Time& t)
    {
        o<<setfill('0')<<setw(2)<<t.hour<<":"<<setw(2)<<t.minute<<":";
        return o<<setw(2)<<t.second<<"
    "<<setfill(' ');
    }
    
    int main()
    {
        Time t;
        t.set(11,59,58);
        cout<<t++;
        cout<<++t;
    }
    
    //输出11:59:58
    //    12:00:00

    【成员操作符】

    操作符作为类中的成员,无须冠之以friend就可以直接访问类中的任何成员了,本质上就是讲成员方法在class的内部既声明且定义。

    #include<iostream>
    using namespace std;
    
    class Point
    {
        int x,y;
    public:
        void set(int a,int b){x=a,x=b;}
        Point operator+(const Point& d)
        {
            Point s;
            s.set(x+d.x,y+d.y);
            return s;
        }
        friend ostream& operator<<(ostream& o,const Point& d);
    };
    
    inline ostream& operator<<(ostream& o,const Point& d)
    {
        return o<<"("<<d.x<<", "<<d.y<<")
    ";
    }
    
    int main()
    {
        Point s,t;
        s.set(2,5);
        t.set(3,1);
        cout<<s+t;
    }
    
    
    普通操作符(需要在class内部声明前加friend)
    Point operator+(const Point& a,const Point& b)
    {
        Point s;
        s.set(a.x+b.x,a.y+b.y);
        return s;
    }
    
    成员操作符
    Point operator+(const Point& d)
    {
        Point s;
        s.set(x+d.x,y+d.y);
        return s;
    }

    两者相比,成员操作符省略了第一个参数,因为成员函数总是与对象捆绑使用。被捆绑的对象就是被操作的第一参数

  • 相关阅读:
    镜像的上传和下载
    ps 命令
    过滤不合格数据
    云计算5-3-2法则
    Python Django初入门
    python web框架
    BootStrap、EasyUI、JQueryUI
    JS正则
    ngonx FastCGI 相关参数调优
    Windows10远程连接错误-出现身份验证错误
  • 原文地址:https://www.cnblogs.com/jixiaowu/p/3922552.html
Copyright © 2020-2023  润新知