c ++查找字符串_C ++類和對象| 查找輸出程序| 套裝5

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542236.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542236.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542236.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

mysql8和5.7區別_mysql8.0與mysql5.7安全加密小差別

今天升級到了mysql8.0 做主從同步遇到下面問題2020-07-21T14:09:52.626718Z 13 [ERROR] [MY-010584] [Repl] Slave I/O for channel : error connecting to master slave_replication172.20.0.2:3306 - retry-time: 60 retries: 1 message: Authentication plugin caching_sha2…

c ++查找字符串_C ++類和對象| 查找輸出程序| 套裝3

c 查找字符串Program 1: 程序1&#xff1a; #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;…

時間輪

老早之前就聽說時間輪算法特別高效&#xff0c;Linux內核都用的它&#xff0c;這兩天抽空實現了遍……嗯&#xff0c;被差一bug搞死(~&#xffe3;▽&#xffe3;~) 啊哈 網上扣來的圖&#xff0c;原理好懂&#xff1a;輪子里的每格代表一小段時間&#xff08;精度&#xff09;…

qc35 說明書_使用Bose QC35 2年的心得 | 遲而不遲的深度體驗 | 文附佩戴效果照片...

小編注&#xff1a;此篇文章來自即可瓜分10萬金幣&#xff0c;周邊好禮達標就有&#xff0c;邀新任務獎勵無上限&#xff0c;點擊查看活動詳情創作立場聲明&#xff1a;本文所測商品為自費購入&#xff0c;我會在文中點明。堅持來自內心的主觀評測是起碼的底線&#xff0c;不會…

threadgroup_Java ThreadGroup類的checkAccess()方法和示例

threadgroupThreadGroup類的checkAccess()方法 (ThreadGroup class checkAccess() method) checkAccess() method is available in java.lang package. checkAccess()方法在java.lang包中可用。 checkAccess() method is used to check whether the currently running thread h…

qt tab彈出特效_Nuke Studio 12(影視特效合成軟件)中文版分享

Nuke 12是一款功能強大&#xff0c;世界知名的影視后期特效合成軟件。NUKE是一個獲得學院獎(Academy Award)的數碼合成軟件。已經經過10年的歷練&#xff0c;為藝術家們提供了創造具有高質素的相片效果的圖像的方法。NUKE無需專門的硬件平臺&#xff0c;但卻能為藝術家提供組合…

c ++ 鏈表_C ++程序查找兩個單個鏈表的并集

c 鏈表Problem statement: Write a C program to find the union of two single linked lists. 問題陳述&#xff1a;編寫一個C 程序來查找兩個單個鏈表的并集。 Example: 例&#xff1a; Let the first linked list be:5->4->3->6->1->NULLLet the second l…

精華版線段樹模板

哈哈哈&#xff0c;打了一上午。。。#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> using namespace std; typedef long long ll; ll a[10000010]; ll lazy[1000000]; …

【轉】unity地形插件T4M使用幫助

unity的地形系統在手機游戲中因為效率問題基本無法使用&#xff0c;只能通過T4M這個地形插件來進行優化制作。下面大概講解一下使用流程及方法。 先中U3D里面用自帶的地形系統刷出想要的地形和貼圖。貼圖可以大概刷一下。后面要重新刷。 用導出腳本ExportTerrain.js導出地形為O…

ansys添加力矩_ANSYS軟件中施加扭矩的方法

ANSYS軟件中施加扭矩的方法胡意立&#xff0c;孫明禮&#xff0c;沈燕青&#xff0c;周佳杰&#xff0c;胡林強【摘要】在機械結構的有限元分析中&#xff0c;常會遇到施加扭矩的問題。文中探討了在ANSYS軟件中施加扭矩的一種方法&#xff0c;以在一個六棱柱一端施加扭矩為實例…

Python | 程序從列表中刪除重復的元素

Example: 例&#xff1a; Input:list1: [10, 20, 10, 20, 30, 40, 30, 50]Output:List after removing duplicate elementslist2: [10, 20, 30, 40, 50]Logic: 邏輯&#xff1a; To implement the program is too easy, we have to append elements one by one to another…

Linux的簡介與虛擬機的管理

Linux的簡介&#xff1a; 嚴格的來講&#xff0c;Linux不算是一個操作系統&#xff0c;只是一個Linux系統中的內核&#xff0c;Linux的全稱是GUN/Linux&#xff0c;這才算是一個真正意義上的Linux系統。 Linux是一個多用戶多任務的操作系統&#xff0c;擁有良好的用戶界面&…

python遞歸查找_Python程序使用遞歸查找數字的冪

python遞歸查找Given the base x and the power y and we have to find the x to the power y using recursion in Python. 給定基數x和冪y &#xff0c;我們必須使用Python中的遞歸找到x到冪y 。 By using recursion – We will be multiplying a number (initially with val…

phalapi可以依賴注入么_PHP 依賴注入

通常調用一個類里面的方法需要如何操作&#xff1a;$class new class();$class->fun()依賴注入模式用來減少程序間的耦合依賴注入共有三種模式&#xff1a;setter 方法注入著重說下setter方法注入并結合ArrayAccess/*** Class Di* property People*/class Di implements Ar…

R語言:ggplot2精細化繪圖——以實用商業化圖表繪圖為例(轉)

本文旨在介紹R語言中ggplot2包的一些精細化操作&#xff0c;主要適用于對R畫圖有一定了解&#xff0c;需要更精細化作圖的人&#xff0c;尤其是那些剛從excel轉ggplot2的各位&#xff0c;有比較頻繁的作圖需求的人。不討論那些樣式非常酷炫的圖表&#xff0c;以實用的商業化圖表…

Linux中常用的命令

1.文件建立 touch file&#xff08;文件的名字&#xff09; 注意&#xff1a; touch不但可以建立文件也可以修改文件的時間戳 時間戳分為&#xff1a; atime&#xff1a;文件內容被訪問的時間標識 mtime&#xff1a;文件內容被修改的時間標識 ctime&#xff1a;文件屬性或文件內…

藍橋杯寶藏排序題目算法(冒泡、選擇、插入)

冒泡排序: def bubble_sort(li): # 函數方式for i in range(len(li)-1):exchangeFalsefor j in range(len(li)-i-1):if li[j]>li[j1]:li[j],li[j1]li[j1],li[j]exchangeTrueif not exchange:return 選擇排序: 從左往右找到最小的元素&#xff0c;放在起始位置…

hive分區用2個字段有何限制_[特性]Hive動態分區功能使用

[特性]Hive動態分區功能使用2016-01-31 21:40說明Hive有兩種分區&#xff0c;一種是靜態分區&#xff0c;也就是普通的分區。另一種是動態分區。動態分區在數據導入時&#xff0c;會根據具體的字段值自行決定導入&#xff0c;并創建相應的分區。使用上更為方面。舉例準備工作創…

Linux系統中輸出輸入的管理

1.什么是輸入和輸出 輸入和輸出是計算機系統中的主機與外部進行通信的系統。它由外圍設備和輸入輸出控制系統兩部分組成&#xff0c;我們在shell中鍵入指令&#xff0c;然后送入CPU中運算產生結果&#xff0c;再將結果送到字符設備中顯示。簡單點來說輸入輸出就是通過我們的鍵盤…

find 命令示例_數組find()方法以及JavaScript中的示例

find 命令示例JavaScript find()方法 (JavaScript find() method) find() method is used to get the first element from an array which passes the given test (condition). find()方法用于從通過給定測試(條件)的數組中獲取第一個元素。 Syntax: 句法&#xff1a; array.…