c語言指針++_C ++此指針| 查找輸出程序| 套裝3

c語言指針++

Program 1:

程序1:

#include <iostream>
using namespace std;
class Test {
int VAL;
public:
Test(int v)
{
VAL = v;
}
Test* Sum(Test T1, Test T2)
{
VAL = T1.VAL + T2.VAL;
return this;
}
void print()
{
cout << VAL << " ";
}
};
int main()
{
Test T1(10);
Test T2(20);
Test* T3;
T3 = T1.Sum(T1, T2);
T1.print();
T2.print();
T3->print();
return 0;
}

Output:

輸出:

30 20 30

Explanation:

說明:

Consider the sum() function, the function is taking two objects of Test class arguments and returning the pointer of the current object using this.?

考慮sum()函數,該函數接收Test類參數的兩個對象,并使用this返回當前對象的指針。

And, in the main() function, we created two objects T1, T2, and a pointer T3, which is holding the current object pointer returned by sum(). The sum() is adding the values of T1 and T2 and assigning in T1 because we are calling the function sum() using T1 and returning the address of T1 which is assigning to the pointer T3.

并且,在main()函數中,我們創建了兩個對象T1T2和一個指針T3 ,該指針保存了sum()返回的當前對象指針。 sum()T1T2的值相加并在T1中賦值,因為我們正在使用T1調用函數sum()并返回分配給指針T3T1地址。

Program 2:

程式2:

#include <iostream>
using namespace std;
class Test {
public:
Test call1()
{
cout << "call1 ";
return *this;
}
Test call2()
{
cout << "call2 ";
return *this;
}
Test call3()
{
cout << "call3 ";
return *this;
}
};
int main()
{
Test T1;
T1.call1().call2().call3();
return 0;
}

Output:

輸出:

call1 call2 call3

Explanation:

說明:

Here, we implemented a cascaded function call using this pointer, and created the class Test with 3 member functions call1(), call2(), and call3(). All these functions will return the current object of the class using *this.

在這里,我們使用此指針實現了級聯函數調用,并使用3個成員函數 call1()call2()call3()創建了Test類。 所有這些函數都將使用* this返回類的當前對象。

翻譯自: https://www.includehelp.com/cpp-tutorial/this-pointer-find-output-programs-set-3.aspx

c語言指針++

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

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

相關文章

線性表(代碼、分析、匯編)

目錄&#xff1a;代碼&#xff1a;分析&#xff1a;匯編&#xff1a;代碼&#xff1a; LinkList.h #ifndef _LINKLIST_H_ #define _LINKLIST_H_typedef void LinkList; //定義線性表類型 typedef struct _tag_LinkListNode LinkListNode;//定義線性表節點類型 struct _tag_Li…

WPF 操作 richTextBox

FROM:http://blog.csdn.net/wuzhengqing1/article/details/7010902 取出richTextBox里面的內容 第一種方法&#xff1a; 將richTextBox的內容以字符串的形式取出 string xw System.Windows.Markup.XamlWriter.Save(richTextBox.Document); 第二種方法&#xff1a;將richTe…

微軟企業庫4.1學習筆記(八)創建對象 續集2

3.3通過配置指定和Unity的整合 另外一種方法是在配置源中指定配置的需要&#xff0c;你可以指定下面的一條或者多條&#xff1a; 你可以在Unity配置中指定想要的BlockExtensions  你可以在Unity配置中的type配置節指定如何創建企業庫對象&#xff0c;指定類型映射的關系&…

已知有幾個數據存放在BUF為首址的字節存儲區中,試統計其中正數的個數,并將結果存入ZNUM單元中。

已知有幾個數據存放在BUF為首址的字節存儲區中&#xff0c;試統計其中正數的個數&#xff0c;并將結果存入ZNUM單元中。 P160 例4.17 匯編思路&#xff1a;DATA段&#xff0c;定義BUF存儲區&#xff0c;定義一下DB類型的數據&#xff0c;N為定義數據的總個數&#xff0c;ZNUM…

靜態鏈表(代碼、分析、匯編)

目錄&#xff1a;代碼&#xff1a;分析&#xff1a;匯編&#xff1a;代碼&#xff1a; StaticList.h #ifndef _STATICLIST_H_ #define _STATICLIST_H_typedef void StaticList; //空類型靜態表類型可以接收任何類型的靜態表類型 typedef void StaticListNode;//空類型節點類型…

c語言 typedef_C Typedef-能力傾向問題與解答

c語言 typedefC programming Typedef Aptitude Questions and Answers: In this section you will find C Aptitude Questions and Answers on typedef topics, defining/changing name of any data type, using and accessing the typedef values. C編程Typedef Aptitude問答&…

ios程序 調試log宏的添加

#ifdef DEBUG # define LOG(...) NSLog(__VA_ARGS__) # define LOG_CURRENT_METHOD NSLog("%-%", NSStringFromClass([self class]), NSStringFromSelector(_cmd)) #else # define LOG(...) ; # define LOG_CURRENT_METHOD ; #endif 使用 LOG_CURRENT_METHOD; NS…

Python的線程池實現

代碼 1 #coding:utf-82 3 #Python的線程池實現4 5 importQueue6 importthreading7 importsys8 importtime9 importurllib10 11 #替我們工作的線程池中的線程12 classMyThread(threading.Thread):13 def__init__(self, workQueue, resultQueue,timeout30, **kwargs):14 threadin…

編程統計BUF字單元數據中所含1的個數,并將結果存入COUNT單元中。

編程統計BUF字單元數據中所含1的個數&#xff0c;并將結果存入COUNT單元中。 代碼如下&#xff1a; DATA SEGMENT BUF DW 2345H ;隨機存儲一下數據 COUNT DB ? ;用于統計BUF字單元數據中所含1的個數 DATA ENDS STACK SEGMENT STACKDB 100 DUP(?);在堆棧段開辟一段大小為1…

循環鏈表(代碼、分析、匯編)

目錄&#xff1a;代碼&#xff1a;分析&#xff1a;匯編&#xff1a;代碼&#xff1a; CircleList.h #ifndef _CIRCLELIST_H_ #define _CIRCLELIST_H_typedef void CircleList;typedef struct _tag_CircleListNode CircleListNode;struct _tag_CircleListNode{CircleListNode…

Java Throwable setStackTrace()方法與示例

Throwable類setStackTrace()方法 (Throwable Class setStackTrace() method) setStackTrace() Method is available in java.lang package. setStackTrace()方法在java.lang包中可用。 setStackTrace() Method is used to sets stack trace elements that will be retrieved by…

IOS中設置全局變量

轉&#xff1a;http://blog.csdn.net/totogogo/article/details/7355203 有幾種方法 some developers recommend use singleton patter (ref link http://blog.csdn.net/kmyhy/article/details/7026511) 方法1&#xff1a;使用靜態變量 (不推薦&#xff09; 方法2&#xff1a; …

設計模式之Observer

觀察者模式可以參考郵件訂閱的例子 郵件訂閱設計到2個主要角色&#xff0c;一個是訂閱者(觀察者)&#xff0c;一個是發布者 發布者可以擁有一個觀察者的集合&#xff0c;可以添加&#xff0c;刪除觀察者&#xff0c;當發布者發布一個新的消息時&#xff0c;要郵件通知觀察者集合…

編寫一個程序,計算|X-Y|的值,并將結果存入RESULT單元中,其中X和Y都為帶符號字數據。

編寫一個程序&#xff0c;計算|X-Y|的值&#xff0c;并將結果存入RESULT單元中&#xff0c;其中X和Y都為帶符號字數據。 P154 例4.11 匯編思路:DATA段定義X、Y、RESULE分別用于存放隨機數、存放隨機數、存放最后計算結果。STACK段定義100DB大小的堆棧段運算存儲空間。將AX獲取…

java timezone_Java TimeZone inDaylightTime()方法及示例

java timezoneTimeZone類inDaylightTime()方法 (TimeZone Class inDaylightTime() method) inDaylightTime() method is available in java.util package. inDaylightTime()方法在java.util包中可用。 inDaylightTime() method is used to check whether the given date (d) is…

這幾天好像博客登不上去 什么情況

這幾天好像博客登不上去 什么情況 我多年的心情記錄啊 還以為關掉了 。。。。。。。 今天很生氣&#xff0c;麻痹 轉載于:https://www.cnblogs.com/cloud/archive/2010/04/25/1720744.html

雙向鏈表(代碼、分析、匯編)

目錄&#xff1a;代碼&#xff1a;分析&#xff1a;匯編&#xff1a;代碼&#xff1a; DLinkList.h #ifndef _DLINKLIST_H_ #define _DLINKLIST_H_typedef void DLinkList; typedef struct _tag_DLinkListNode DLinkListNode; struct _tag_DLinkListNode {DLinkListNode* nex…

[道理]關于人生的,很不錯!

[道理]關于人生的&#xff0c;很不錯&#xff01; 心理學課上&#xff0c;周正教授正在授課&#xff1a;“上次下課時&#xff0c;一個男孩子遞了張紙條&#xff1a;我是個比較內向的人&#xff0c;又沒什么特長&#xff0c;不會踢足球&#xff0c;不會打籃球……唯一的愛好是寫…

變量和簡單數據類型(一)

1&#xff0c;title()方法 將字符串中的每個單詞的首字符大寫 2&#xff0c;upper()方法 將字符串的所有字母大寫 3&#xff0c;lower()方法 將字符串的所有字母小寫 name "beyond Sq" print(name.title()) print(name.upper()) print(name.lower())調用方式&…

long類型20位示例_Java Long類lowerOneBit()方法與示例

long類型20位示例長類lowerOneBit()方法 (Long class lowestOneBit() method) lowestOneBit() method is available in java.lang package. minimumOneBit()方法在java.lang包中可用。 lowestOneBit() method is used to find at most only single 1’s bit from the rightmost…