c++重载++运算符_C ++运算符重载| 查找输出程序| 套装3

c++重载++运算符

Program 1:

程序1:

#include <iostream>
using namespace std;

class Test {
public:
    int A;
    Test()
    {
        A = 0;
    }

    Test(int a)
    {
        A = a;
    }
    void print()
    {
        cout << A << " ";
    }
};

Test operator*(Test T1, Test T2)
{
    Test temp;

    temp.A = T1.A * T2.A;

    return temp;
}

int main()
{
    Test T1(10);
    Test T2(5);
    Test T3;

    T3 = operator*(T1, T2);

    T3.print();

    return 0;
}

Output:

输出:

50

Explanation:

说明:

Here, we created a class Test with public data member A, and we defined constructors and print() member function, overloaded ‘*’ operator using non-member function. So, we passed two objects argument and returned a temporary object that contains the result of the multiplication.

在这里,我们使用公共数据成员 A创建了一个Test类,并定义了构造函数print()成员函数,并使用非成员函数重载了'*'运算符。 因此,我们传递了两个对象参数,并返回了一个包含乘法结果的临时对象。

In the main() function, we created three objects T1, T2, and T3. And then multiplication of T1 and T2 assigned to the object T3 using the overloaded function.

main()函数中,我们创建了三个对象T1T2T3 。 然后使用重载函数将分配给对象T3T1T2相乘。

Program 2:

程式2:

#include <iostream>
using namespace std;

class Test {
    int A;

public:
    Test()
    {
        A = 0;
    }
    Test(int a)
    {
        A = a;
    }

    int operator<(Test T)
    {
        int ret = 0;

        if (A < T.A)
            return 1;
        else
            return 0;
    }
};

int main()
{
    Test T1(10);
    Test T2(5);

    if (T1 < T2)
        cout << "T1 less than T2";
    else
        cout << "T2 less than T1";

    return 0;
}

Output:

输出:

T2 less than T1

Explanation:

说明:

Here, we created the class Test with data member A and defined two constructors. We also defined function to overload less than '<' operator to compare two objects. The overloaded function returned an integer value for comparison.

在这里,我们使用数据成员A创建了Test类,并定义了两个构造函数。 我们还将函数定义为重载小于'<'运算符以比较两个对象。 重载的函数返回一个整数值以进行比较。

In the main() function. Here we created T1 and T2 objects that initialized with 10 and 5 respectively. And we compared both objects then "T2 less than T1" message will be printed on the console screen.

main()函数中。 在这里,我们创建了分别用10和5初始化的T1T2对象。 然后我们比较了两个对象,然后控制台屏幕上将显示“ T2小于T1”消息。

Program 3:

程式3:

#include <iostream>
using namespace std;

class Test {
    int A;

public:
    Test()
    {
        A = 0;
    }
    Test(int a)
    {
        A = a;
    }

    int operator ::()
    {
        return A;
    }
};

int main()
{
    Test T1(10);

    cout << ::T1;

    return 0;
}

Output:

输出:

main.cpp:17:18: error: expected type-specifier before ‘::’ token
     int operator ::()
                  ^~
main.cpp: In function ‘int main()’:
main.cpp:27:13: error: ‘::T1’ has not been declared
     cout << ::T1;
             ^~

Explanation:

说明:

It will generate syntax error because we cannot overload "::" operator in C++.

因为我们不能在C ++中重载“ ::”运算符,所以它将产生语法错误。

翻译自: https://www.includehelp.com/cpp-tutorial/operator-overloading-find-output-programs-set-3.aspx

c++重载++运算符

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值