freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

清華大學c課件第4章(編輯修改稿)

2024-11-12 13:37 本頁面
 

【文章內(nèi)容簡介】 { x = xx。 y = yy。 } Point(Pointamp。 p)。 int getX() { return x。 } int getY() { return y。 } private: int x, y。 }。 構(gòu)造函數(shù)和析構(gòu)函數(shù) Point::Point (Pointamp。 p) { x = 。 y = 。 cout Calling the copy constructor endl。 } 33 C++語言程序設(shè)計 清華大學 鄭莉 34 拷貝構(gòu)造函數(shù) (例 42) ? 當用類的一個對象去初始化該類的另一個對象時系統(tǒng)自動調(diào)用拷貝構(gòu)造函數(shù)實現(xiàn)復制。 int main() { Point a(1,2)。 Point b = a。 //拷貝構(gòu)造函數(shù)被調(diào)用。也可以寫 Point b(a)。 cout () endl。 } 構(gòu)造函數(shù)和析構(gòu)函數(shù) C++語言程序設(shè)計 清華大學 鄭莉 35 拷貝構(gòu)造函數(shù) (例 42) ? 若函數(shù)的形參為類對象,調(diào)用函數(shù)時,實參賦值給形參,系統(tǒng)自動調(diào)用拷貝構(gòu)造函數(shù)。例如: void fun1(Point p) { cout () endl。 } int main() { Point a(1, 2)。 fun1(a)。 //調(diào)用拷貝構(gòu)造函數(shù) return 0。 } 構(gòu)造函數(shù)和析構(gòu)函數(shù) C++語言程序設(shè)計 清華大學 鄭莉 36 拷貝構(gòu)造函數(shù) (例 42) ? 當函數(shù)的返回值是類對象時,系統(tǒng)自動調(diào)用拷貝構(gòu)造函數(shù)。例如: Point fun2() { Point a(1, 2)。 return a。 //調(diào)用拷貝構(gòu)造函數(shù) } int main() { Point b。 b = fun2()。 return 0。 } 構(gòu)造函數(shù)和析構(gòu)函數(shù) C++語言程序設(shè)計 清華大學 鄭莉 37 隱含的拷貝構(gòu)造函數(shù) 如果程序員沒有為類聲明拷貝初始化構(gòu)造函數(shù),則編譯器自己生成一個隱含的拷貝構(gòu)造函數(shù)。 這個構(gòu)造函數(shù)執(zhí)行的功能是:用作為初始值的對象的每個數(shù)據(jù)成員的值,初始化將要建立的對象的對應數(shù)據(jù)成員。 構(gòu)造函數(shù)和析構(gòu)函數(shù) C++語言程序設(shè)計 清華大學 鄭莉 38 析構(gòu)函數(shù) ? 完成對象被刪除前的一些清理工作。 ? 在對象的生存期結(jié)束的時刻系統(tǒng)自動調(diào)用它,然后再釋放此對象所屬的空間。 ? 如果程序中未聲明析構(gòu)函數(shù),編譯器將自動產(chǎn)生一個隱含的析構(gòu)函數(shù)。 構(gòu)造函數(shù)和析構(gòu)函數(shù) C++語言程序設(shè)計 清華大學 鄭莉 39 構(gòu)造函數(shù)和析構(gòu)函數(shù)舉例 include iostream using namespace std。 class Point { public: Point(int xx,int yy)。 ~Point()。 //...其他函數(shù)原型 private: int x, y。 }。 構(gòu)造函數(shù)和析構(gòu)函數(shù) Point::Point(int xx,int yy) { x = xx。 y = yy。 } Point::~Point() { } //...其他函數(shù)的實現(xiàn)略 40 C++語言程序設(shè)計 清華大學 鄭莉 41 類的應用舉例 (例 43) 一圓形游泳池如圖所示,現(xiàn)在需在其周圍建一圓形過道,并在其四周圍上柵欄。柵欄價格為 35元 /米,過道造價為 20元 /平方米。過道寬度為 3米,游泳池半徑由鍵盤輸入。要求編程計算并輸出過道和柵欄的造價。 游泳池 過道 include iostream using namespace std。 const float PI = 。 //圓周率 const float FENCE_PRICE = 35。 //柵欄的單價 const float CONCRETE_PRICE = 20。//過道水泥單價 class Circle { //定義類 Circle public: //外部接口 Circle(float r)。 //構(gòu)造函數(shù) float circumference()。 //計算圓的周長 float area()。 //計算圓的面積 private: //私有數(shù)據(jù)成員 float radius。 //圓半徑 }。 42 //類的實現(xiàn) //構(gòu)造函數(shù)初始化數(shù)據(jù)成員 radius Circle::Circle(float r) { radius = r。 } //計算圓的周長 float Circle::circumference() { return 2 * PI * radius。 } //計算圓的面積 float Circle::area() { return PI * radius * radius。 } 43 int main () { float radius。 cout Enter the radius of the pool: 。 // 提示用戶輸入半徑 cin radius。 Circle pool(radius)。 //游泳池邊界 Circle poolRim(radius + 3)。 //柵欄 //計算柵欄造價并輸出 float fenceCost = () * FENCE_PRICE。 cout Fencing Cost is $ fenceCost endl。 44 //計算過道造價并輸出 float concreteCost = (() ()) * CONCRETE_PRICE。 cout Concrete Cost is $ concreteCo
點擊復制文檔內(nèi)容
教學課件相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖片鄂ICP備17016276號-1