




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012第第6 6章章 類與對象類與對象 6.1 6.1 類與對類與對象象 6.2 6.2 構造函數(shù)和析構函數(shù)構造函數(shù)和析構函數(shù) 6.3 6.3 類的其他成員類的其他成員 6.4 6.4 類的包含類的包含 小結小結華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012 類(類(ClassClass)是面向對象程序設計()是面向對象程序設計(OOPOOP)實現(xiàn)信息封裝的基礎。)實現(xiàn)信息封裝的基礎。 類是用戶定義類型,也稱為類類型類是用戶定義類型,也稱為類類型 每個類包含數(shù)據(jù)說明和一組操作數(shù)據(jù)或傳遞消息的函數(shù)
2、。類的每個類包含數(shù)據(jù)說明和一組操作數(shù)據(jù)或傳遞消息的函數(shù)。類的 實例稱為對象實例稱為對象第第6 6章章 類與對象類與對象 華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012 面向對象編程的程序基本單位是類面向對象編程的程序基本單位是類 類是數(shù)據(jù)和操作數(shù)據(jù)的函數(shù)的封裝類是數(shù)據(jù)和操作數(shù)據(jù)的函數(shù)的封裝 類的對象使用自己的方法完成對數(shù)據(jù)的操作類的對象使用自己的方法完成對數(shù)據(jù)的操作 類可以隱藏數(shù)據(jù)和操作細節(jié),對象通過類接口與外部通信類可以隱藏數(shù)據(jù)和操作細節(jié),對象通過類接口與外部通信 6.1 6.1 類與對象類與對象 華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20
3、1220126.1 類與對象/ 排序函數(shù)原型void Sort (int , int ) ; / 數(shù)組相加函數(shù)原型void Add ( int , int , int ) ; int main() int a 10 , b 10 ; . Sort ( a , 10 ) ; Sort ( b , 10 ) ; Add ( a , b , 10) ; .數(shù)組與數(shù)組類數(shù)組與數(shù)組類華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012class Array/定義數(shù)組類 int *ap ; int len ; public: Array( int size )/ 建立數(shù)組 len=
4、 size ; ap = new int size ; void Sort ( ) ;/ 排序 / 重載算符 +函數(shù) Array operaor + (const Array & other) ; int main() Array a(10) , b(10) ;/ 聲明對象 . a . Sort() ; b . Sort() ; / 調用排序方法 a = a + b ;/ 數(shù)組相加 ./ 排序函數(shù)原型void Sort (int , int ) ; / 數(shù)組相加函數(shù)原型void Add ( int , int , int ) ; int main() int a 10 , b 10 ;
5、 . Sort ( a , 10 ) ; Sort ( b , 10 ) ; Add ( a , b , 10) ; .數(shù)組與數(shù)組類數(shù)組與數(shù)組類6.1 類與對象華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012class Array/定義數(shù)組類 int *ap ; int len ; public: Array( int size )/ 建立數(shù)組 len= size ; ap = new int size ; void Sort ( ) ;/ 排序 / 重載算符 +函數(shù) Array operaor + (const Array & other) ; int m
6、ain() Array a(10) , b(10) ;/ 聲明對象 . a . Sort() ; b . Sort() ; / 調用排序方法 a = a + b ;/ 數(shù)組相加 ./ 排序函數(shù)原型void Sort (int , int ) ; / 數(shù)組相加函數(shù)原型void Add ( int , int , int ) ; int main() int a 10 , b 10 ; . Sort ( a , 10 ) ; Sort ( b , 10 ) ; Add ( a , b , 10) ; .數(shù)組與數(shù)組類數(shù)組與數(shù)組類class Array/定義數(shù)組類 int *ap ; int len
7、; public: Array( int size )/ 建立數(shù)組 len= size ; ap = new int size ; void Sort ( ) ;/ 排序 / 重載算符 +函數(shù) Array operaor + (const Array & other) ;類是數(shù)據(jù)和操作數(shù)據(jù)的函數(shù)的封裝6.1 類與對象華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012class Array/定義數(shù)組類 int *ap ; int len ; public: Array( int size )/ 建立數(shù)組 len= size ; ap = new int size
8、 ; void Sort ( ) ;/ 排序 / 重載算符 +函數(shù) Array operaor + (const Array & other) ; int main() Array a(10) , b(10) ;/ 聲明對象 . a . Sort() ; b . Sort() ; / 調用排序方法 a = a + b ;/ 數(shù)組相加 ./ 排序函數(shù)原型void Sort (int , int ) ; / 數(shù)組相加函數(shù)原型void Add ( int , int , int ) ; int main() int a 10 , b 10 ; . Sort ( a , 10 ) ; Sort
9、 ( b , 10 ) ; Add ( a , b , 10) ; .數(shù)組與數(shù)組類數(shù)組與數(shù)組類對象使用自己的方法對數(shù)據(jù)操作6.1 類與對象華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 201220126.1 類與對象華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012class 鐘 private : 鐘的構造; public : 讀取時間值讀取時間值 ; 調整時間值調整時間值 ;對象通過類接口與外部通信6.1 類與對象華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.1 6.1.1 定義類和對象定義類和對象 / 例例
10、6-1 一個類的例子一個類的例子#includeusing namespace std ;class Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;int main() Td
11、ate a ; a.Set (10, 16, 2003) ; a.Print() ;華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.1 6.1.1 定義類和對象定義類和對象 / 例6-1 一個類的例子#includeusing namespace std ;class Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%4
12、00 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;int main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ;class Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400
13、= 0); void Print() cout year . month . day endl ; private: int month; int day; int year;華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.1 6.1.1 定義類和對象定義類和對象 / 例6-1 一個類的例子#includeusing namespace std ;class Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() retu
14、rn ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;int main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ;class Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return
15、( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;關鍵字關鍵字定義一個類定義一個類標識符標識符類名類名 class ,struct,union 都可以定義一個類:都可以定義一個類: class缺省說明時,其成員被認為缺省說明時,其成員被認為是私有的是私有的 struct 若不特別指出,其所有成員若不特別指出,其所有成員都是公有的都是公有的 union其所有成員都是公有的,且其所有
16、成員都是公有的,且不能更改不能更改華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.1 6.1.1 定義類和對象定義類和對象 / 例例6-1 一個類的例子一個類的例子#includeusing namespace std ;class Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Prin
17、t() cout year . month . day endl ; private: int month; int day; int year;int main() Tdate ; a.Set (10, 16, 2003) ; a.Print() ;Tdate 類型的一個類型的一個對象(實例)對象(實例)華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.1 6.1.1 定義類和對象定義類和對象 / 例例6-1 一個類的例子一個類的例子#includeusing namespace std ;class Tdate public: void Set(int
18、 m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;int main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ; 類由成員構成: 數(shù)據(jù)成員數(shù)據(jù)成員描述對象的屬性 成員函數(shù)成員函數(shù)描述
19、對象的方法華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.1 6.1.1 定義類和對象定義類和對象 / 例例6-1 一個類的例子一個類的例子#includeusing namespace std ;class Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout yea
20、r . month . day endl ; private: int month; int day; int year;int main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ;int month; int day; int year;數(shù)據(jù)成員數(shù)據(jù)成員華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.1 6.1.1 定義類和對象定義類和對象 / 例例6-1 一個類的例子一個類的例子#includeusing namespace std ;class Tdate public: void Set(int
21、m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;int main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ; void Set(int m, int d, int y ) mont
22、h = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; 類中定義成員函數(shù)類中定義成員函數(shù)內聯(lián)函數(shù)處理內聯(lián)函數(shù)處理華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.1 6.1.1 定義類和對象定義類和對象 / 例例6-1 一個類的例子一個類的例子#includeusing namespace std ;class Tda
23、te public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;int main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ; void Set(in
24、t m, int d, int y ) ; int IsLeapYear() ; void Print() ;在類外定義在類外定義成員函數(shù)成員函數(shù) void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year.month.dayendl ; 華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.1 6
25、.1.1 定義類和對象定義類和對象 / 例6-1 一個類的例子#includeusing namespace std ;class Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int yea
26、r;int main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ;成員的性質由關鍵字成員的性質由關鍵字public、protected、private 決定決定公有公有 公有段的成員是提供給外部的接口公有段的成員是提供給外部的接口 保護保護 保護段成員在該類和它的派生類中可見保護段成員在該類和它的派生類中可見 私有私有 私有段成員僅在類中可見私有段成員僅在類中可見各段中既可以包含數(shù)據(jù)成員,也可以包含成員函數(shù)各段中既可以包含數(shù)據(jù)成員,也可以包含成員函數(shù)華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.1 6.1
27、.1 定義類和對象定義類和對象 / 例6-1 一個類的例子#includeusing namespace std ;class Tdate public: void Set(int m, int d, int y ) month = m ; day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;
28、int main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ; class 類名類名 public:公有段數(shù)據(jù)成員和成員函數(shù)公有段數(shù)據(jù)成員和成員函數(shù) ; protected: 保護段數(shù)據(jù)成員和成員函數(shù)保護段數(shù)據(jù)成員和成員函數(shù) ; private:私有數(shù)據(jù)成員和成員函數(shù)私有數(shù)據(jù)成員和成員函數(shù) ; ; 華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.1 6.1.1 定義類和對象定義類和對象 類說明的一般形式為:類說明的一般形式為: class 類名類名 public:公有段數(shù)據(jù)成員和成員函數(shù)公有段數(shù)據(jù)成員和成員
29、函數(shù) ; protected: 保護段數(shù)據(jù)成員和成員函數(shù)保護段數(shù)據(jù)成員和成員函數(shù) ; private:私有數(shù)據(jù)成員和成員函數(shù)私有數(shù)據(jù)成員和成員函數(shù) ; ; 1. 允許已定義類名出現(xiàn)在類的說明中允許已定義類名出現(xiàn)在類的說明中例:例: class link link * next ; ;/ 聲明一個指向聲明一個指向link類類型的指針類類型的指針華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.1 6.1.1 定義類和對象定義類和對象 類說明的一般形式為:類說明的一般形式為: class 類名類名 public:公有段數(shù)據(jù)成員和成員函數(shù)公有段數(shù)據(jù)成員和成員函數(shù)
30、 ; protected: 保護段數(shù)據(jù)成員和成員函數(shù)保護段數(shù)據(jù)成員和成員函數(shù) ; private:私有數(shù)據(jù)成員和成員函數(shù)私有數(shù)據(jù)成員和成員函數(shù) ; ; 1. 允許已定義類名出現(xiàn)在類的說明中允許已定義類名出現(xiàn)在類的說明中例:例: class X ; class Y X dataMember ; ;/ 聲明一個類類型數(shù)據(jù)成員聲明一個類類型數(shù)據(jù)成員華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.1 6.1.1 定義類和對象定義類和對象 類說明的一般形式為:類說明的一般形式為: class 類名類名 public:公有段數(shù)據(jù)成員和成員函數(shù)公有段數(shù)據(jù)成員和成員函數(shù)
31、 ; protected: 保護段數(shù)據(jù)成員和成員函數(shù)保護段數(shù)據(jù)成員和成員函數(shù) ; private:私有數(shù)據(jù)成員和成員函數(shù)私有數(shù)據(jù)成員和成員函數(shù) ; ; 1. 允許已定義類名出現(xiàn)在類的說明中允許已定義類名出現(xiàn)在類的說明中例:例: class dataMember ; ;/ 錯誤錯誤錯誤錯誤無窮遞歸結構無窮遞歸結構華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.1 6.1.1 定義類和對象定義類和對象 類說明的一般形式為:類說明的一般形式為: class 類名類名 public:公有段數(shù)據(jù)成員和成員函數(shù)公有段數(shù)據(jù)成員和成員函數(shù) ; protected: 保護
32、段數(shù)據(jù)成員和成員函數(shù)保護段數(shù)據(jù)成員和成員函數(shù) ; private:私有數(shù)據(jù)成員和成員函數(shù)私有數(shù)據(jù)成員和成員函數(shù) ; ; 1. 允許已定義類名出現(xiàn)在類的說明中允許已定義類名出現(xiàn)在類的說明中2. 類可以無名,用于直接聲明對象類可以無名,用于直接聲明對象例:例: class mydate ;/ 直接直接聲明一個對象聲明一個對象華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.1 6.1.1 定義類和對象定義類和對象 類說明的一般形式為:類說明的一般形式為: class 類名類名 public:公有段數(shù)據(jù)成員和成員函數(shù)公有段數(shù)據(jù)成員和成員函數(shù) ; protecte
33、d: 保護段數(shù)據(jù)成員和成員函數(shù)保護段數(shù)據(jù)成員和成員函數(shù) ; private:私有數(shù)據(jù)成員和成員函數(shù)私有數(shù)據(jù)成員和成員函數(shù) ; ; 1. 允許已定義類名出現(xiàn)在類的說明中允許已定義類名出現(xiàn)在類的說明中2. 類可以無名,用于直接聲明對象類可以無名,用于直接聲明對象/例例6-2#includeusing namespace std ;class empty ;int main() empty e1 ; cout &e1 = &e1 endl ; cout sizeof e1 = sizeof(e1) endl ; 3. 類是一個程序包。可以只有數(shù)據(jù)成員或只有成員函數(shù),或者為空。類是一個
34、程序包??梢灾挥袛?shù)據(jù)成員或只有成員函數(shù),或者為空。 一個空類一個空類華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.1 6.1.1 定義類和對象定義類和對象 類說明的一般形式為:類說明的一般形式為: class 類名類名 public:公有段數(shù)據(jù)成員和成員函數(shù)公有段數(shù)據(jù)成員和成員函數(shù) ; protected: 保護段數(shù)據(jù)成員和成員函數(shù)保護段數(shù)據(jù)成員和成員函數(shù) ; private:私有數(shù)據(jù)成員和成員函數(shù)私有數(shù)據(jù)成員和成員函數(shù) ; ; 1. 允許已定義類名出現(xiàn)在類的說明中允許已定義類名出現(xiàn)在類的說明中2. 類可以無名,用于直接聲明對象類可以無名,用于直接聲明
35、對象3. 類是一個程序包。可以只有數(shù)據(jù)成員或只有成員函數(shù),或者為空。類是一個程序包??梢灾挥袛?shù)據(jù)成員或只有成員函數(shù),或者為空。 /例例6-2#includeusing namespace std ;class empty ;int main() empty e1 ; cout &e1 = &e1 endl ; cout sizeof e1 = sizeof(e1) endl ; 空類對象空類對象華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.1 6.1.1 定義類和對象定義類和對象 類說明的一般形式為:類說明的一般形式為: class 類
36、名類名 public:公有段數(shù)據(jù)成員和成員函數(shù)公有段數(shù)據(jù)成員和成員函數(shù) ; protected: 保護段數(shù)據(jù)成員和成員函數(shù)保護段數(shù)據(jù)成員和成員函數(shù) ; private:私有數(shù)據(jù)成員和成員函數(shù)私有數(shù)據(jù)成員和成員函數(shù) ; ; 1. 允許已定義類名出現(xiàn)在類的說明中允許已定義類名出現(xiàn)在類的說明中2. 類可以無名,用于直接聲明對象類可以無名,用于直接聲明對象/例例6-2#includeusing namespace std ;class empty ;int main() empty e1 ; cout &e1 = &e1 endl ; cout sizeof e1 = sizeof(e
37、1) ” 運算符訪問對象成員運算符訪問對象成員 w 6.1.2 6.1.2 訪問對象成員訪問對象成員 /例例6-3 訪問對象的公有成員訪問對象的公有成員 #includeusing namespace std ;class Tclass public: int x,y ; void print() cout x “,” ” 運算符訪問對象成員運算符訪問對象成員 w 6.1.2 6.1.2 訪問對象成員訪問對象成員 /例例6-3 訪問對象的公有成員訪問對象的公有成員#includeusing namespace std ;class Tclass: int x,y ; void print()
38、cout x “,” ” 運算符訪問對象成員運算符訪問對象成員 w 6.1.2 6.1.2 訪問對象成員訪問對象成員 /例例6-3 訪問對象的公有成員訪問對象的公有成員#includeusing namespace std ;class Tclass: int x,y ; void print() cout x “,” ” 運算符訪問對象成員運算符訪問對象成員 w 6.1.2 6.1.2 訪問對象成員訪問對象成員 /例例6-4 用指針訪問對象成員用指針訪問對象成員 #includeusing namespace std ;class Tclass public : int x, y ; voi
39、d print() cout x , y x + ptf-y ) ; int main() Tclass test, * pt = new(Tclass) ; pt-x = 100 ; pt-y = 200 ; pt-print() ; test.x = 150 ; test.y = 450 ; test.print() ; cout x+y= ” 運算符訪問對象成員運算符訪問對象成員 w 6.1.2 6.1.2 訪問對象成員訪問對象成員 /例例6-4 用指針訪問對象成員用指針訪問對象成員 #includeusing namespace std ;class Tclass public : i
40、nt x, y ; void print() cout x , y x + ptf-y ) ; int main() Tclass test, * pt = ; pt-x = 100 ; pt-y = 200 ; pt-print() ; test.x = 150 ; test.y = 450 ; test.print() ; cout x+y= ” 運算符訪問對象成員運算符訪問對象成員 w 6.1.2 6.1.2 訪問對象成員訪問對象成員 /例例6-4 用指針訪問對象成員用指針訪問對象成員 #includeusing namespace std ;class Tclass public :
41、int x, y ; void print() cout x , y x + ptf-y ) ; int main() Tclass test, * pt = new(Tclass) ; = 100 ; = 200 ; ; test.x = 150 ; test.y = 450 ; test.print() ; cout x+y= ” 運算符訪問對象成員運算符訪問對象成員 w 6.1.2 6.1.2 訪問對象成員訪問對象成員 /例例6-4 用指針訪問對象成員用指針訪問對象成員 #includeusing namespace std ;class Tclass public : int x, y
42、 ; void print() cout x , y x + ptf-y ) ; int main() Tclass test, * pt = new(Tclass) ; pt-x = 100 ; pt-y = 200 ; pt-print() ; test.x = 150 ; test.y = 450 ; test.print() ; cout x+y= ” 運算符訪問對象成員運算符訪問對象成員 w 6.1.2 6.1.2 訪問對象成員訪問對象成員 /例例6-4 用指針訪問對象成員用指針訪問對象成員 #includeusing namespace std ;class Tclass publ
43、ic : int x, y ; void print() cout x , y x + ptf-y ) ; int main() Tclass test, * pt = new(Tclass) ; pt-x = 100 ; pt-y = 200 ; pt-print() ; test.x = 150 ; test.y = 450 ; test.print() ; cout x+y= add() ;向函數(shù)向函數(shù)傳遞對象地址傳遞對象地址 華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.3 this6.1.3 this指針指針 #include /例例6-5u
44、sing namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printX
45、Y () ;華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.3 this6.1.3 this指針指針 #include /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY ()
46、; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;obj1.xobj1.yobj2.xobj2.yobj3.xobj3.y華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.3 this6.1.3 this指針指針 #include /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y
47、= b ; void printXY() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;obj1.xobj1.yobj2.xobj2.yobj3.xobj3.y華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.3 th
48、is6.1.3 this指針指針 #include /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 .
49、 setXY ( 30, 35 ) ; obj3 . printXY () ;obj1.xobj1.yobj2.xobj2.yobj3.xobj3.y向哪個對象向哪個對象的數(shù)據(jù)成員賦值?的數(shù)據(jù)成員賦值? 華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.3 this6.1.3 this指針指針 #include /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x
50、 , y x = a ; this-y = b ; obj1 . setXY ( 10, 15, &obj1 ) ;成員函數(shù)隱含定義成員函數(shù)隱含定義 this 指針指針接受調用對象的地址接受調用對象的地址華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.3 this6.1.3 this指針指針 #include /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() co
51、ut x , y x = a ; this-y = b ; this 指針不能顯式聲明指針不能顯式聲明華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.3 this6.1.3 this指針指針 #include /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y x = a ; this-y = b ; this 指針不能顯式聲明指針不能顯式聲明可以顯式
52、使用可以顯式使用obj1 . setXY ( 10, 15, &obj1 ) ;華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.3 this6.1.3 this指針指針 #include /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 .
53、 setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;obj1.xobj1.yobj2.xobj2.yobj3.xobj3.y通過調用函數(shù)的對象通過調用函數(shù)的對象this 指針獲取對象地址指針獲取對象地址華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.3 this6.1.3 this指針指針 #include /例例6-5using namespac
54、e std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;1015obj
55、1.xobj1.yobj2.xobj2.yobj3.xobj3.y華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.3 this6.1.3 this指針指針 #include /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10
56、, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;1015obj1.xobj1.yobj2.xobj2.yobj3.xobj3.y華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.3 this6.1.3 this指針指針 #include /例例6-5using namespace std ;class Simple int x, y ; public : void s
57、etXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;1015obj1.xobj1.yobj2.xobj2.yobj3.xobj3.y在在 obj1 上操作上操
58、作 10 , 15 20 , 25 30 , 35華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.3 this6.1.3 this指針指針 #include /例例6-5using namespace std ;class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ;
59、 obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;1015obj1.xobj1.yobj2.xobj2.yobj3.xobj3.y 10 , 15 20 , 25 30 , 35華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.3 this6.1.3 this指針指針 #include /例例6-5using namespace std ;class Simple int x, y ;
60、 public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ;int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ;1015obj1.xobj1.yobj2.xobj2.yobj3.xobj3.y 10 , 15 20 , 25 30 , 35在在 obj2 上操作上操作華南理工大學計算機學院華南理工大學計算機學院 周靄如周靄如 20122012w 6.1.3 this6.
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 【正版授權】 ISO 9335:2025 EN Optics and photonics - Optical transfer function - Principles and procedures of measurement
- 【正版授權】 ISO/IEC 27562:2024 EN Information technology - Security techniques - Privacy guidelines for fintech services
- 【正版授權】 ISO 21068-3:2024 EN Chemical analysis of raw materials and refractory products containing silicon-carbide,silicon-nitride,silicon-oxynitride and sialon - Part 3: Determina
- 2025年度數(shù)據(jù)中心電路改造及智能監(jiān)控服務協(xié)議
- 2025年度金融機構間同業(yè)拆借合同模板
- 2025年度辦公場地租賃及物業(yè)管理合同范本
- 2025年度城市綠化工程項目承包合同范本
- 2025年度城市燃氣管道安裝工程總承包合同范本
- 2025年度餐飲店鋪裝修設計與施工合同
- 2025年度戀愛雙方戀愛期間責任劃分合同模板
- 漸變方格模板
- 潛水作業(yè)指導書
- 感謝對手閱讀附答案
- 環(huán)境因素匯總識別及評價表(保衛(wèi)部 )
- GB/T 8012-2000鑄造錫鉛焊料
- 第一課 第一章 AutoCAD 2012概述入門
- GB/T 33880-2017熱等靜壓鋁硅合金板材
- 消防安全風險辨識清單
- 超市店長考核方案(實例)
- 2023年版勞動實踐河北科學技術出版社一年級下冊全冊教案
- 方案報審表(樣表)
評論
0/150
提交評論