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 ++查找字符串