軟件設(shè)計(jì)(II)教學(xué)課件:Chapter1-4 Basics of OOP and C++_第1頁(yè)
軟件設(shè)計(jì)(II)教學(xué)課件:Chapter1-4 Basics of OOP and C++_第2頁(yè)
軟件設(shè)計(jì)(II)教學(xué)課件:Chapter1-4 Basics of OOP and C++_第3頁(yè)
軟件設(shè)計(jì)(II)教學(xué)課件:Chapter1-4 Basics of OOP and C++_第4頁(yè)
軟件設(shè)計(jì)(II)教學(xué)課件:Chapter1-4 Basics of OOP and C++_第5頁(yè)
已閱讀5頁(yè),還剩32頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Chapter 14 Basics of OOP and C+1.1 From POP to OOP1.2 Concepts and Benefits of OOP1.3 OOP Languages - from C to C+1.4 Different Statements from C1.5 Functions in C+1.1 From POP to OOP Software Evolution Driven by software crisis, arose in 1970s, and continuing? The Standish Group CHAOS report 1994 -

2、 2004 (Schach, 2005)Evolution of Programming ParadigmsMachine languageAssembly languageProcedure-oriented Programming (POP)Object-oriented Programming (OOP)A Look at POP4The problem is viewed as a sequence of thingsProgram structure focuses on hierarchical decomposition.The OOP Paradigm5View the pro

3、blem as objectsData are tied closely to the functions that operate on it.1.2 Concepts and Benefits of OOP6Objects(對(duì)象對(duì)象): the basic run-time entities in an object-oriented system, such as a person, a place, a bank account.Two popular notations (Fig.1.7):7Concepts of OOP Object(對(duì)象) Represents an entit

4、y in the real world that can be distinctly identified. For example, a student, a desk, a circle, and even a loan. Has a unique identity, state, and behaviors. State: a set of data fields (or properties) with values Behavior: a set of functions Class: Objects are variables of the type class, so a cla

5、ss is thus a collection of objects of similar type. Examples: Fruit mango/ Fruit apple/Fruit orange mango, apple and orange are members of class fruit.78Objects and Class(類與對(duì)象)(類與對(duì)象)Circle objectsradius,getArea()Circle class89Class Abstraction To separate class implementation from the use of the cla

6、ss The creator of the class provides a description of the class and let the user know how the class can be used. The detail of implementation is encapsulated and hidden from the user. The user of the class does not need to know how the class is implemented. Class Contract (Signatures of public metho

7、ds and public constants) Class Class implementation is like a black box hidden from the clients Clients use the class through the contract of the class Object vs. Class The “is a” relationship Apple is a fruit Elephant is an animal Circle is a shape A is a B A has the characteristics/features/proper

8、ties of B A inherits B Class A inherits class BInheritance can extend existing classes!10Features of OOPl Data encapsulationl Data hiding and access mechanismsl Automatic initialization and clear-up of objectsl Operator overloadingBenefits of OOP12BenefitsGreater programmer productivity, Better qual

9、ity of softwareLess maintenance cost How can?Eliminating redundant code and re-using existing classes;Building programs from standard working modules;Building secure programs based on the principle of data hiding;1.3 OOP Languages - from C to C+l C+ is an augmented(incremented) version of ClFrom “i+

10、”l C+ is a superset of Clalmost all C programs are also C+ programs.l Add-ons in C+lObject and class, lInheritance lFunction overloading, and lOperator overloadingA Simple C+ Program14#include /*include header file. iostream is the header fil to use input/output statements. */using namespace std; /*

11、Defining a scope for the identifiers that are used in program. std is the current global scope. */ int main() int i; cini; couti is I endl; /C+ statement return 0; /End of exampleAn Example with Class15Program 2.3. The codeclass person char name30; int age; public: void getdata(void); void display(v

12、oid); ; int main() person p; p.getdata(); p.display(); return 0; Enter Name: Ravinder Enter Age: 30 Name: Ravinder Age: 30Structure of C+ Program16Include filesClass declarationsMember functions definitionsMain functionHeader fileBody fileMainfile 1.4 Different Statements from C Reference variables

13、New operators in C+ Memory management Format manipulators Type casting Operator overloading Reference variables A new type of variableAn alias of a variable, NOT an independent variableE.g.: char &a = n;Useful: call-by-reference - to avoid the copy of parameters back and forth. 18 data-type &

14、; reference-name = variable name a special type of variable an alias for another variable Syntax: & = ;Reference Variable19int count = 1; int &refCount = count; 1countrefCountCall-by-Reference To avoid the copy of parameters back and forthint main()int a=3, b=5;coutmax(a,b)=y) z=x; else z=y;

15、 return z;New Operators in C+ : Scope resolution operator(作用域解析操作符) :* Pointer-to-member declarator (成員指針聲明符) -* Pointer-to-member operator(成員指針操作符) .* Pointer-to-member operator (成員指針操作符) new Memory allocation operator (內(nèi)存分配操作) delete Memory release operator(內(nèi)存釋放操作符) endl Line feed operator (換行操作符)

16、 setw Field width operator (域?qū)挷僮鞣?21 Memory Managementmalloc/calloc freenew delete Dynamic apply and release of memory C operator: C+ operator: int *pValue = new int; /Allocate memory for an int variable; /The address is assigned to “pValue”.delete pValue; int *mylist = new int10; /Allocate memory f

17、or an array of 10 ele.; /The address is assigned to “mylist”.delete mylist;23Dynamic Memory Allocation The method allowing programmers to allocate storage Dynamically (during the execution time) and “Manually” (using explicit statement) “new” operator: to allocate memory int *pValue = new int; int *

18、mylist = new int10;CorrectReverseAllocate memory for an int variable;The address is assigned to “pValue”. Allocate memory for an array of 10 int elements;The address is assigned to “mylist”.malloc vs. new malloc/free is standard library function Be invoked at runtime NOT suitable for objects new/del

19、ete is C + + operator Be compiled by compiler Suitable for objects Can be used to execute complex operations, including construction of objects.Format Manipulatorsendl = n coutxendl; coutx“n”;setw: controlling output interval. coutsetw(5)sumendl; 25 Type Casting OperatorUsage: (type-name)expression;

20、 / C type-name(expression); /C+ static_cast(expression);/C+Examples: average = sum/(float)i; average = sum/float(i); average = sum/static_cast(i); 26 1.5 Functions in C+ Call by Reference Inline Functions Default Arguments Function OverloadingCall-by-Reference void swap(int &a,int &b) int t=

21、 a; /Dynamic initialization a = b; b = t; int main() int m, n; cinmn; swap(m, n); 28 the function is working with its own arguments,it is actually working on the original data Cost of functions Runtime overhead caused by function calls Pushing arguments and CPU registers into the stack Transferring

22、control to and from a function Inline function (內(nèi)聯(lián)函數(shù)) A function not called by the caller. The compiler copies the function code in line at the point of each invocation.Inline Functions29inline (para list) Used for short functions rather than long functions Copying long function codes lengthens the

23、program A long “inline” function may be treated as a common function: the “inline” is ignored. The compiler makes such decisionsNotes for Inline Functions30 Parameters declared with default valuesDefault Arguments (缺省參數(shù)缺省參數(shù)) 31type name(type para1, , type parai=defaultvalue, ) /Function body Stateme

24、nts;l Note: Default arguments must be declared at lastint max(int x, int y=0, int z=0) int max(int x, int y=0, int z) E.g.: Program4.2Const Arguments In c+, an argument to a function can be declare as constant. To protect the arguments from changes by the function. E.g. int strlen(const char *p);int length(const string & s); Overloading (重載): two functions within one file with The sam

溫馨提示

  • 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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論