




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、第2部分 類與數(shù)據(jù)抽象 2.1 類的定義和對象的創(chuàng)建2.2 構(gòu)造函數(shù)與析構(gòu)函數(shù)2.3 this, const和new, delete2.4 復(fù)制構(gòu)造函數(shù)2.5 轉(zhuǎn)換構(gòu)造函數(shù)與類型轉(zhuǎn)換2.6 對象數(shù)組、對象指針與對象引用2.7 靜態(tài)成員與友元2.8 從面向過程到面向?qū)ο?面向?qū)ο缶幊痰某绦蚧締挝皇穷?類是數(shù)據(jù)和操作數(shù)據(jù)的函數(shù)的封裝 類的對象使用自己的方法完成對數(shù)據(jù)的操作 類可以隱藏數(shù)據(jù)和操作細節(jié),對象通過類接口與外部通信 2.1 類與對象 2.2 類與對象/ 排序函數(shù)原型void Sort (int , int ) ; / 數(shù)組相加函數(shù)原型void Add ( int , int , int
2、) ; void main() int a 10 , b 10 ; . Sort ( a , 10 ) ; Sort ( b , 10 ) ; Add ( a , b , 10) ; .數(shù)組與數(shù)組類封裝class 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) ; void main() Array a(10) ,
3、b(10) ;/ 聲明對象 . a . Sort() ; b . Sort() ;/ 調(diào)用排序方法 a = a + b ;/ 數(shù)組相加 ./ 排序函數(shù)原型void Sort (int , int ) ; / 數(shù)組相加函數(shù)原型void Add ( int , int , int ) ; void main() int a 10 , b 10 ; . Sort ( a , 10 ) ; Sort ( b , 10 ) ; Add ( a , b , 10) ; .數(shù)組與數(shù)組類封裝2.2 類與對象class Array/定義數(shù)組類 int *ap ; int len ; public: Array(
4、 int size )/ 建立數(shù)組 len= size ; ap = new int size ; void Sort ( ) ;/ 排序 / 重載算符 +函數(shù) Array operaor + (const Array & other) ; void main() Array a(10) , b(10) ;/ 聲明對象 . a . Sort() ; b . Sort() ;/ 調(diào)用排序方法 a = a + b ;/ 數(shù)組相加 ./ 排序函數(shù)原型void Sort (int , int ) ; / 數(shù)組相加函數(shù)原型void Add ( int , int , int ) ; void main(
5、) int a 10 , b 10 ; . Sort ( a , 10 ) ; Sort ( b , 10 ) ; Add ( a , b , 10) ; .數(shù)組與數(shù)組類class 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) ;類是數(shù)據(jù)和操作數(shù)據(jù)的函數(shù)的封裝封裝2.2 類與對象class Array/定義數(shù)組類
6、int *ap ; int len ; public: Array( int size )/ 建立數(shù)組 len= size ; ap = new int size ; void Sort ( ) ;/ 排序 / 重載算符 +函數(shù) Array operaor + (const Array & other) ; void main() Array a(10) , b(10) ;/ 聲明對象 . a . Sort() ; b . Sort() ;/ 調(diào)用排序方法 a = a + b ;/ 數(shù)組相加 ./ 排序函數(shù)原型void Sort (int , int ) ; / 數(shù)組相加函數(shù)原型void Ad
7、d ( int , int , int ) ; void main() int a 10 , b 10 ; . Sort ( a , 10 ) ; Sort ( b , 10 ) ; Add ( a , b , 10) ; .數(shù)組與數(shù)組類對象使用自己的方法對數(shù)據(jù)操作a . Sort() ; b . Sort() ; / 調(diào)用排序方法a = a + b ; / 數(shù)組相加封裝2.2 類與對象幾點如何調(diào)整通信2.2 類與對象構(gòu)造class 鐘 private : 鐘的構(gòu)造; public : 讀取時間值 ; 調(diào)整時間值 ;對象通過類接口與外部通信通信2.2 類與對象2.1 定義類和對象在C+中,類是
8、一種數(shù)據(jù)類型。 客觀事物是復(fù)雜的,要描述它必須從多方面進行,也就是用不同的數(shù)據(jù)類型來描述不同的方面。如每天可以這樣描述: 年份(用整型數(shù)描述) 月份(用整型數(shù)描述) 日期(用整型數(shù)描述)2.1 定義類和對象結(jié)構(gòu)體描述:Structure Tday int year ; int month ; int day ; mybirthday類描述:class goods private: int year ; int month ; int day ;.2.1 定義類和對象 / 例 一個類的例子#includeclass Tdate public: void Set(int m, int d, int
9、 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;void main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ;2.1 定義類和對象 / 例 一個類的例子#includeclass Tdate public: voi
10、d 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;void main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ;class Tdate public: void Set(
11、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;2.1 定義類和對象 / 例 一個類的例子#includeclass Tdate public: void Set(int m, int d, int y ) month = m ;
12、 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;void main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ;class Tdate public: void Set(int m, int d, int y ) month = m ; day =
13、 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;關(guān)鍵字定義一個類標識符類名 class ,struct,union 都可以定義一個類: class缺省說明時,其成員被認為是私有的 struct若不特別指出,其所有成員都是公有的 union其所有成員都是公有的,且不能更改2.1 定義類和對象 / 例 一個類的
14、例子#includeclass 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;void main() Tdate a ; a.Set (10, 16, 2003) ; a.Print
15、() ;aTdate 類型的一個對象(實例)2.1 定義類和對象 / 例 一個類的例子#includeclass 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;void main()
16、 Tdate a ; a.Set (10, 16, 2003) ; a.Print() ; 類由成員構(gòu)成: 數(shù)據(jù)成員描述對象的屬性 成員函數(shù)描述對象的方法2.1 定義類和對象 / 例 一個類的例子#includeclass 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 e
17、ndl ; private: int month; int day; int year;void main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ;int month; int day; int year;數(shù)據(jù)成員2.1 定義類和對象 / 例 一個類的例子#includeclass 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 )
18、|( year%400 = 0); void Print() cout year . month . day endl ; private: int month; int day; int year;void main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ; 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 Pr
19、int() cout year . month . day endl ; 類中定義成員函數(shù)內(nèi)聯(lián)函數(shù)處理2.1 定義類和對象 / 例 一個類的例子#includeclass 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;
20、int day; int year;void main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ; void Set(int m, int d, int y ) ; int IsLeapYear() ; void Print() ;在類外定義成員函數(shù) void Tdate : Set(int m, int d, int y ) month = m ; day = d ; year = y ; int Tdate : IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0
21、); void Tdate : Print() cout year.month.dayendl ; 2.1 定義類和對象 / 例 一個類的例子#includeclass 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; i
22、nt day; int year;void main() Tdate a ; a.Set (10, 16, 2003) ; a.Print() ;成員的性質(zhì)由關(guān)鍵字public、protected、private 決定public 公有 公有段的成員是提供給外部的接口protected 保護 保護段成員在該類和它的派生類中可見private 私有 私有段成員僅在類中可見各段中既可以包含數(shù)據(jù)成員,也可以包含成員函數(shù)2.1 定義類和對象 / 例 一個類的例子#includeclass Tdate public: void Set(int m, int d, int y ) month = m ;
23、day = d ; year = y ; int IsLeapYear() return ( year%4 = 0 & year%100 != 0 )|( year%400 = 0); void Print() cout year . month . day ” 運算符訪問對象成員 訪問對象成員 /訪問對象的公有成員 #includeclass Tclass public: int x,y ; void print() cout x “,” ” 運算符訪問對象成員 訪問對象成員 /訪問對象的公有成員 #includeclass Tclass public: int x,y ; void pri
24、nt() cout x “,” ” 運算符訪問對象成員 訪問對象成員 /訪問對象的公有成員 #includeclass Tclass public: int x,y ; void print() cout x “,” ” 運算符訪問對象成員 訪問對象成員 / 用指針訪問對象成員 #includeclass Tclass public : int x, y ; void print() cout x , y x + ptf-y ) ; void main() Tclass test, * pt = new(Tclass) ; pt-x = 100 ; pt-y = 200 ; pt-print(
25、) ; test.x = 120 ; test.y = 420 ; test.print() ; cout x+y= ” 運算符訪問對象成員 訪問對象成員 / 用指針訪問對象成員 #includeclass Tclass public : int x, y ; void print() cout x , y x + ptf-y ) ; void main() Tclass test, * pt = new(Tclass) ; pt-x = 100 ; pt-y = 200 ; pt-print() ; test.x = 120 ; test.y = 420 ; test.print() ; c
26、out x+y= ” 運算符訪問對象成員 訪問對象成員 / 用指針訪問對象成員 #includeclass Tclass public : int x, y ; void print() cout x , y x + ptf-y ) ; void main() Tclass test, * pt = new(Tclass) ; pt-x = 100 ; pt-y = 200 ; pt-print() ; test.x = 120 ; test.y = 420 ; test.print() ; cout x+y= ” 運算符訪問對象成員 訪問對象成員 / 用指針訪問對象成員 #includecl
27、ass Tclass public : int x, y ; void print() cout x , y x + ptf-y ) ; void main() Tclass test, * pt = new(Tclass) ; pt-x = 100 ; pt-y = 200 ; pt-print() ; test.x = 120 ; test.y = 420 ; test.print() ; cout x+y= ” 運算符訪問對象成員 訪問對象成員 / 用指針訪問對象成員 #includeclass Tclass public : int x, y ; void print() cout x
28、 , y x + ptf-y ) ; void main() Tclass test, * pt = new(Tclass) ; pt-x = 100 ; pt-y = 200 ; pt-print() ; test.x = 120 ; test.y = 420 ; test.print() ; cout x+y= add(&test) ;向函數(shù)傳遞對象地址 2.2 構(gòu)造函數(shù)和析構(gòu)函數(shù) C+ 為對象的初始化提供了必要的機制,讓用戶可以自己編寫初始化程序,這就是構(gòu)造函數(shù)。 構(gòu)造函數(shù)是用于創(chuàng)建對象的特殊成員函數(shù) 當創(chuàng)建對象時,系統(tǒng)自動調(diào)用構(gòu)造函數(shù) 構(gòu)造函數(shù)的作用是: 為對象分配空間;對數(shù)據(jù)成員賦初
29、值;請求其他資源 沒有用戶定義的構(gòu)造函數(shù)時,系統(tǒng)提供缺省版本的構(gòu)造函數(shù) 構(gòu)造函數(shù)名與類名相同 構(gòu)造函數(shù)一般被聲明為公有函數(shù),可以重載。 構(gòu)造函數(shù)可以有任意類型的參數(shù),但沒有返回類型,也不能標記為void類型。2.2 構(gòu)造函數(shù)和析構(gòu)函數(shù)2.2 構(gòu)造函數(shù)和析構(gòu)函數(shù) 析構(gòu)函數(shù)是用于取消對象的成員函數(shù) 當一個對象作用域結(jié)束時,系統(tǒng)自動調(diào)用析構(gòu)函數(shù)來釋放存儲空間。 析構(gòu)函數(shù)名為: 類名 沒有用戶定義析構(gòu)函數(shù)時,系統(tǒng)提供缺省版本的析構(gòu)函數(shù) 析構(gòu)函數(shù)的作用是進行清除對象,釋放內(nèi)存等操作 析構(gòu)函數(shù)沒有參數(shù),也沒有返回類型2.3 構(gòu)造函數(shù)和析構(gòu)函數(shù)例: class AA AA() AA() ; void tes
30、t() int x , y ; AA t1, t2 ; return ; 2.2 構(gòu)造函數(shù)和析構(gòu)函數(shù) 2.3 構(gòu)造函數(shù)和析構(gòu)函數(shù)例: class AA AA() AA() ; void test() int x , y ; AA t1, t2 ; return ; 2.2 構(gòu)造函數(shù)和析構(gòu)函數(shù) 構(gòu)造函數(shù)2.3 構(gòu)造函數(shù)和析構(gòu)函數(shù)例: class AA AA() AA( ) ; void test() int x , y ; AA t1, t2 ; return ; 2.2 構(gòu)造函數(shù)和析構(gòu)函數(shù) 析構(gòu)函數(shù)2.3 構(gòu)造函數(shù)和析構(gòu)函數(shù)例: class AA AA() AA() ; void test()
31、 int x , y ; AA t1, t2 ; return ; 2.2 構(gòu)造函數(shù)和析構(gòu)函數(shù) / 聲明變量時開辟兩個整型存儲空間2.3 構(gòu)造函數(shù)和析構(gòu)函數(shù)例: class AA AA() AA() ; void test() int x , y ; AA t1, t2 ; return ; 2.2 構(gòu)造函數(shù)和析構(gòu)函數(shù) / 兩次調(diào)用構(gòu)造函數(shù),創(chuàng)建對象2.3 構(gòu)造函數(shù)和析構(gòu)函數(shù)例: class AA AA() AA() ; void test() int x , y ; AA t1, t2 ; return ; 2.2 構(gòu)造函數(shù)和析構(gòu)函數(shù) / 兩次調(diào)用構(gòu)造函數(shù),創(chuàng)建對象2.3 構(gòu)造函數(shù)和析構(gòu)函數(shù)
32、例: class AA AA() AA() ; void test() int x , y ; AA t1, t2 ; return ; 2.2 構(gòu)造函數(shù)和析構(gòu)函數(shù) /兩次調(diào)用析構(gòu)函數(shù),撤消對象2.3 構(gòu)造函數(shù)和析構(gòu)函數(shù)例: class AA AA() AA() ; void test() int x , y ; AA t1, t2 ; return ; 2.2 構(gòu)造函數(shù)和析構(gòu)函數(shù) /兩次調(diào)用析構(gòu)函數(shù),撤消對象2.3 構(gòu)造函數(shù)和析構(gòu)函數(shù)例: class AA AA() AA() ; void test() int x , y ; AA t1, t2 ; return ; 2.2 構(gòu)造函數(shù)和析構(gòu)
33、函數(shù) /兩次調(diào)用析構(gòu)函數(shù),撤消對象/ 變量 x, y 生存期結(jié)束2.3 構(gòu)造函數(shù)和析構(gòu)函數(shù)簡單構(gòu)造函數(shù)和析構(gòu)函數(shù) 構(gòu)造函數(shù)原型類名 : 類名 ( 參數(shù)表 );析構(gòu)函數(shù)原型 類名 : 類名(); 2.3.1 簡單構(gòu)造函數(shù)和析構(gòu)函數(shù)#include/ 例2-11class Date public: Date() ; Date() ; void SetDate( int y, int m, int d ) ; int IsLeapYear() ; void PrintDate() ; private: int year, month, day ; ;Date: Date() cout Date ob
34、ject initialized.n ; / 無參構(gòu)造函數(shù)Date: Date() cout Date object destroyed.n ; / 析構(gòu)函數(shù)void Date: SetDate( int y, int m, int d ) year = y ; month = m ; day = d ; int Date:IsLeapYear() return ( year%4=0 & year%100!=0 ) | ( year%400=0 ) ; void Date:PrintDate() cout year / month / day endl ; void main() Date d
35、 ; d.SetDate(2001,10,1) ; d.PrintDate() ; 2.3.1 簡單構(gòu)造函數(shù)和析構(gòu)函數(shù)#include / 例2-6class Date public: Date() ; Date() ; void SetDate( int y, int m, int d ) ; int IsLeapYear() ; void PrintDate() ; private: int year, month, day ; ;Date: Date() cout Date object initialized.n ; / 無參構(gòu)造函數(shù)Date: Date() cout Date obj
36、ect destroyed.n ; / 析構(gòu)函數(shù)void Date: SetDate( int y, int m, int d ) year = y ; month = m ; day = d ; int Date:IsLeapYear() return ( year%4=0 & year%100!=0 ) | ( year%400=0 ) ; void Date:PrintDate() cout year / month / day endl ; void main() Date d ; d.SetDate(2001,10,1) ; d.PrintDate() ; Date() ;Date:
37、 Date() cout Date object initialized.n ; / 無參構(gòu)造函數(shù)Date d ;創(chuàng)建對象時調(diào)用構(gòu)造函數(shù)2.3.1 簡單構(gòu)造函數(shù)和析構(gòu)函數(shù)#include / 例2-6class Date public: Date() ; Date() ; void SetDate( int y, int m, int d ) ; int IsLeapYear() ; void PrintDate() ; private: int year, month, day ; ;Date: Date() cout Date object initialized.n ; / 無參構(gòu)造函數(shù)
38、Date: Date() cout Date object destroyed.n ; / 析構(gòu)函數(shù)void Date: SetDate( int y, int m, int d ) year = y ; month = m ; day = d ; int Date:IsLeapYear() return ( year%4=0 & year%100!=0 ) | ( year%400=0 ) ; void Date:PrintDate() cout year / month / day endl ; void main() Date d ; d.SetDate(2001,10,1) ; d.P
39、rintDate() ; Date() ;Date: Date() cout Date object destroyed.n ; / 析構(gòu)函數(shù)作用域結(jié)束時調(diào)用析構(gòu)函數(shù)2.3.1 簡單構(gòu)造函數(shù)和析構(gòu)函數(shù)#include / 例2-6class Date public: Date() ; Date() ; void SetDate( int y, int m, int d ) ; int IsLeapYear() ; void PrintDate() ; private: int year, month, day ; ;Date: Date() cout Date object initializ
40、ed.n ; / 無參構(gòu)造函數(shù)Date: Date() cout Date object destroyed.n ; / 析構(gòu)函數(shù)void Date: SetDate( int y, int m, int d ) year = y ; month = m ; day = d ; int Date:IsLeapYear() return ( year%4=0 & year%100!=0 ) | ( year%400=0 ) ; void Date:PrintDate() cout year / month / day endl ; void main() Date d ; d.SetDate(2
41、001,10,1) ; d.PrintDate() ;2.3.1 簡單構(gòu)造函數(shù)和析構(gòu)函數(shù)#include / 例2-6class Date public: Date() ; Date() ; void SetDate( int y, int m, int d ) ; int IsLeapYear() ; void PrintDate() ; private: int year, month, day ; ;Date: Date() cout Date object initialized.n ; / 無參構(gòu)造函數(shù)Date: Date() cout Date object destroyed.n
42、 ; / 析構(gòu)函數(shù)void Date: SetDate( int y, int m, int d ) year = y ; month = m ; day = d ; int Date:IsLeapYear() return ( year%4=0 & year%100!=0 ) | ( year%400=0 ) ; void Date:PrintDate() cout year / month / day SetDate(2001,10,1) ; pd-PrintDate() ; delete pd ; 2.3.1 簡單構(gòu)造函數(shù)和析構(gòu)函數(shù)#include / 例2-6class Date pu
43、blic: Date() ; Date() ; void SetDate( int y, int m, int d ) ; int IsLeapYear() ; void PrintDate() ; private: int year, month, day ; ;Date: Date() cout Date object initialized.n ; / 無參構(gòu)造函數(shù)Date: Date() cout Date object destroyed.n ; / 析構(gòu)函數(shù)void Date: SetDate( int y, int m, int d ) year = y ; month = m
44、; day = d ; int Date:IsLeapYear() return ( year%4=0 & year%100!=0 ) | ( year%400=0 ) ; void Date:PrintDate() cout year / month / day SetDate(2001,10,1) ; pd-PrintDate() ; delete pd ; Date *pd = new ( Date );Date() ;Date: Date() cout Date object initialized.n ; / 無參構(gòu)造函數(shù)創(chuàng)建動態(tài)對象時調(diào)用構(gòu)造函數(shù)2.3.1 簡單構(gòu)造函數(shù)和析構(gòu)函數(shù)#
45、include / 例2-6class Date public: Date() ; Date() ; void SetDate( int y, int m, int d ) ; int IsLeapYear() ; void PrintDate() ; private: int year, month, day ; ;Date: Date() cout Date object initialized.n ; / 無參構(gòu)造函數(shù)Date: Date() cout Date object destroyed.n ; / 析構(gòu)函數(shù)void Date: SetDate( int y, int m, in
46、t d ) year = y ; month = m ; day = d ; int Date:IsLeapYear() return ( year%4=0 & year%100!=0 ) | ( year%400=0 ) ; void Date:PrintDate() cout year / month / day SetDate(2001,10,1) ; pd-PrintDate() ; delete pd ; delete pd ;Date() ;Date: Date() cout Date object destroyed.n ; / 析構(gòu)函數(shù)釋放動態(tài)對象時調(diào)用析構(gòu)函數(shù)2.3.1 簡單
47、構(gòu)造函數(shù)和析構(gòu)函數(shù) 帶參數(shù)的構(gòu)造函數(shù)在建立對象時,以特定的數(shù)據(jù)初始化 對象的數(shù)據(jù)成員帶參數(shù)的構(gòu)造函數(shù) 2.3.2 帶參數(shù)的構(gòu)造函數(shù)#include / 例2-7class Date public: Date(int,int,int) ; Date() ; void SetDate( int y, int m, int d ) ; void IsLeapYear() ; void PrintDate() ; private: int year, month, day ; ;Date: Date(int y, int m, int d) year = y ; month = m ; day = d
48、 ; cout year / month / day : Date object initialized. n ; Date: Date() cout year / month / day : Date object destroyed. n ; void Date: SetDate( int y, int m, int d ) year = y ; month = m ; day = d ; void Date:IsLeapYear() if ( year%4 = 0 & year%100 != 0 | year%400 = 0 )cout year Is leap year.n ; els
49、e cout Is not leap year.n ; void Date:PrintDate() cout year / month / day endl ; void main() Date d1( 2000, 2, 1 ) ; d1.PrintDate() ; d1.IsLeapYear() ;2.3.2 帶參數(shù)的構(gòu)造函數(shù)#include / 例2-7class Date public: Date(int,int,int) ; Date() ; void SetDate( int y, int m, int d ) ; void IsLeapYear() ; void PrintDate
50、() ; private: int year, month, day ; ;Date: Date(int y, int m, int d) year = y ; month = m ; day = d ; cout year / month / day : Date object initialized. n ; Date: Date() cout year / month / day : Date object destroyed. n ; void Date: SetDate( int y, int m, int d ) year = y ; month = m ; day = d ; v
51、oid Date:IsLeapYear() if ( year%4 = 0 & year%100 != 0 | year%400 = 0 )cout year Is leap year.n ; else cout Is not leap year.n ; void Date:PrintDate() cout year / month / day endl ; void main() Date d1( 2000, 2, 1 ) ; d1.PrintDate() ; d1.IsLeapYear() ;Date(int,int,int) ;Date: Date(int y, int m, int d
52、)/ 帶參數(shù)構(gòu)造函數(shù) year = y ; month = m ; day = d ; cout year /month/day: Date object initialized.n ; 2.3.2 帶參數(shù)的構(gòu)造函數(shù)#include / 例2-7class Date public: Date(int,int,int) ; Date() ; void SetDate( int y, int m, int d ) ; void IsLeapYear() ; void PrintDate() ; private: int year, month, day ; ;Date: Date(int y, i
53、nt m, int d) year = y ; month = m ; day = d ; cout year / month / day : Date object initialized. n ; Date: Date() cout year / month / day : Date object destroyed. n ; void Date: SetDate( int y, int m, int d ) year = y ; month = m ; day = d ; void Date:IsLeapYear() if ( year%4 = 0 & year%100 != 0 | y
54、ear%400 = 0 )cout year Is leap year.n ; else cout Is not leap year.n ; void Date:PrintDate() cout year / month / day endl ; void main() Date d1( 2000, 2, 1 ) ; d1.PrintDate() ; d1.IsLeapYear() ;Date(int,int,int) ;Date: Date(int y, int m, int d)/ 帶參數(shù)構(gòu)造函數(shù) year = y ; month = m ; day = d ; cout year /mo
55、nth/day: Date object initialized.n ; Date: Date(int y, int m, int d) : year(y), month(m), day(d) coutyear/month/day: Date object initialized.n ; 用初始式對數(shù)據(jù)成員置值2.3.2 帶參數(shù)的構(gòu)造函數(shù)Date: Date(int y, int m, int d) : year(y), month(m), day(d) coutyear/month/day: Date object initialized.n ; 使用“初始式”的構(gòu)造函數(shù)形式為:構(gòu)造函數(shù)名
56、(變元表): 數(shù)據(jù)成員1 (變元表), , 數(shù)據(jù)成員n (變元表) /* */ “初始式”可以調(diào)用類類型成員或基類構(gòu)造函數(shù)2.3.2 帶參數(shù)的構(gòu)造函數(shù)/例2-13 對象成員初始化 #includeclass A public : A ( int x ) : a ( x ) int a ; ;class B public : B( int x, int y ) : aa( x ), b( y ) void out() cout aa = aa.a endl b = b endl ; private : int b ; A aa ; ;void main () B objB ( 3, 2 ) ;
57、objB . out ( ) ; 類類型數(shù)據(jù)成員類的復(fù)合 /例2-13 對象成員初始化#includeclass A public : A ( int x ) : a ( x ) int a ; ;class B public : B( int x, int y ) : aa( x ), b( y ) void out() cout aa = aa.a endl b = b endl ; private : int b ; A aa ; ;void main () B objB ( 3, 2 ) ; objB . out ( ) ; 類的復(fù)合/例2-13 對象成員初始化#includeclas
58、s A public : A ( int x ) : a ( x ) int a ; ;class B public : B( int x, int y ) : aa( x ), b( y ) void out() cout aa = aa.a endl b = b endl ; private : int b ; A aa ; ;void main () B objB ( 3, 2 ) ; objB . out ( ) ; 首先調(diào)用成員類構(gòu)造函數(shù)2.3.2 帶參數(shù)的構(gòu)造函數(shù)類的復(fù)合/例2-13 對象成員初始化#includeclass A public : A ( int x ) : a (
59、 x ) int a ; ;class B public : B( int x, int y ) : aa( x ), b( y ) void out() cout aa = aa.a endl b = b endl ; private : int b ; A aa ; ;void main () B objB ( 3, 2 ) ; objB . out ( ) ; 3objB.aa.a類的復(fù)合3objB.aa.a/例2-13 對象成員初始化#includeclass A public : A ( int x ) : a ( x ) int a ; ;class B public : B( i
60、nt x, int y ) : aa( x ), b( y ) void out() cout aa = aa.a endl b = b endl ; private : int b ; A aa ; ;void main () B objB ( 3, 2 ) ; objB . out ( ) ; 2objB.b再初始化自身數(shù)據(jù)成員類的復(fù)合3objB.aa.a/例2-13 對象成員初始化#includeclass A public : A ( int x ) : a ( x ) int a ; ;class B public : B( int x, int y ) : aa( x ), b(
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度汽車維修行業(yè)人才引進與培養(yǎng)合同
- 2025年度環(huán)衛(wèi)工人勞動爭議調(diào)解與處理合同
- 二零二五年度農(nóng)村宅基地租賃協(xié)議(農(nóng)村文化產(chǎn)業(yè)發(fā)展)
- 2025年度高級建造師聘用與技術(shù)咨詢服務(wù)協(xié)議
- 二零二五年度商業(yè)企業(yè)購銷合同印花稅稅率調(diào)整與稅收籌劃實務(wù)
- 二零二五年度藝人經(jīng)紀與全產(chǎn)業(yè)鏈合作合同
- IT基礎(chǔ)設(shè)施建設(shè)項目投資合同
- 鄉(xiāng)村旅游資源開發(fā)利用合作協(xié)議
- 電梯采購工程合同
- 文化旅游項目開發(fā)合作框架協(xié)議
- 2024年江蘇農(nóng)林職業(yè)技術(shù)學(xué)院單招職業(yè)適應(yīng)性測試題庫匯編
- 《中國痤瘡治療指南》課件
- 《休閑農(nóng)業(yè)園區(qū)管理》課件-第三章 休閑農(nóng)業(yè)的生產(chǎn)管理
- 2024年常州機電職業(yè)技術(shù)學(xué)院單招職業(yè)適應(yīng)性測試題庫及答案解析
- 2024年人教版小學(xué)語文六年級下冊第二單元測試卷(含答案解析)【可編輯打印】
- 教育技術(shù)學(xué)研究方法基礎(chǔ)
- 幼兒園大班科學(xué)課件:《植物的生長》
- 湖南高速鐵路職業(yè)技術(shù)學(xué)院單招職業(yè)技能測試參考試題庫(含答案)
- 《商務(wù)數(shù)據(jù)分析》 課件 項目一 商務(wù)數(shù)據(jù)分析認知
- 2023學(xué)年、2024學(xué)年臨平區(qū)公辦學(xué)校校方責(zé)任險投保采購項目招標文件
- 橋梁施工案例分析
評論
0/150
提交評論