日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/11 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

推薦的帖子

  • C++ const Keyword | Find output programs | Set 2

    C ++ const關鍵字| 查找輸出程序| 套裝2

  • C++ Operators | Find output programs | Set 1

    C ++運算符| 查找輸出程序| 套裝1

  • C++ Operators | Find output programs | Set 2

    C ++運算符| 查找輸出程序| 套裝2

  • C++ Reference Variable| Find output programs | Set 1

    C ++參考變量| 查找輸出程序| 套裝1

  • C++ Reference Variable| Find output programs | Set 2

    C ++參考變量| 查找輸出程序| 套裝2

  • C++ Conditional Statements | Find output programs | Set 1

    C ++條件語句| 查找輸出程序| 套裝1

  • C++ Conditional Statements | Find output programs | Set 2

    C ++條件語句| 查找輸出程序| 套裝2

  • C++ Switch Statement | Find output programs | Set 1

    C ++轉換語句| 查找輸出程序| 套裝1

  • C++ Switch Statement | Find output programs | Set 2

    C ++轉換語句| 查找輸出程序| 套裝2

  • C++ goto Statement | Find output programs | Set 1

    C ++ goto語句| 查找輸出程序| 套裝1

  • C++ goto Statement | Find output programs | Set 2

    C ++ goto語句| 查找輸出程序| 套裝2

  • C++ Looping | Find output programs | Set 1

    C ++循環| 查找輸出程序| 套裝1

  • C++ Looping | Find output programs | Set 2

    C ++循環| 查找輸出程序| 套裝2

  • C++ Looping | Find output programs | Set 3

    C ++循環| 查找輸出程序| 套裝3

  • C++ Looping | Find output programs | Set 4

    C ++循環| 查找輸出程序| 套裝4

  • C++ Looping | Find output programs | Set 5

    C ++循環| 查找輸出程序| 套裝5

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

c語言 關鍵字const

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的c语言 关键字const_C ++ const关键字| 查找输出程序| 套装1的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。