c ++查找字符串_C ++类和对象| 查找输出程序| 套装3
c ++查找字符串
Program 1:
程序1:
#include <iostream> using namespace std;class Sample {int X;public:void set(int x){X = x;}void print(){cout << X << " ";}} A, B;int main() {A.set(10);B.set(20);A.print();B.print();return 0; }Output:
輸出:
10 20Explanation:
說明:
In the above program, we created class Sample and two objects A and B using before semicolon at the end of the class.
在上面的程序中,我們創建了Sample類,并在類末尾使用分號前的兩個對象A和B。
In the main() function, we call set() and print() function, then it will print "10 20" on the console screen.
在main()函數中,我們調用set()和print()函數,然后它將在控制臺屏幕上打印“ 10 20” 。
Program 2:
程式2:
#include <iostream> using namespace std;const class Sample {int X;public:void set(int x){X = x;}void print(){cout << X << " ";} };int main() {Sample A;Sample B;A.set(10);B.set(20);A.print();B.print();return 0; }Output:
輸出:
main.cpp:4:1: error: ‘const’ can only be specified for objects and functionsconst class Sample {^~~~~Explanation:
說明:
The above program will generate an error because we cannot use the const keyword with class declaration. The const keyword can be used with data members and member functions of the class.
上面的程序將產生錯誤,因為我們不能在類聲明中使用const關鍵字 。 const關鍵字可與類的數據成員和成員函數一起使用。
Program 3:
程式3:
#include <iostream> using namespace std;class Sample {int X;public:void set(int x);void print(); };void Sample : set(int x) {X = x; }void Sample : print() {cout << X << " "; }int main() {Sample A;Sample B;A.set(10);B.set(20);A.print();B.print();return 0; }Output:
輸出:
main.cpp:12:13: error: found ‘:’ in nested-name-specifier, expected ‘::’void Sample : set(int x)^ main.cpp:17:13: error: found ‘:’ in nested-name-specifier, expected ‘::’void Sample : print()^Explanation:
說明:
The above program will generate errors because in the above program we declare member function inside the class and defined outside the class. But we use the colon operator instead of scope resolution operator ("::" ).
上面的程序將產生錯誤,因為在上面的程序中,我們在類內部聲明成員函數 ,并在類外部定義成員函數 。 但是我們使用冒號運算符而不是范圍解析運算符(“ ::”) 。
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 4
C ++類和對象| 查找輸出程序| 套裝4
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-3.aspx
c ++查找字符串
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的c ++查找字符串_C ++类和对象| 查找输出程序| 套装3的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 判断五个分数等级划分_压力表精度等级怎么
- 下一篇: 网际控制报文协议icmp_网络中的ICM