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數據成員A和B。 眾所周知,我們可以在聲明時分配常量的值。 但是這里我們使用成員初始化列表的概念。
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