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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

c ++查找字符串_C ++类和对象| 查找输出程序| 套装4

發布時間:2023/12/1 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c ++查找字符串_C ++类和对象| 查找输出程序| 套装4 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

c ++查找字符串

Program 1:

程序1:

#include <iostream> using namespace std;class Sample {int X;int* const PTR = &X;public:void set(int x);void print(); };void Sample::set(int x) {*PTR = x; }void Sample::print() {cout << *PTR - EOF << " "; }int main() {Sample S;S.set(10);S.print();return 0; }

Output:

輸出:

11

Explanation:

說明:

In the above program, we created class Sample that contain member X and a constant pointer PTR that contains the address of X, here we cannot relocate the pointer but we can change the value of X using pointer PTR.

在上面的程序,我們創建的類樣品含有成員X和包含X的地址,在這里我們不能搬遷的指針,但我們可以用指針PTR改變X的值恒定的指針PTR。

Here, we defined two member functions set() and print() outside the class.

在這里,我們在類外部定義了兩個成員函數set()和print() 。

The set() function set the value to the data member X. and print() member function uses cout statement.

set()函數將值設置為數據成員X。 和print()成員函數使用cout語句。

cout << *PTR-EOF << " ";

The value of EOF is -1 then 10- -1 = 11.

EOF的值為-1,然后10- -1 = 11

Thus, 11 will be printed on the console screen.

因此,將在控制臺屏幕上打印11

Program 2:

程式2:

#include <iostream> using namespace std;class Sample {int X;int* const PTR = &X;public:void set(int x);void print(); };void Sample::set(int x) {*PTR = x; }void Sample::print() {cout << *PTR - EOF << " "; }int main() {Sample* S;S->set(10);S->print();return 0; }

Output:

輸出:

Segmentation fault (core dumped) OR Runtime error

Explanation:

說明:

The above code will generate a runtime error because in the above program we created the pointer of the class but, we did not initialize pointer PTR with any object of the Sample class. If we try to access the member of the class Sample then it will generate a runtime error.

上面的代碼將產生運行時錯誤,因為在上面的程序中我們創建了該類的指針,但沒有使用Sample類的任何對象初始化指針PTR 。 如果我們嘗試訪問Sample類的成員,則它將生成運行時錯誤

Program 3:

程式3:

#include <iostream> using namespace std;class Sample {int X;int* const PTR = &X;public:void set(int x);void print(); };void Sample::set(int x) {*PTR = x; }void Sample::print() {cout << *PTR - EOF << " "; }int main() {Sample* S = &(Sample());S->set(10);S->print();return 0; }

Output:

輸出:

main.cpp: In function ‘int main()’: main.cpp:25:27: error: taking address of temporary [-fpermissive]Sample* S = &(Sample());^

Explanation:

說明:

The above program will generate an error due to the below statement used in the main() function:

由于main()函數中使用了以下語句,因此上述程序將生成錯誤:

Sample *S = &(Sample());

Here we created a pointer of the class and tried to initialize with the object of the class, this is not the correct way. The correct way is,

這里我們創建了一個類的指針,并試圖用該類的對象進行初始化,這不是正確的方法。 正確的方法是

Sample OB; Sample *S = &OB;

Recommended posts

推薦的帖子

  • C++ Class and Objects | Find output programs | Set 1

    C ++類和對象| 查找輸出程序| 套裝1

  • C++ Class and Objects | Find output programs | Set 2

    C ++類和對象| 查找輸出程序| 套裝2

  • C++ Class and Objects | Find output programs | Set 3

    C ++類和對象| 查找輸出程序| 套裝3

  • C++ Class and Objects | Find output programs | Set 5

    C ++類和對象| 查找輸出程序| 套裝5

  • 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

  • C++ Default Argument | Find output programs | Set 1

    C ++默認參數| 查找輸出程序| 套裝1

  • C++ Default Argument | Find output programs | Set 2

    C ++默認參數| 查找輸出程序| 套裝2

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

    C ++數組| 查找輸出程序| 套裝1

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

    C ++數組| 查找輸出程序| 套裝2

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

    C ++數組| 查找輸出程序| 套裝3

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

    C ++數組| 查找輸出程序| 套裝4

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

    C ++數組| 查找輸出程序| 套裝5

翻譯自: https://www.includehelp.com/cpp-tutorial/class-and-objects-find-output-programs-set-4.aspx

c ++查找字符串

總結

以上是生活随笔為你收集整理的c ++查找字符串_C ++类和对象| 查找输出程序| 套装4的全部內容,希望文章能夠幫你解決所遇到的問題。

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