版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Classes 2,Prepared by: Yang YongE-Mail:,Review questions,What is the difference between public members and private members? Please tell the difference between class and object. Do you think class is better than struct? How can you say that?,4. class A public: void setA(int); void showA() coutaendl;
2、private: int a; ; void setA(int x) a=x; ,#include void main() A B; int x; cinxendl; B. setA(x); coutB.aendl; ,Review questions,Contents,Constructor Copy-constructor Destructor Class combination Static members,Main objectives,How to define constructors and destructors for a class. Grasp the mechanism
3、 of class combination and the calling order in the class. How to use static members in the class.,Constructor,1.Definition: A special member function, it is primarily used to allocate memory for object and initialize the object as a particular state. 2. Characteristics: The name of constructor must
4、be same as the class name, or it will be regarded as a common member function. Recalled by the complier system automatically when the object is defined.,It can have any type of parameters, but cant have any returning type. So when defining and declaring a constructor, you cant point out its type, ev
5、en the “void” type. In practice, you usually need to define a constructor for each class. If you havent define a constructor, the complier will automatically create a constructor without a parameter. Its aim is to allocate a memory to the object.,Constructor,class A int a; public: void setA(int x) a
6、=x; void showA() coutaendl; ;,#include void main() A B; int x; coutx; B. setA(x); B.showA(); ,Example 1 of default constructor,A( ) ,class A int a; public: void setA(int x) a=x; void showA() coutaendl; ;,#include void main() A B; int x; coutx; B. setA(x); B.showA(); ,A( ) cout“Constructor”endl;,Exam
7、ple 2 of default constructor,Example of constructor,/ The declaration of constructor: class Clock public: Clock (int NewH, int NewM, int NewS); /constructor void ShowTime() coutHour“:” Minute “:” Second endl; private: int Hour,Minute,Second; ;,/The realization of constructor: Clock:Clock(int NewH, i
8、nt NewM, int NewS) Hour=NewH; Minute=NewM; Second=NewS; /The recall of constructor: void main() Clock A (10,10,15); /calling constructor A.ShowTime( ); ,Note: Constructor cant be recalled as other member functions, it must be recalled together with the object being defined: class_name object_name(pr
9、actical argument) The following is wrong: void main() Clock A; A. Clock(10,10,15); ,Example of constructor,Constructor with default parameters,For example: class simplecat public: simplecat(int age=0, int weight=0); int GetAge() return itsage; int GetWeight() return itsweight; private: int itsage; i
10、nt itsweight; ;,simplecat:simplecat(int age, int weight) itsage=age; itsweight=weight; void main() simplecat F1; coutF1.GetAge()tF1.GetWeight()endl; simplecat F2(5); coutF2.GetAge()tF2.GetWeight()endl; simplecat F3(5,8); coutF3.GetAge()tF3.GetWeight()endl; ,Copy-constructor,1. Definition: Copy-const
11、ructor is a special constructor, its format parameter is a reference of the self-class object. 2. Definition Method: class class_name public : class_name (formal argument);/constructor class_name (class_name Copy-constructor only has a parameter, and the parameter must be the reference of self-class
12、 object; If the user hasnt defined a copy-constructor, you recall the default copy-constructor when defining a object.,Copy-constructor (cont.),Example of copy-constructor,class Point public: Point(int xx=0,int yy=0)X=xx; Y=yy; Point(Point,Point:Point (Point ,Destructor,1. Definition: Destructor is
13、a special member functions, and it performs opposite to the constructor, including the following tasks: Complete some cleaning work before the object being deleted. Automatically recall the destructor by the complier system when the survival period ended, and then free up all resource belonging to t
14、his object, such as memory space.,Destructor (cont.),2. Characteristics: The name of destructor is the same as constructor except with a “” before it; Destructor has no parameters and no returning value, and cant be overloaded. So there is only a destructor within a class. If destructor hasnt been d
15、eclared in the program, the complier will automatically create a default destructor.,Example of constructor and destructor,class demo int x,y; public: demo(int a, int b)x=a;y=b; demo(demo ,void main() demo d1; d1.show(); demo d2(3,5); d2.show(); ,Example of constructor and destructor,Example of clas
16、s application,一圓型游泳池如圖所示,現(xiàn)在需在其周?chē)ㄒ粓A型過(guò)道,并在其四周?chē)蠔艡?。柵欄價(jià)格為35元/米,過(guò)道造價(jià)為20元/平方米。過(guò)道寬度為3米,游泳池半徑由鍵盤(pán)輸入。要求編程計(jì)算并輸出過(guò)道和柵欄的造價(jià)。,游泳池,過(guò)道,#include const float PI = 3.14159; const float FencePrice = 35; const float ConcretePrice = 20; /Circle declarations class Circle private: float radius; public: Circle(float r); /con
17、structor float Circumference() ; / function for circumference float Area() ; / function for area ;,Circle:Circle(float r) radius=r; float Circle:Circumference() return 2 * PI * radius; float Circle:Area() return PI * radius * radius; ,Example of class application,void main() float radius; float Fenc
18、eCost, ConcreteCost; coutradius; / Declare two circles Circle Pool(radius); Circle PoolRim(radius + 3);,Example of class application,/ 計(jì)算柵欄造價(jià)并輸出 FenceCost = PoolRim.Circumference() * FencePrice; cout Fencing Cost is ¥ FenceCost endl; / 計(jì)算過(guò)道造價(jià)并輸出 ConcreteCost = (PoolRim.Area ( ) -Pool.Area()* Concret
19、ePrice; cout Concrete Cost is ¥ ConcreteCost endl; ,Overloading of constructors,Similar to a common function, the constructors also can be overloaded. The constructors which have similar function, but have different types and number of parameters, can be assigned to the same function name. The overl
20、oaded constructors can be respectively recalled according to their parameters types and number by the complier.,Example of overloaded constructors,class rectangle public: Rectangle( ); Rectangle(int width, int length); private: int width; int length; ; int main() Rectangle rect1; /call Rectangle( )
21、Rectangle rect2(4,5); /call Rectangle(int , int) ,Notes: When overloading the none-parameters constructors and constructors with default parameters, the main program will bring about duality.,For example: class rectangle public: Rectangle( ); Rectangle(int width=4, int length=6); private: int width;
22、 int length; ; int main() Rectangle rect1; Rectangle rect2(4,5); ;,The concept of class combination,The member data of one class is an object of another class. The more complicated abstract can be realized based on the existing abstract.,Example,class Point private: float x,y; public: Point(float h,
23、float v); float GetX(void); float GetY(void); void Draw(void); ; /.omit the realization of the functions,class Line private: Point p1,p2; public: Line (Point a,Point b); void Draw(void); ; /.omit the realization of the functions,Example,Class combination calling order,Recall order of constructors: F
24、irstly recall the constructor of built-in object. (According the order of declaration). Then recall the constructors of self-class. (The destructors recall order is to the contrary.) If recalling the default constructor ,the initialization of built-in object will recall the relevant default construc
25、tors simultaneously.,Example of class combination,#include int nextStudentID=0; class StudentID public: StudentID( ) value=+nextStudentID; cout Constructing StudentID:valueendl; StudentID() coutDestructing id:valueendl; protected: int value; ;,class Student public: Student(char * pName=“noName”) cou
26、t“Constructing student”“ ”pNameendl; Student()cout“Destructing Student” endl; protected: StudentID id; ; void main() Student s(“randy”); ,Constructor design of class combination,Principle: The constructor not only has to initialize fundamental type member data of self-class, but has to initialize th
27、e object member of other class. Declaration method: class:class(args.):object1(arg.),object2(arg.),. / initialization of the class. ,class StudentID public: StudentID(int id) value=id; coutAssigning student idvalueendl; StudentID( ) coutDestructing ID,valueendl; protected: int value; ;,class student
28、 public: student(char * pName=no name, int ssID=0):id(ssID) coutConstructing student,pName endl; protected: StudentID id; ;,void main() Student s(“Randy”,9818); student t(“Jenny”,9819); ,Constructor design of class combination,Former reference declaration,Class must be declared firstly before it can
29、 be used. If you want to cite some class before its being defined,your must use former reference declaration. Former reference declaration only to introduce an identifier for the program, and its detailed declaration is located somewhere else.,Example,class B; / former declaration class A public: vo
30、id f(B b); ; class B public: void g(A a); ;,Static member,1. Definition: Static member is similar to static variable. It doesnt belong to a certain object, but belongs to the whole class and shared by all objects of the class. Static member has two kinds: static member data, static member function.,
31、2. Static member data: Declared by keyword “static”. Used by all objects belonging to the class, so only updated once, all the static member data of all objects can be updated simultaneously, and its value is the same. Initialized outside of the class with: type classname: static data=initialization
32、 value,Static member (cont.),Example 1 of Static member data,#include class rectangle public: rectangle(int width=0, int length=0) itswidth=width; itslength=length; coutR+; rectangle(rectangle ,rectangle:rectangle(rectangle ,#include #include using namespace std; class Student string name; public: static int num; void set(string str) name = str; +num; ;,Example 2 of Static m
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 安全復(fù)工復(fù)產(chǎn)培訓(xùn)課件
- 2025年儲(chǔ)能隔膜技術(shù)前沿:多孔材料應(yīng)用與離子傳導(dǎo)效率分析報(bào)告
- 企業(yè)環(huán)保法律法規(guī)實(shí)施指南
- 山東省體育局所屬事業(yè)單位2025年度公開(kāi)招聘人員備考題庫(kù)完整答案詳解
- 電力光伏知識(shí)培訓(xùn)課件
- 2026年漢中職業(yè)技術(shù)學(xué)院?jiǎn)握新殬I(yè)傾向性考試題庫(kù)必考題
- 2026年山西衛(wèi)生健康職業(yè)學(xué)院?jiǎn)握新殬I(yè)技能測(cè)試題庫(kù)必考題
- 2026年度河南省省直機(jī)關(guān)公開(kāi)遴選公務(wù)員159人備考題庫(kù)附答案
- 2026山東臨沂高新區(qū)法律顧問(wèn)選聘4人參考題庫(kù)含答案
- 2026年甘肅省天水工業(yè)博物館寒假大學(xué)生志愿者招募參考題庫(kù)及答案1套
- 上海市松江區(qū)2026屆初三一模物理試題(含答案)
- 小學(xué)六年級(jí)英語(yǔ)2026年上學(xué)期語(yǔ)法改錯(cuò)綜合真題
- 2026長(zhǎng)治日?qǐng)?bào)社工作人員招聘勞務(wù)派遣人員5人備考題庫(kù)完美版
- 護(hù)理核心制度內(nèi)容精要
- 光伏板清洗施工方案
- 閱讀理解體裁與命題方向(復(fù)習(xí)講義)-2026年春季高考英語(yǔ)(上海高考專(zhuān)用)
- 俱樂(lè)部轉(zhuǎn)讓合同模板(3篇)
- 指南抗菌藥物臨床應(yīng)用指導(dǎo)原則(2025版)
- 光伏系統(tǒng)的安裝工程監(jiān)理實(shí)施細(xì)則
- 教練員勞務(wù)合同范本
- 預(yù)防凍雨災(zāi)害課件
評(píng)論
0/150
提交評(píng)論