C++笔记--面向对象(OOP)编程基础--操作符重载及友元(4)

目录

运算符重载基础 

全局函数和类成员函数的区别

运算符重载

友元函数


运算符重载基础 

全局函数和类成员函数的区别

1. 如果把全局函数转成成员函数,少了一个操作数,通过this指针,被隐藏

2. 把成员函数转成全局函数,需要多一个参数

#include <iostream>

#include "stdio.h"
#include "stdlib.h"

using namespace std;

class Test {
    private:
        int a;
        int b;
    public:
        Test(int a, int b) {
            this->a = a;
            this->b = b;
        }
        int getA() {
            return this->a;
        }
        int getB() {
            return this->b;
        }
        //如果把全局函数转成成员函数,少了一个操作数,通过this指针,被隐藏
        Test add(Test &b) {
            Test c(this->a + b.getA(), this->b + b.getB());
            return c;
        }
        void print() {
            cout << "a=" << a << ",b=" << b << endl;
        }
};
//把成员函数转成全局函数,需要多一个参数
Test add(Test &a, Test &b) {
    Test c(a.getA() + b.getA(), a.getB() + b.getB());
    return c;
}

void Print(Test *pTest) {
    cout << "全局函数 a=" << pTest->getA() << ",b=" << pTest->getB() << endl;
}

int main() {

    Test t1(1, 2); //Test(&t1, 1, 2);
    Test t2(3, 4);
    Test t3 = add(t1, t2);

    t3.print();
    Print(&t3);

    return 0;
}

输出

a=4,b=6
全局函数 a=4,b=6
 

示例

#include <iostream>
#include "stdlib.h"
#include "stdio.h"

using namespace std;

class Good {
    private:
        int weight;
        static int total_weight;
    public:
        Good *next;
        Good(int weight) {
            this->weight = weight;
            Good::total_weight = Good::total_weight + weight;
        }
        ~Good() {
            Good::total_weight = Good::total_weight - weight;
        }
        static int TotalWeight() {
            return Good::total_weight;
        }
};
int Good::total_weight = 0;

void purchase(Good * &front, Good * &rear, int weight) {

    Good *good = new Good(weight);

    if(front == NULL) {
        front = rear = good;
    } else {
        rear->next = good;
    }
}

void sale(Good *&front, Good *&rear) {
    if(front == NULL) {
        return;
    }

    Good *p = front;
    front = front->next;

    delete p;
    p = NULL;
}

int main() {

    Good * front = NULL /*头*/, * rear = NULL ;
    int  w ;  int  choice ;
    do
    {
        cout << "Please choice:\n" ;
        cout << "Key in 1 is purchase,\nKey in 2 is sale,\nKey in 0 is over.\n" ;
        cin >> choice ;
        switch ( choice )		// 操作选择
        {
            case 1 :                                               // 键入1,购进1箱货物
            {  cout << "Input weight: " ;
                cin >> w ;
                purchase( front, rear, w ) ;          // 从表尾插入1个结点
                break ;
            }
            case 2 : 		            // 键入2,售出1箱货物
            { sale( front, rear ) ;    break ; }       // 从表头删除1个结点
            case 0 :  break ; 		            // 键入0,结束
        }
        cout << "Now total weight is:" << Good::TotalWeight() << endl ;
    } while ( choice ) ;

    return 0;
}

运算符重载

语法形式:

类型 operator op(参数表) {

    //相对于该类定义的操作

}

示例

#include "iostream"
#include "stdio.h"
#include "stdlib.h"

using namespace std;

class Test {
    private:
        int a;
    public:
        Test(int a) {
            this->a = a;
        }
        int getA() {
            return a;
        }
};

Test operator+(Test &t1, Test &t2) {
    Test t(t1.getA() + t2.getA());
    return t;
}

int main() {

    Test t1(1);
    Test t2(2);
    //1. 调用方式1
    //Test t3 = operator+(t1, t2);
    //2. 调用方式2
    Test t3 = t1 + t2;
    cout << "t3=" << t3.getA() << endl;

    return 0;
}

1. 调用方式1
 Test t3 = operator+(t1, t2);
2. 调用方式2
 Test t3 = t1 + t2;

友元函数

声明了一个友元函数,它是普通函数定义在类体外。友元函数中通过指定的对象访问了类中的私有数据成员,并进行了运算

类中的成员函数作为另一个类的友元函数,其语法格式如下:

friend 类名::函数返回值类型 函数名(形式参数列表);

示例 

class A {

    private:
        int a1;

    public:
        //声明友元函数,
        friend void setA1(A *p, int a1);
};

void setA1(A *p, int a1) {
    p->a1 = a1;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值