c语言 关键字const_C ++ const关键字| 查找输出程序| 套装1

c语言 关键字const

Program 1:

程序1:

#include <iostream>
using namespace std;

void fun(int& A) const
{
    A = 10;
}

int main()
{
    int X = 0;

    fun(X);

    cout << X;

    return 0;
}

Output:

输出:

[Error] non-member function 'void fun(int)' cannot have const qualifier.

Explanation:

说明:

The above code will generate an error because we cannot define, the non-member function as a const. Without a const keyword the above code will work fine.

上面的代码将产生错误,因为我们无法将非成员函数定义为const 。 如果没有const关键字,则上面的代码可以正常工作。

Program 2:

程式2:

#include <iostream>
using namespace std;

int main()
{
    const int X = 0;
    int* ptr;

    ptr = &X;
    *ptr = 10;

    cout << X;

    return 0;
}

Output:

输出:

error: invalid conversion from ‘const int*’ to ‘int*’ [-fpermissive] ptr = &X;

Explanation:

说明:

The above program will generate an error in C++, C++ does not allow modification in a constant using pointer, but we can modify the value of a constant in C language. The below program in C language will work fine.

上面的程序在C ++中会产生错误,C ++不允许使用指针修改常量,但是我们可以在C语言中修改常量的值。 下面的C语言程序可以正常运行。

#include <stdio.h>

int main()
{
    const int X = 0;
    int* ptr;

    ptr = &X;
    *ptr = 10;

    printf("%d", X);

    return 0;
}

Program may run on C language compiler, but it is not a standard that we can change the constant. In C language compiler – it can be changed through the pointer.

程序可以在C语言编译器上运行,但是我们不能更改常量不是标准。 在C语言编译器中–可以通过指针进行更改。

Program 3:

程式3:

#include <iostream>
using namespace std;

class Sample {
    const int A;
    const int B;

public:
    Sample(): A(10), B(20)
    {
    }
    void print()
    {
        cout << A << " " << B;
    }
};

int main()
{
    Sample S;

    S.print();

    return 0;
}

Output:

输出:

10 20

Explanation:

说明:

The above code will print "10 20" on the console screen.

上面的代码将在控制台屏幕上显示“ 10 20”

Let's understand the program step by step.

让我们逐步了解程序。

Here we created a class Sample that contains two const data members A and B. As we know that we can assign the values of constant at the time of declaration. But here we use the concept of member initialize list.

在这里,我们创建了一个Sample类,其中包含两个const数据成员AB。 众所周知,我们可以在声明时分配常量的值。 但是这里我们使用成员初始化列表的概念。

Sample ():A(10),B(20)
{
}

We can assign value to const data members using the member initialize list. We can initialize members by a colon (:) with members and value in the constructor.

我们可以使用成员初始化列表为const数据成员分配值。 我们可以初始化一个冒号成员:在构造函数中成员和值()。

Here we also defined a print() member function, which is used to print values of data members.

在这里,我们还定义了一个print()成员函数,该函数用于打印数据成员的值。

Recommended posts

推荐的帖子

翻译自: https://www.includehelp.com/cpp-tutorial/const-keyword-find-output-programs-set-1.aspx

c语言 关键字const

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值