c ++查找字符串
Program 1:
程序1:
#include <iostream>
using namespace std;
class Sample {
int X;
int* PTR = &X;
public:
void set(int x) const;
void print();
};
void Sample::set(int x) const
{
*PTR = x;
}
void Sample::print()
{
cout << *PTR - EOF << " ";
}
int main()
{
Sample OB;
Sample* S = &OB;
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() 。
Here, set() is a const member function, but we can assign value using pointer variable in set() function.
這里, set()是const成員函數,但是我們可以在set()函數中使用指針變量來賦值。
In the main() function, we created object of class and created pointer initialized with object OB, and print() member function uses cout statement.
在main()函數中,我們創建了class對象,并創建了用對象OB初始化的指針,而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 {
private:
int* X;
public:
void init()
{
X = new int();
*X = 5;
}
void fun()
{
*X = *X * *(&(*X)) + 10;
cout << *X;
}
};
int main()
{
Sample OB;
OB.init();
OB.fun();
return 0;
}
Output:
輸出:
35
Explanation:
說明:
In the above program, we created a class Sample that contains an integer pointer X and then we initialize pointer X in init() member function using the new operator. And assign with value 5.
在上述程序中,我們創建了一個包含整數指針 X的類Sample ,然后使用new運算符在init()成員函數中初始化了指針X。 并賦值5。
Now, come to the member function fun() and evaluate the expression,
現在,進入成員函數fun()并計算表達式,
*X = *X* *(&(*X))+10;
*X = 5 * 5+10;
*X = 25+10;
*X = 35;
Then the final value of *X is 35 that will be printed on the console screen.
然后, * X的最終值為35 ,它將在控制臺屏幕上打印。
Program 3:
程式3:
#include <iostream>
using namespace std;
class Sample {
private:
int* X;
int* Y;
public:
void init()
{
X = new int();
Y = new int();
X = 10;
Y = 20;
}
void fun()
{
*X = *X + *Y;
cout << *X;
}
};
int main()
{
Sample OB;
OB.init();
OB.fun();
return 0;
}
Output:
輸出:
main.cpp: In member function ‘void Sample::init()’:
main.cpp:15:13: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
X = 10;
^~
main.cpp:16:13: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
Y = 20;
^~
Explanation:
說明:
The above program will generate errors. Because in the init() member function, we assigned integer value to the pointer variable, if we want to assign value to pointer variable then we need to use asterisk (*) operator like,
上面的程序將產生錯誤。 因為在init()成員函數中,我們為指針變量分配了整數值,所以,如果我們想為指針變量分配值,則需要使用星號(*)運算符,例如
*X = 10;
*Y = 20;
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 4
C ++類和對象| 查找輸出程序| 套裝4
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-5.aspx
c ++查找字符串