c语言指针++_C ++此指针| 查找输出程序| 套装2

c语言指针++

Program 1:

程序1:

#include <iostream>
using namespace std;

class Test {
    int VAL;

public:
    Test(int VAL)
    {
        this->VAL = VAL;
        cout << this->VAL;
    }
};

int main()
{
    Test T;

    return 0;
}

Output:

输出:

main.cpp: In function ‘int main()’:
main.cpp:17:10: error: no matching function for call to ‘Test::Test()’
     Test T;
          ^

Explanation:

说明:

The above code will generate an error, in the main() function we created the object class T. it will require a default or no-argument constructor which does not exist inside the class.

上面的代码将产生一个错误,在main()函数中,我们创建了对象类T。 它需要一个在类内部不存在的默认或无参数构造函数

Program 2:

程式2:

#include <iostream>
using namespace std;

class Test {
    int VAL;

public:
    Test(int VAL)
    {
        this->VAL = VAL;
        cout << this->VAL;
    }
};

int main()
{
    Test T(108);

    return 0;
}

Output:

输出:

108

Explanation:

说明:

Here, we created a class Test that contains a member VAL inside the class. And defined a parameterized constructor inside the class.

在这里,我们创建了一个类Test ,该类在类内部包含成员VAL 。 并在类内部定义了参数化的构造函数

In the constructor, we passed an argument VAL, and VAL is also a data member of the class so that we used this pointer, which is used to access data members. Then we are accessing data member VAL using this pointer and local variable VAL can be used directly.

构造函数中 ,我们传递了一个参数VAL ,并且VAL也是该类的数据成员 ,因此我们使用了此指针该指针用于访问数据成员。 然后,我们使用指针访问数据成员VAL,并且可以直接使用局部变量VAL

Then we are assigning value to the data member and print using cout.

然后,我们为数据成员分配值,并使用cout打印。

Program 3:

程式3:

#include <iostream>
using namespace std;

class Test {
    int VAL;

public:
    Test(int v)
    {
        VAL = v;
    }
    Test Sum(Test T1, Test T2)
    {
        VAL = T1.VAL + T2.VAL;

        return *this;
    }
    void print()
    {
        cout << VAL << " ";
    }
};

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

    Test T3(0);

    T3 = T1.Sum(T1, T2);

    T1.print();
    T2.print();
    T3.print();

    return 0;
}

Output:

输出:

30 20 30

Explanation:

说明:

Here, we created a class called Test that contains a data member VAL, parameterized constructor, and member functions sum() and print()

在这里,我们创建了一个称为Test的类,其中包含数据成员VAL ,参数化构造函数以及成员函数sum()print()

sum() function is taking two objects of Test class as arguments and returning the current object using *this because this is a pointer to the current object and *this represents the current object.

sum()函数将Test类的两个对象作为参数,并使用* this返回当前对象,因为这是指向当前对象的指针,并且* this表示当前对象。

In the main() function, we created three objects T1, T2, and T3 those are initialized with 10, 20, and 30 respectively.

main()函数中,我们创建了三个对象T1T2T3,它们分别用10、20和30初始化。

We are calling the sum() function using T1 object and passing T1 and T2 as arguments, then the addition of the value of T1 and T2 assigned to T3, and T1 is also containing the sum of T1 and T2, because,

我们使用T1对象并传递T1T2作为参数来调用sum()函数,然后将分配给T3T1T2的值相加,并且T1也包含T1T2的和,因为,

VAL = T1.VAL + T2.VAL;


翻译自: https://www.includehelp.com/cpp-tutorial/this-pointer-find-output-programs-set-2.aspx

c语言指针++

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值