1 hibernate 一級緩存
Session?
? evict(Object o) 從緩存中清除指定的持久化對象
? clear()???????? 清除緩存中所有對象
2 批量更新于批量刪除
? 1) 批量更新
?? Iterator customers=session.find("from Customer c where c.age>0");
?? while(customers.hasNext()){
???? Customer customer=(Customer)customers.next();
???? customer.setAge(customer.getAge()+1);
?? }
?? tx.commit();
?? session.close();
? 缺點:內存中加載了大量數據
??????? 執行了多次update 語句
?
?? 改進
?? Iterator customers=session.find("from Customer c where c.age>0");
?? while(customers.hasNext()){
???? Customer customer=(Customer)customers.next();
???? customer.setAge(customer.getAge()+1);
???? session.flush();
???? session.evict(customer);
?? }
?? tx.commit();
?? session.close();
?? 遺留問題
?? 執行了多次update 語句
??
?? 采用jdbc api 進行調用
?? Connection con=session.connection();
?? PrepareStatement stmt=con.prepareStatement("update customers set age=age+1 where age>0");
?? stmt.executeUpdate();
?? tx.commit();
?? 另外,也可以調用底層的存儲過程進行批量更新
?? create or replace procedure batchUpdateCustomer(p_age,in number) as
?? begin
????? update customer set age=age+1 where age>p_age;
?? end;
??
?? tx=session.beginTransaction();
?? Connection con=session.connection();
?? CallableStatement cstmt=con.prepareCall(batchUpdateCustomer);
?? cstmt.setInt(1,0);
?? cstmt.eqecuteUpdate();
?? tx.commit();
?? 2) 批量數據的刪除
??? session.delete("from? Customer c where c.age>0");
??? 實際調用的過程
??? session * from Customer where age>0;
??? 在把所有數據加載到內存之后執行多條delete 語句
??? delete from customer where id=i;
???? .......................
?? 改進辦法采用jdbc api 進行批量數據的刪除
?????
?? tx=session.beginTransaction();
?? Connection con=session.connection();
?? con.execute("delete from customers where age>0");
?? tx.commit();
Hibernate????????????????? java???????????????????? sql??????????????????? oracle???????
integer or int???????????? int or Integer?????????? INTEGER
long?????????????????????? long or Long???????????? BIGINT
short????????????????????? short or Short?????????? SMALLINT
byte?????????????????????? byte or Byte???????????? TINYINT
float????????????????????? float or Float?????????? FLOAT
double???????????????????? double or Double???????? DOUBLE
big_decimal??????????????? java.math.BigDecimal???? NUMBERBIC
character????????????????? char java.lang.Character CHAR(1)
?????????????????????????? String?????????????????????
string???????????????????? String??????????????????? VARCHAR
boolean??????????????????? boolean or Boolean??????? BIT
date?????????????????????? java.util.Date??????????? DATE
?????????????????????????? java.sql.Date
time?????????????????????? Date or java.sql.time???? TIME
timestamp????????????????? Date or java.sql.Timestamp TIMESTAMP??????????????
binary???????????????????? byte[]??????????????????? blog?????????????????? blog
text?????????????????????? String??????????????????? clob?????????????????? clog
serializable???????????????????????????????????????? blog?????????????????? blog????????????
clob?????????????????????? java.sql.clob??????????? clob??????????????????? clob
blob?????????????????????? java.sql.blob???????????? blog?????????????????? blob
1 動態連接
Windows 運作機制的核心是一個稱作動態連接的概念
#include <windows.h>
int WINAPI WinMain (?HINSTANCE hInstance, HINSTANCE hPrevInstance,
???PSTR szCmdLine, int iCmdShow)
{
MessageBox (NULL, TEXT ("Hello, Windows 98!"), TEXT ("HelloMsg"), 0);
return 0 ;
}
?1) #include <windows.h>包含其它的頭文件
?2) 程序入口
?? int WINAPI WinMain (?HINSTANCE hInstance,HINSTANCE hPrevInstance,
??????????? ?PSTR szCmdLine,int iCmdShow)
?? a #define WINAPI __stdcall? 指定一個呼叫約定,包括如何生產機器碼,參數如何入棧
?? b HINSTANCE hInstance 執行體代號,唯一標識該程序
?? c HINSTANCE hPrevInstance 已經不采用
?? d PSTR szCmdLine 參數列表
?? e int iCmdShow 顯示方式
? 3) MessageBox 函數
?? MessageBox (NULL, TEXT ("Hello, Windows 98!"), TEXT ("HelloMsg"), 0);
?? 參數1 窗體代號
?? 參數2 主題顯示文字
?? 參數3 標題顯示文字
?? 參數4 按鈕,0 為確認 使用C語言的OR(|)操作符號將上面顯示的一個常數與代表內定按鈕的常數組合:
?
#define ?MB_OK?????????????????????? ????0x00000000L
#define ?MB_OKCANCEL???????????????? ????0x00000001L
#define ?MB_ABORTRETRYIGNORE???????? ????0x00000002L
#define ?MB_YESNOCANCEL????????????? ????0x00000003L
#define ?MB_YESNO??????????????????? ????0x00000004L
#define ?MB_RETRYCANCEL?
#define ?MB_DEFBUTTON1?????????????? ????0x00000000L
#define ?MB_DEFBUTTON2?????????????? ????0x00000100L
#define ?MB_DEFBUTTON3?????????????? ????0x00000200L
#define ?MB_DEFBUTTON4
圖示的外觀
#define ?MB_ICONHAND???????????????? ?????? 0x00000010L
#define ?MB_ICONQUESTION???????????? ????????? 0x00000020L
#define ?MB_ICONEXCLAMATION????????? ????????? 0x00000030L
#define ?MB_ICONASTERISK
#define ?MB_ICONWARNING????????????? ?MB_ICONEXCLAMATION
#define ?MB_ICONERROR??????????????? ?MB_ICONHAND
#define ?MB_ICONINFORMATION????????? ?MB_ICONASTERISK
#define ?MB_ICONSTOP
2 c 語言編譯過程 c--compile --?? .obj?? ---linking----? .exe
一 文件
?? 1 c 標準文件驅動器,可以支持兩種文件類型,二進制文件,和文本文件。c 標準文件是在頭文件
stdio.h 中聲明。
標準文件類型通過指針來進行存儲 FILE * fp;
?? 2 c++ 流式文件類 fstream,ifstream 和ofstream,分別對應讀寫,讀和寫,并支持文本和二進制文
件。
?? 3 非緩沖文件
二 文件對話框組件
?? 1 OpenDialog 兩種.TXT and .PAS 兩種類型的過濾器。
???? 1) Filter OpenDaalog1->Filter="Text files{*.txt}|*.TXT|Pascal files{*.pas}|*.PAS";
? 同一個過濾器中,還可以有多種文件后綴
????? OpenDialog1->Filter="Pascal files|*.PAS;*.DPK;*.DPR";
???? 2) FilterIndex 設置對話框一打開時選中的文件過濾。數值從1開始計算。
???? 3) InitialDir 設置對話框打開時定位的目錄。
???? 4) Options
?????? OpenPictureDialog1->Options.Clear();
?????? OeenPictureDialog1->Options<<ofFileMustExist<<ofHideReadOnly<<ofNoChangeDir;
???? 5) Title 設置對話框標題中顯示的內容。
?? 2 SaveDialog 組建可以選擇并保存文件
?? 3 OpenPictureDialog 可以選擇并打開圖形文件。
?? 4 SavePictureDialog 可以選擇并保存圖形文件。
三 Win3。1 相關組件
?? FileListBox,DirectoryListBox,DriveCombox,FilterComboBox
四 常用文件管理函數
? 1 文件函數常用函數
? 將一個文件從記錄盤上刪除,如果不存在或無法刪除。則返回False。
? extern PACKAGE bool __fastcall DeleteFile(const AnsiString FileName);
? void __fastcall TFORM1::ButtonClick(TObject *Sender)
? {
????? char buffer[256];
????? GetWindowsDirectory(buffer,sizeof(buffer));//獲取Windows 系統目錄
???? AnsiString asFileName=FileSearch(Edit1->Text,GetCurrentDir()+AnsiString(";")
+AnsiString(buffer));//在當前目錄下和windows系統}//目錄下查詢文件。?
? if(asFileName.IsEmty()) ShowMessage(AnsiString("Couldn't Found")+Edit1->Text1);
? 2 FileSeek
? extern PACKAGE int __fastcall FileSeek(int Handle, int Offset, int Origin);
? extern PACKAGE __int64 __fastcall FileSeek(int Handle, const __int64 Offset, int Origin);
? Description
? Use FileSeek to reposition the read/write point in a file that was opened with FileOpen or
FileCreate. Handle is the file handle that was returned by FileOpen or FileCreate.
? Offset specifies the number of bytes from Origin where the file pointer should be
positioned. Origin is a code with three possible values, denoting the beginning of the file,
? the end of the file, and the current position of the file pointer.
? Origin?Action
? 0?The file pointer is positioned Offset bytes from the beginning of the file.
? 1?The file pointer is positioned Offset bytes from its current position.
? 2?The file pointer is positioned Offset bytes from the end of the file.
? If FileSeek is successful, it returns the new position of the file pointer; otherwise, it
returns -1.
?? void __fastcall TForm1::Button1Click(TObject *Sender)
{
? int iFileHandle;
? int iFileLength;
? int iBytesRead;
? char *pszBuffer;
? if (OpenDialog1->Execute())
? {
??? try
??? {
????? iFileHandle = FileOpen(OpenDialog1->FileName, fmOpenRead);
????? iFileLength = FileSeek(iFileHandle,0,2);
????? FileSeek(iFileHandle,0,0);
????? pszBuffer = newchar[iFileLength+1];
????? iBytesRead = FileRead(iFileHandle, pszBuffer, iFileLength);
????? FileClose(iFileHandle);
????? for (int i=0;i<iBytesRead;i++)
????? {
??????? StringGrid1->RowCount += 1;
??????? StringGrid1->Cells[1][i+1] = pszBuffer[i];
??????? StringGrid1->Cells[2][i+1] = IntToStr((int)pszBuffer[i]);
????? }
????? delete [] pszBuffer;
??? }
??? catch(...)
??? {
????? Application->MessageBox("Can't perform one of the following file operations: Open,
Seek, Read, Close.", "File Error", IDOK);
??? }
? }
}
?3FileExists
? if(FileExist(SaveDialog1->FileName))
?{
?? RenameFile(SaveDialog1->File,SaveDialog1->FileName+".bak");
?}
?iFileHandle=fileCreate(SaveSialog1->FileName);
?for(int i=0;i<Memo2->Lines->String[i].length())
?{
?? FileWrite(iFileHandle,Memo2->Lines->String[i].c_str(),length);
?}
?FileClose(iFileHandle);
? 4 FileGetAttrs
? FileGetAttr returns the attributes of the file as a string of bits. This value is the same
as the Attr field of a TSearchRec struct. Check for individual attributes with code such as
the following:
? int Attrs = FileGetAttr("MyFile.sys");
? if x(Attrs & faHidden)
? FileSetAttr("MyFile.sys", Attrs & !faHidden);
? A return value of -1 indicates that an error occurred.
? 5 FileSetAttrs
? FileSetAttr sets the file attributes of the file given by FileName to the value given by
Attr. The value of Attr is formed by combining the appropriate file attribute constants, as
in the following:
? FileSetAttr("MyFile.sys", faReadOnly | faSysFile);
? FileSetAttr returns zero if the function was successful. Otherwise the return value is an
error code.
三 目錄操作常用函數
? 1 CreateDir
???? #include <Filectrl.hpp>
?? void __fastcall TForm1::Button1Click(TObject *Sender)
?? {
????? if (!DirectoryExists("c:\\temp"))
????? {
??????????? if (!CreateDir("C:\\temp"))
??????????? throw Exception("Cannot create c:\\temp directory.");
????? }
?? }
?? 2 ForceDirectories
?? ForceDirectories creates a new directory as specified in Dir, which must be a fully-
qualified path name. If the directories given in the path do not yet exist, ForceDirectories
attempts to create them.
?? ForceDirectories returns true if it successfully creates all necessary directories, false
if it could not create a needed directory.
?? Important
?? Do not call ForceDirectories with an empty string. Doing so causes ForceDirectories to
throw an exception.?
?? void __fastcall TForm1::Button1Click(TObject *Sender)
?? {
???????? AnsiString Dir = "C:\Apps\Sales\Local";
???????? if (ForceDirectories(Dir))
???????? Label1->Caption = Dir + " was created";
?? }
?? 3 GetCurrentDir
?? 獲取當前的目錄完整的路徑名
?? 4 RemoveDir
?? 刪除一個存在的目錄,目錄必須為空
?? 5 SetCurrentDir設置系統的當前目錄
?? 6 SelectDirectory
?? extern PACKAGE bool __fastcall SelectDirectory(constAnsiString Caption, const WideString
Root, AnsiString &Directory);
?? Call SelectDirectory to let the user enter a directory name.??
?? Use the first syntax to display the Windows directory browser. The Caption parameter
specifies a caption for the dialog. The Root parameter specifies the root directory from
which to browse. The selected directory is returned as the Directory parameter. When using
this syntax,
?? SelectDirectory does not change the value of the current directory.
??
?? extern PACKAGE bool __fastcall SelectDirectory(AnsiString &Directory, TSelectDirOpts
Options, int HelpCtx);
?? enum TSelectDirOpt { sdAllowCreate, sdPerformCreate, sdPrompt };
?? typedef Set<TSelectDirOpt, sdAllowCreate, sdPrompt>? TSelectDirOpts;
??
?? sdAllowCreate?An edit box allows the user to type in the name of a directory that
??????????????????????? does not exist. This option does not create a directory: the
application
??????????????????????? must read the name of the selected directory and create it i
????????????????????? f desired.
?? sdPerformCreate?Used only in combination with sdAllowCreate. If the user enters a
directory
??????????????????????? name that does not exist, the directory selection dialog creates it.
?? sdPrompt??????? Used only in combination with sdAllowCreate. Displays a message box
?????????????????????? that informs the user when the entered directory does not exist
?????????????????????? and asks if the directory should be created.
?????????????????????? If the user chooses OK, the directory is created
?????????????????????? if the option set includes sdPerformCreate.
?????????????????????? If the option set does not include sdPerformCreate,
?????????????????????? the directory is not created:
??????????????? the application must read the directory name and create
??? #include <FileCtrl.hpp>
??? void __fastcall TForm1::Button1Click(TObject *Sender)
?? {
????? AnsiString Dir = "C:\\Program Files\\MyApp";
????? if (SelectDirectory(Dir, TSelectDirOpts() << sdAllowCreate << sdPerformCreate <<
sdPrompt,1000))
????? Label1->Caption = Dir;
?? }
三 驅動器常用函數
? 1 DiskFree 指定驅動器中剩余空間的字節數
? 2 DiskSize 驅動器容量
四文件名常用函數
? 1 ChangeFileExt
? 2 ExtractFileDir
? 3 ExtractFileDriver
? 4 ExtractFileExt
? 5 ExtractFileName
? 6 ExtractFilePath
? 7 ExtractRelativePath
實例
1
?? 1
L a b e l 1 目錄列表( & D ) : FocusControl: DirectoryListBox1
D i r e c t o r y L i s t B o x 1 D i r L a b e l : L a b e l 6 ;
??????????????????????????????????????????????????? FileList: FileListBox1
L a b e l 2 文件列表( & S ) : FocusControl: FileListBox1
F i l e L i s t B o x 1 FileEdit: Edit1
L a b e l 3 驅動器( & R ) : FocusControl: DriveComboBox1
D r i v e C o m b o B o x 1 DirList: DirectoryListBox1
L a b e l 4 文件類型( & F ) : FocusControl: FilterComboBox1
F i l t e r C o m b o B o x 1 FileList: FileListBox1
????? Filter: 所有文件 ( * . * ) | * . * |文本文件( * . t x t ) | * . t x t
L a b e l 5 路徑名:
L a b e l 6 C : \ S a m p l e s \ S 0 6 B
L a b e l 7 文件名( & N ) : FocusControl: Edit1
E d i t 1
B u t t o n 1 文件長度( & L ) . . . Te x t : * . *
#include<stdio.h>
void __fastcall TForm1::Button1Click(TObject *Sender)
{
FILE* fp;
AnsiString FileFullName;
long size;
AnsiString PropertyMes;
FileFullName=Label2->Caption+"\\"+Edit1->Text;
if(FileExists(FileFullName))
{
? fp=fopen(FileFullName.c_str(),"rt");
? if(fp!=NULL)
? {
??? fseek(fp,0L,SEEK_END);
??? size=ftell(fp);//get the length of file
??? PropertyMes="file is total"+IntToStr(size)+"bytes.";
??? MessageDlg(PropertyMes,mtInformation,TMsgDlgButtons() << mbOK, 0);
? }else
? {
????? MessageDlg(PropertyMes,mtWarning,TMsgDlgButtons() << mbOK, 0);
? }
? fclose(fp);
?}
}
?2 獲取驅動器類型信息
?UINT GetDriveType(
? LPCTSTR lpRootPathName //獲取根目錄的路徑名稱
?)
?
表6-5?? 函數G e t D r i v e Ty p e的返回值及其含義
數 值???????????????????????????? 含 義
0??????????????????????????? 無法檢測驅動器的類型
1???????????????????????????? 根目錄不存在
D R I V E _ R E M O VA B L E 可移動驅動器
D R I V E _ F I X E D 不可移動驅動器
D R I V E _ R E M O T E 網絡驅動器
D R I V E _ C D R O M C D - R O M驅動器
D R I V E _ R A M D I S K 虛擬驅動器
Result=GetDriveType(Edit2->Text.c_str());
3 操作ini 文件
動態連接庫(Dynamic link library),是一些編譯過的可以執行的代碼模塊,后綴為.dll
1 DLL的基本理論
? 在使用普通函數庫時,可以在程序連接時將庫中的代碼拷貝到執行文件中,這時靜態鏈接,在多個同樣程序執行時,體統保留了許多
代碼副本,造成了內存資源的浪費。在使用dll時,不必將dll鏈接到程序中,而是在應用程序運行時動態的裝載dll,裝載dll被映射
到進程的地址空間中。同時,使用dll 并不時將庫代碼拷貝,只是在程序中記錄了函數的入口點和接口。
2 DLL 的優點
?1) 節省系統資源
?2) 不僅可以包括可執行代碼,還可以包括數據和各種資源
?3)支持多語言
?4)升級可以僅僅覆蓋dll 文件
?5)dll 獨立編程語言,c++builder 中的dll vc 也可以使用
3導入導出匹配
??? DLL函數一般有兩種函數,內部函數(internal)和導出函數(export).在實際情況下,許多DLL 調用了其他DLL里面的函數,因此
DLL可以同時有導入和導出函數。
???? DLL 包含有一個導出函數表,可以通過函數的符號化的名字和稱為序號的正書來識別這些函數,函數表中包含了函數在dll內部的
地址。在動態鏈接進程好建立一張表,把客戶的調用與dll里的函數的地址連接起來。
??? double dbValue(value);//內部函數
??? double dbValue(value);//內部函數
??? extern"c" _declspec(dllexpoert) double changeValue(double,bool);//外部函數
?? double dblValue(double value)
?? {
?????? return value*vlaue;
?? }
?? double changeValue(double value,bool whichOp)
? {
???? return whichOp?doublValue(value):halfValue(value);
?? }
? 如果我們希望dll可以用于其他語言或不同版本的c++ 需要在聲明導出函數的時候加上extern "C."
?4 隱式的鏈接和顯示的鏈接
?? 隱式鏈接(前面所講)在創建dll 的時候,鏈接器產生一個lib 文件把拿獲了dll中的導出符號和序號
?? 顯示鏈接調用Win32 的LoadLibrary 函數,使用完后,調用 FreeLibrary.
?5 查找dll
?? 依次順序為包含exe 的目錄,進程的當前目錄,Windows 目錄,path 環境變量里列出的目錄。
?6 創建動態鏈接庫
?? (如何查看dll 文件的定義)
?7 導出函數:extern "C" __declspec(dllexport) ExportType FunctionName(Parameter)
??? extern "C" __declspec(dllimport) __stdcall void CreateFromFunct();
??? extern "C" __declspec(dllexport) __stdcall void CreateFromFunct();//導出函數
??? 導出類:class __declspec(dllexport) ExportType ClassName{...}
??? class __declspec(dllexport) __stdcall MyDllClass { //導出類
???? public:
??? MyDllClass();
??? void CreateAForm();
??? TDllFrm* DllMyForm;
??? };
???? __declspec(dllimport) class __stdcall MyDllClass {
?????? public:
?????? MyDllClass();
?????? void CreateAForm();
?????? TDllFrm* DllMyForm;
??? };
??? 靜態調用,build 生成dll 文件和lib 文件,并把lib 文件導入到工程中
???? void __fastcall TForm1::Button1Click(TObject *Sender)
??? { // 導出類實現,導出類只能使用靜態方式調用
?????? DllClass = new MyDllClass();
?????? DllClass->CreateAForm();??
??? }
??? void __fastcall TForm1::Button2Click(TObject *Sender)
??? { // 導出函數實現
?????? CreateFromFunct();
???? }
???? void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
??? {
???????? delete DllClass;
???? }
?? 動態調用
?? class TForm1 : public TForm
? {
???????? ...
????? private: // User declarations
????? void (__stdcall *CreateFromFunct)();
?????????? ...
?? }
? HINSTANCE DLLInst = NULL;
? void __fastcall TForm1::Button2Click(TObject *Sender)
? {??
???? if( NULL == DLLInst ) DLLInst = LoadLibrary("DLL.dll"); //上面的 Dll
???? if (DLLInst) {
???????????? CreateFromFunct = (void (__stdcall*)()) GetProcAddress(DLLInst,
????????????????????????? "CreateFromFunct");
????? if (CreateFromFunct) CreateFromFunct();
????? else ShowMessage("Could not obtain function pointer");
?? }
????? else ShowMessage("Could not load DLL.dll");
? }
?? void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
? {
???? if ( DLLInst ) FreeLibrary (DLLInst);
? }
8? bcb 調用vc 編寫的DLL
? 1) 名字分解
?? 1. 名字分解:
? 沒有名字分解的函數
??? TestFunction1 // __cdecl calling convention
??? @TestFunction2 // __fastcall calling convention
??? TESTFUNCTION3 // __pascal calling convention
??? TestFunction4 // __stdcall calling convention
? 有名字分解的函數
??? @TestFunction1$QV // __cdecl calling convention
??? @TestFunction2$qv // __fastcall calling convention
??? TESTFUNCTION3$qqrv // __apscal calling convention
??? @TestFunction4$qqrv // __stdcall calling convention
? 使用 extern "C" 不會分解函數名
?? 2)
????? __cdecl 缺省
?? 是 Borland C++ 的缺省的 C 格式命名約定,它在標識符前加一下劃線,以保留
? 它原來所有的全程標識符。參數按最右邊參數優先的原則傳遞給棧,然后清棧。
??? extaern "C" bool __cdecl TestFunction();
?? 在 def 文件中顯示為
??? TestFunction @1
?? 注釋: @1 表示函數的順序數,將在“使用別名”時使用。
? __pascal Pascal格式
?? 這時函數名全部變成大寫,第一個參數先壓棧,然后清棧。
??? TESTFUNCTION @1 //def file
? __stdcall 標準調用
?? 最后一個參數先壓棧,然后清棧。
??? TestFunction @1 //def file
? __fastcall 把參數傳遞給寄存器
?? 第一個參數先壓棧,然后清棧。
??? @TestFunction @1 //def file
? 3)
?? 3. 解決調用約定:
?? Microsoft 與 Borland 的 __stdcall 之間的區別是命名方式。 Borland 采用
? __stdcall 的方式去掉了名字起前的下劃線。 Microsoft 則是在前加上下劃線,在
? 后加上 @ ,再后跟為棧保留的字節數。字節數取決于參數在棧所占的空間。每一個
? 參數都舍入為 4 的倍數加起來。這種 Miocrosoft 的 DLL 與系統的 DLL 不一樣。
?4 查看dll 的調用接口tdump -ee MyDll.dll >1.txt (查看 1.txt 文件即可)
5 編輯c++ builder build 為一個可以直接調用的 .exe 。 點擊project option? 中linker 標簽 去掉user dynamic RTL 選項 和package 中 去掉builder with runtime package 選項.
6 調用代參數的vl 編寫的dll 調用的實例。
int (__cdecl *fun)(char*,char*,char*);
HINSTANCE DLLInst = NULL;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if( NULL == DLLInst )
{
DLLInst = LoadLibrary("HollyIVRCard.dll");
}
if(DLLInst)
{
? fun=(int (__cdecl*)(char*,char*,char*))GetProcAddress(DLLInst,"hTrade");
? if(fun)
? {
??? cardid=Edit1->Text.c_str();
??? num=Edit2->Text.c_str();
??? ShowMessage(fun(cardid,num,res));
??? //cout<<cardid<<endl;
??? //cout<<num<<endl;
? }
} else{
?? ShowMessage("load dll fail");
}
?? ShowMessage(AnsiString(res));
}
Containing your beans
??? Tere i sno single Spring container.Spring actually comes with two distince types of containers:Bean factories and Application
contexts.Beyong there two basic types of contains .Srping come with sereral implementss of BeanFacotory and ApplicationContext.
? 1 introducing the BeanFactory
? There are several implementations of BeanFactory in Spring .But the most userful one is XmlBeanFactory,whick loads
its bean based on the definitions contained in an xml file.
? Creat a XmlBeanFactory? BeanFactory factory=new XMLBeanFactory(new FileInputStream("beans.xml"));
?But at that time ,the BeanFactory does not initialize the bean ,it is loaded lazliy.
? Get the Bean :MyBean myBean=(MyBean)factory.getBean("myBean");
?When getBean() is called ,the factory will instantiate the bean and being setting the bean using dependency injection.
? 2 Working with an application context
?an ApplicationContextis prefered over a BeanFactory in nearly all application ,the only time you might consider using a BeanFactory
are in circumtance where resource s are scarce.
?? Amony the many implements of ApplicationContext are three that are commonly used:
????? ClassPathXmlApplicationContext,FileSystemXmpApplicationContext,XmlWebApplicationContext.
?? ApplicationContext context=new ClassPathXmlApplicationContext("foo.xml");
?wiring the beans
? <beans>
??? <bean id="foo" class="com.springinaction.Foo" />
?</beans>
?Prototyping vs.singleton
?By default ,all Spring beans are singletons.When the container dispenses a bean it will always give the exact same instance of the????
In this case ,you would want to define a prototype bean .Defining a prototype means that instead of defining a single bean.
???? <bean id="foo" class="com.springinaction.Foo"? single/>on="false" />
Initialization and destruction
?? <bean id="foo" class="com.springinaction.Foo" init-method="setup" destory-method="teardown">
Injectiong dependencies via setter method
?? <bean id="foo" class="com.srpinginaction.Foo" >
??????? <property name="name">Foo McFoo</value>
?? </bean>
?Referencing other beans
?? <bean id="foo" class="com.springinaction.Foo">
???? <property name="bar" >
??????? <ref bean="bar" />
???? </property>
??? </bean>
??? <bean id="bar" colass="com.srpinginaction.Bar" />
?Inner beans
?? <bean id="courseService"
???????????? class="com.CourseWericeImpl">
???? <property nanme="studentService">
???????? <bean
????????????? class="com....." />
????? </property>
?? </bean>
? Wiring collections
??? 1Wiring lists and arrays? java.util.List
????? <property name="barList">
?????????? <list>
??????????????? <value>bar1</value>
??????????????? <ref bean="bar2"/>
?????????? </lsit>
????? </property>
?? 2 Wiring set? java.tuil.Set
??? <property name="barSet">
????????? <set>
???????????????? <value>bar1</value>
???????????????? <ref bean="bar2" />
????????? </set>
?? </property>
?? 3 Wiring maps java.util.Map
?? <property name="barMap">
??????? <ebtry key="key1">
??????? <value>bar1</value>
?? </property>
? 4 Wiring propertyies
??? <property name="barProps">
???????? <props>
??????????? <prop key="key1">bar1</prop>
??????????? <prop key="key2">bar2</prop>
???????? </props>
??? </property>
? 5 Setting null values
?? <property name="foo"><null/><property>
?? injecting dependencies via constructor
?? <id="foo" class="com.springinaction.Foo">
??? <constructor-arg>
??????? <value>42<value>( <ref bean="bar">)
???? </constructor-arg>
??? <bean id="foo" class="com.springinaction.Foo">
?????? <constructor-arg>
??????????? <value>http://www.manning.com</value>
?????? </constructor-arg>
? <constructor-arg>
??????????? <value>http://www.manning.com</value>
?????? </constructor-arg>
?? </bean>
?? 代碼會生銹嗎?這真是一個很奇怪的問題,代碼怎么會生銹呢?但是現在的許多軟件企業,卻總認為代碼放久了就會發霉,會生銹,因此每次發布的新版本總拋棄了原來所有的代碼從頭來過。這種做法真的可取嗎?我們且不說這么做要浪費多少人力物力(反正公司有的是錢 really?但為什么工資只開這么一點點,配的電腦也這么爛),就僅僅從新版本的質量來講,也不見得盡如人意。誰能夠保證新版本的核心人員與原來版本是同一批人,那么又怎么來保證原來的“經驗積累”能夠在新版本中發揮作用。另外,老版本的代碼多是經過項目實踐來檢驗的,它們身上可能帶著修復bug后,留下的傷疤,但是至少它已經痊愈
了,他已經成為了一名經歷過戰場洗禮的戰士。而新版本呢,好比是在軍校學習的學生,它們可進行了更為先進的戰略戰術的學習(一些更先進的技術)但是,遺憾的是他們從來沒有在戰場上真槍實彈的打過仗,(在項目中許多新技術的應用往往是程序員邊學邊用的,當然,這也是軟件行業的一個特點)因此能否成為合格的戰士還需要經過實戰(項目)的考驗,而不僅僅是考試(測試人員)的成績。如果我們把一些戰斗經驗豐富的老戰士,進一步培訓(對老版本進行修復,重構)我想他們的戰斗力可能會遠遠超過這些新兵?
?? 但是為什么這么多的企業,都會不約而同的選擇重新編寫代碼呢,我想很可能是那些程序員在作怪(呵呵,不好意思我也是一個程序員,在這里只是就事論事 不敢含有任何貶低咱程序員的意思)。程序員總是不停的在抱怨,原來的代碼事如何如何的亂,幾頁的代碼竟然沒有任何注釋,許許多多的代碼我竟然不知道做什么用的,讓我修改,我還不如重寫一遍呢?這是發生在程序員修改別人寫的代碼時,時常會發的牢騷。 原來的代碼真的真么糟嗎,其實并不盡然。那寫你看起來一團糟的代碼,也許就是修改某個
bug 時留下的傷疤,如果從頭寫一段新的代碼,誰能保證你的代碼沒有原來那bug呢?其實我們可以采用很多重構的方法來解決,如設計模式的開閉原則,就可以很好的規避這一類問題。
? 因此我認為,一個企業不要總是頻繁的發布新版本,只有可以明確的指出現有版本已經滿足不了市場的需求了,我們才需要重新規劃。我們需要明確,我們當前最需要做的是對現有版本修修補補,使之不斷完善,不斷健壯。君不見,網景的netscape 和borland 的dbasde就是前車之鑒嗎?
?
?? 注:網景的netscape 因新版本重寫代碼,整整用了3年的時間,其市場份額從80% 降到了20%
?????? borland 從些Arago(dbase的前身) 也把市場白白的讓給了 access
? 現在各行各業都興采用什么,矩陣管理的方式,號稱可以提高效率,但是在軟件行業一定適用嗎?
? 據說,微軟采取的就是這種矩陣管理的方式,每一個項目由技術人員,項目經理,測試人員三個
部門的人員組成。其中,項目經理負責項目的協調。看起來,是人盡其用,每個人只需完成它分內的工作
即可,但是在一個項目中,涉及到大量的溝通(正式的和非正式的,內部的和外部的),和協調。如果項目組成員僅僅完成自己分內的工作,而把剩下的工作全部交給項目經理去完成,那么工作效率之低就可想而知了。這理涉及到一個問題,如何提高項目組成員的工作主動性,主動去完成自己分外的,但是自己最合適的工作呢?當然可以提高員工的工資,但是人的欲望是沒有止境的,長到多少合適呢,在說公司能夠承受多少呢?
?? 可見,漲工資不是解決問題的根本方法。因此我們需要想一些別的辦法。
?? 在矩陣管理的模式下,由于每個人不隸屬于任何的項目,只隸屬于自己的部門。因此,在項目經理
與組員進行溝通時,也仿佛在與其他部門進行交互一樣,存在這推諉,敷衍等等許多問題。如何解決這
個問題呢,那就是讓每個項目組成員都要與這個項目榮辱與共。這恰恰就是矩陣管理存在的最大問題。
? 在矩陣管理中,當項目組成員完成一步份工作后,可能就會撤出這個項目,因此這個組員也不會全
身心的投入項目的開發,因為它還要想著下一個項目。項目經理常常掛在嘴邊的一句話,我的項目
怎么怎么樣了(先不管這樣說會讓其它項目組成員心里怎么想),但是很少有項目組成員會說我的項目。。。。,這是為什么呢。因為不管嘴里怎么說,項目組成員在內心深處就沒有把他當作自己的項目
他只需要完成自己的工作就可以了,沒有必要作一些額外的工作,不在其位,不謀其政嗎?這里所提到的
是矩陣管理存在的一些共性的問題,對軟軟件小組進行矩陣管理存在的問題更大了。眾所周知,軟件開發
是一種創造性的工作,其工作主動性所產生的作用更遠遠大于其它行業。在《人件》認為軟件工程的管理
其實就是對人的管理,一切管理都要以人為核心。但是某些領導,往往忽視了這一點,把軟件人員當作一種可以重復利用的資源,結果吃虧的將會是項目本身。
????? 也許大家會說了,你是不當家不知柴米貴,不作領導你不知領導的難處,在這里發發牢騷誰都會,如果你是領導,你會怎么辦呢?
? 當然了,作為非領導的我,只是從我的角度講了一下我對這個問題的看法,另外我也不只是在這里
夸夸其談,我呢,在下面也闡述一下,如果是我,我會如何管理,希望某個領導看到后也也考慮考慮。
?? 1 軟件小組依然存在,但是其作用已經發生的變化。首先軟件小組對已經進入項目的小組成員不能
進行工作的安排。只能對在項目之外的軟件人員進行工作安排。其次,軟件小組需要擔付起對新入職的
軟件人員進行培訓的工作,不能把培訓大量的工作放到真實項目中,這樣必然會降低項目的質量。再次
軟件小組,對項目中一些通用的問題集中解決,對項目中的疑難問題提供技術支持。另外軟件小組還需要
對小組成員代碼的質量進行走查,提高軟件小組的成員的技能。當然作為軟件小組成員的行政單位,軟件小組成員的考核還是要有軟件小組主導的。
?? 2 軟件人員,盡能的參與項目真個生命周期,項目緊張人緊張,項目不緊張人員可以稍微放松,或及時進行充電。忽略了軟件人員的對不斷學習的需求,是當前軟件管理的一大弊病。(可能有些上崗上線了,但這對調動軟件人員的積極性和保持相對穩定的團隊有這意想不到的作用)
?? 3 項目經理,需要對軟件開發的特點,和軟件人員的管理有所了解,建議讀一讀《人件》這本書。當然
??? 提高項目經理本身的軟件素養才能根本解決問題,建議軟件項目的項目經理一定要具備較強的開發能力和較為豐富的開發經驗,公司不要心疼一點點工資,它可以給公司帶來的效益要遠遠高于此。
?? 4 項目經理不要說,“我的項目”,應該改成“我們的項目”,讓項目組的每個成員由一種歸屬感。
作為一個項目組成員,我們需要的是一種經歷風雨見彩虹的成功感,而不是一種機械的忙碌的工作。
?? 5 最后,也是每一個軟件人員都十分關心的問題,什么時候給我們換一個快點的機器呀。一個好一點的機器
比可能比更高一點的工資更具備吸引力,對公司來講也是更劃算的。(效率和人員流動上,呵呵)
?
??
?1 寫出用戶級的需求用例。在現在的需求調研中,更多的是把客戶提出的需求用流程圖的方式表達。但是
在給客戶講解的時候效果不是很好,主要存在以下問題:
?? 1)流程圖不適合描述分支很多的流程。分支過多,將導致流程圖十分復雜,不具有可讀性。
?? 2)流程圖的受眾比較少。在實際的業務業務調研中,面對的用戶往往是系統的使用者,而不是作為
技術人員的維護和開發者,由于不具備相關技術背景,因此這些人在閱讀流程圖的時候,往往覺的眼花繚亂,更不要說提什么修改建議了。這將會使很多本應在需求調研階段發現的問題,遺留到了用戶正式使用的時候,由此帶來的損失不可估量。
?? 3)由于流程圖是一個粗線條的流程描述,因此許多客戶提出的細節問題,需要以另外的方式進行記錄
這可能會導致在設計階段遺漏掉。如 系統使用這在使用時的心態,等等
? 2 采用用例和流程的結合形式來描述客戶的需求,原因如下:
?? 1)由于用例采用自然語言描述,所以任何人可以輕松的進行閱讀。
?? 2)在一定格式的輔助下,用例描述不必受流程分支多少的約束。
?? 3)采用自然語言描述,可以詳細的描述處用戶的使用細節,這些細節可能會對這個項目的開發起著
?? 決定作用。
?? 4)加入流程圖,讓具有相關背景知識的人迅速的了整個流程的全貌。如果分支不是很多流程圖
?? 還是可以畫的十分簡潔易懂的。
?? 5)好的用例對后期的設計,開發,測試都是很有幫助的。甚至可以直接作為客戶的培訓素材
? 3 如何寫用例
?? 1)寫用例需要結合用例圖,用例圖可以讓用例的讀者了解整個系統提供的功能,和與其他系統的關系。?這樣可以使讀者的在閱讀用例時,在一個限定的范圍內思考問題。
?? 2)用例的格式大體上可以分為前提條件,主成功用例,擴展。當然,作者可以豐富用例的格式,這里
?? 僅僅是一個最簡單的框架。
?? 3)由于是在需要階段的用例,因此用例盡量用輕松的語調來寫用例。你甚至可以把用例當作一片散文來寫。這里需要注意的是,在寫用戶級用例的時候需要把系統當作一個黑盒,不要去描述系統內部是如何工作的
?? 此時作者一定要以一個客戶的角度來考慮問題,來描述系統。
?? 4)用例可以也可以想寫函數是的寫一些通用性比較強的模塊,以便其他用例可以復用。如用戶身份驗證模塊。
?? 5)在寫用戶級的用例時候,對用戶使用系統的細節需要描述,如使用者的心態。這可能決定著用戶易用度的設計。
?? 6)在寫用例的時候一定需要用完整的主謂賓來寫。及誰,在做什么
?4 用例描述僅僅是對用戶需求一個梳理的過程,我們還需分析出其中的主要實體,和他們的關系,在分析
? 的過程中可能會對產生新的疑惑,可以和客戶及時溝通。當然在設計的過程中,也可以繼續和客戶溝通
? 但是,客戶方不一定能夠隨時協調到合適人員,這將導致項目的推后。因此,在集中討論期間
? 多做一些分析,乃至設計(原型的設計),可以大大減少后期的工作了量,提高客戶滿意度。用例,分析,和原型 可以是在需求期間一個小的迭代周期。
?5 在討論需求的時候,最好是以集中會議的形式進行,參會者應為 相關領導,系統將來的實際使用者
? ,技術把關人員。為什么者樣作,和如何利用其中的微妙關系,需要大家自己去體會,去琢磨。呵呵
??? 用了幾年java 了,突然想學習c++ ,昨天用了一天的時間瘋狂的學習了一天c++ 基礎知識,發現感覺還不錯,不過精驗告訴我,學編程語言一定要實踐,在這里指記錄了一些學習中的點滴。
? 1 const 與volatile 的用法
? 1 const
?? #include<conio.h>
?? #include<iostream.h>
?? //行參數指向const 類型變量的指針
?? void display_c(cons int * pi)
?? {
???? cout<<"display_c:"<<*pi<<endl;
?? }
?? //行參為普通類型變量的指針
?? void display(int *pi)
?? {
???? cout<<"display_c:"<<*pi<<endl;
?? }
?? //const 類型變量
?? const int i1=1;
?? //error? i1=3;
?? //const 類型變量的指針
?? int i2=2;
?? const int * pi2=&i2;
?? //error *pi2=3
?? // const 指針
?? int * const pi3=&i3;
?? *pi3=5;
?? // error *pi3=&i4? 可以賦予不同的值,但是不可一指向不同的變量
?? //指向const 內容的const 指針
?? const int * cosnt pi5=&i5;
2 sizeof 返回某個便賴寧嘎或數據類型的長度
3 引用
?1 引用變量
? int i=1;
? int & r_i=i;
5 名空間
? 1 namespace
?? namespace car
? {
??? int model;
??? int length;
??? int width;
? }
? namespace plane
? {
??? int model;
??? namespace size
??? {
????? int lenth;
????? int width;
??? }
? }
? namespace car //添加名空間的成員
? {
??? char * name;
? }
? namespqce c=car; //定義別名
? int Time //外不變量屬于全局名空間
?
? car::length=3;
? plane::size::length=70;
?
? int Time=1996;
? ::Time=1997;
?2 using
? void main()
?{
?? using namespace car;
?? length;
?? using namespace phane;
?? model;
?}
6 new 與delete 運算符
?double * pd;?? // define pointer variable
?pd=new double; // allocate memory
?if(pd!=null)
?{
?? ...
? delete pd;? // free memory
?}
?double1=null
?* pds=new double[100];
?if(pds)
?{
?? ....
? delete[] pds;
?}
?如果是使用new 進行內存空間分配沒有成功,則返回空指針null
?釋放一個數組分配的內存是,常常采用帶[]的形式
7 void 指針 它指向一個地址值,但是不說名數據類型,可以使用void 指針創建一個通用的函數,在使用
?的時候將指針類型轉化成相應的具體類型。
?void ShowHex(void *pv,int siae)
{
?....
?((unsigned char *)pv)[i]
}
void main()
{
? void *pv=null;
? unsigned char c1=0x12;
? pv=&c1;
? ShowHex(pv,sizeof(c1));
?}
9 typeid 運算符 用來求得變量或隊形愛女嘎的數據類型,返回一個type_info 類型的對象,通過該對象
? 可以獲取數據類型的名稱
? include<typeinfo.h>
? int i;
? const type_info &t0=typeid(i);
? t0.name();
10 函數
?? 1 模板函數
? template<class T> T min(R &x,Tv&y)
?{
?? return x<y?x:y;
?}
? 2 指向函數的指針
?? int max(int x,int y)
? {
??? ...
? }
? int min(int x,int y)
? {
???? ...
? }
? int (* m_pfunction)(int,int);
? void main()
? {
??? m_pfunction=max;
??? (*m_pfunction)(2,3);
???? m_pfunction=min;
??? (*m_pfunction)(2,3);
? }
11 類與對象
?1 構在函數和析構函數
?#include <iostream.h>
#include <string.h>
#include <conio.h>
class Book
{
private:
? char * pBookName;
public:
? int PageNum;
public:
? Book(char * pBN=NULL);
? ~ B o o k ( v o i d ) ;
? void SetBookName(char * pBN);
? int GetBookName(char * pBN,unsigned int MaxSize);
} ;
Book:Book(char *PBN)
{
? cout<<"構造函數"<<endl;
? pBookName=null;
? if(oBN!=null)
? {
???? pBookName=new char[strlen(pBN)+1];
???? if(pBookName!=null)
???????? strcyp(pBookName,pBN);
? }
}
Book::~Book()
{
? delete[] pBookName;
}
void Book::SetBookName(char * pBN)
{
? if(pBookName!=null)
??? delete[] pBookName;
?? pBookName=new char[strlen(pBN)+1];
???? if(pBookName!=null)
???????? strcyp(pBookName,pBN);
}
int Book::GetBookName(char * pBN,unsigned intmaxSize)
{
?if((pBookName!=null))&&(MaxSize>strlen(pBookName))
{
? strcpy(pBN,pBookName);
? retrun strlen(strlen(pBookName));
?}
}
// 使用
Book b1;
b1.SetBookName("test");
Book b2(test1);
?2 對象的引用參數傳遞
? void Add(Book b)
? void AddRef(Book & b);
?3 靜態成員變量 是一個公共變量
?在初始化 的時候利用作用運算符:: 對私有類型的靜態成員變量可以向公有類型的靜態成變量一樣賦值
?但不能直接引用
?3 const 類型成員函數與mutable
?class CDate
?{
?? public:
??? int year;
??? mutable int month;
??? CDate(int y=2000,int m=1)
??? {
?????? year=y;
?????? month=m;
??? };
??? int BetMonth() const ;//read only
??? void SetMonth(int m);// write only
?}
? void CDate::SetMonth(int m)
?{
?? month=m;
?}
?void main()
?{
?? CDate d1;
??
?}
?在const 類型的成員函數定義中,不可一直接或簡介的修改普通類型的成員變量
?如果象修改const 類型對象的成員函數,可以使用關鍵字mutable 對該成員變量進行修改
?5 對象的初始化與初始化行
?將參數類表中的數值賦予成員變量時,不僅可以在構造函數的函數體中進行,以阿寬衣在初始化行中進行
?在初始化處驚醒初始化的情況有:
? 1 分層類的構在函數可以調用它的任何一個子對象的構造函數
? 2 對常量const 類型的成員變量的初始化必須在初始化行上
? 3 對于引用類型的成員變量的初始化必須在初始化行上
?
?class CPoint
?{
?? public:
??? int x,y;
??? CPoint(int ax=0,int ay=0)
??? {
????? x=ax;
????? y=ay;
??? }
?};
?class CRect
?{
?? private:
??? CPoint low_right;
??? CPoint up_left;
?? public:
??? int & CenterX;
??? const int CenterY;
??? CRect(int x1,int y1,int x2,int y2,int &x3,int y3)
???? :up_left(x1,y1),low_right(x2,y2),CenterX(x3),CenterY(y3)
?? {
????
?? }
?};
void main()
{
? int cx=5;
? int cy=6;
? CRect r1(1,2,3,4,cx,cy);
}
?6 拷貝構造函數
? 拷貝構造函數與普通構造函數的差別在與棋參數類表,參數列表中有一個對象,該對象的數據類型是
?本類的一個引用,而且一般情況下,該對象還應該是一個const 類型的對象。
? 如果在類的定義是不是顯示的定義一個拷貝函數,則編譯器會自動的定義一個拷貝構造函數
?
?class CPoint
?{
?? public:
???? int x,y;
???? CPoint(int m_x=0,ubt m_y=0);?? // default constructor?
???? cPoint(const CPoint &c);????? //copy consrucotr
?};
?CPoint::CPoint(int m_x,int m_y)
?{
??
?}
?
?CPoint::CPoint(const CPoint &c)
?{
?? x=c.y;
?? y=c.x;
?}
?void main()
?{
?? CPoint c1(1,2);? //invoke default constructor
?? CPoint c2-c1;??? // invoke copy constructor
?}
7 template class
?template<class t,int Size>class Array // template class
?{
?? private:
???? T arr[Size];
???? int CurSize;
?? public:
??? Array(T * date,int n)
??? {
????? CurSize=n<Size?n;Size;
????? for(int i=0;i<CurSize;i++)
????? {
??????? Arr[i]=data[i];?
????? }
??? }
}
void main()
{
? int i1[10]={1,2,3,4,5,6,7,8,9};
? Array<int,6>array_i1(i1,i0);
}
1 友員類和友員函數
? #include <iostream.h>
? #include<string.h>
? #include<conio.h>
class SoftWareEngineer; //先對SoftwareEngineer 類進行顯示說明一下
class Computer // 定義Computer 類
{
?? private:
???? int Price;
?? public:
???? Computer(int p){Price=p};
??? friend class SoftwareEngineer; //將友員類載這里聲明
??? frined void Judge(Computer &c,SoftwareEngineer? & s) //友員函數
???
};
class SoftwareEngineer
{
?? int Salary;
?? char Name[20];
? public:
??? SoftwareEngineer(int s,char *n //構造函數)
? {
???? Salary=s;
???? strcpy(Name,n);
? }
int GetComputerPrice(Computer &c){return c.Price} //訪問Computer 的思友變量
friend void Judge(Computer &c,SoftwareEngineer & s) //友員函數
};
//判斷一個軟件工程師是否可以用一個月的薪水買的器一臺電腦
void Judge(Computer &c,SoftwareEngineer &s) //橋友員函數
{
?? if(c.Price>s.Salary)
???? cout<<"軟件工程師"<<s.Name<<"的一個月薪水買不起一臺電腦"<<endl;
?? else
???? cout<<"軟件工程師"<<s.Name<<"的一月薪水買的起一臺電腦"<<endl;
}
void main()
{
? Computer c1(100);
SoftwareEngineer s1(120,"user1");
Judge(c1,s1);
SiftwareEngineer s2(80,"user2")
Judge(c1,s2);
getch();
}
2運算符重載
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class TValue{
?private:
???? int value;
?public:
?? TValue(int i){value=i}
?//成員函數重載運算符
? TValue operator!();
? TValue operator+(TValue & rv);
?// 友員函數重載運算符
? friend ostream & operator<<{ostream & os,TValue & rv};
}
TValue Tvalue::operator!()
{
? return TValue(!value);
}
TValue TValue:operator+(TValue& rv)
{
? return TValue(value+rv.value);
}
ostream & operator<<(ostream & os,TValue rv)
{
?? os<<setw(5)<<rv.value;
? return os;
}
void main()
{
?? TValue v1(3),v2(5);
?? cout<<
}
3 類的派生和繼承
1class Box{
?? public:
???? int width,height;
???? void SetWidth(int w){width=w};
???? void SetWidth(int h){height=h;};
?};
?class TColorBox::public Box{
? public:
???? int color;
???? void SetColor(int c){color=c;};
?};
void main(){
??? TColorBox cb;
??? cb.SetColor(255); //聲明非繼承類的對象
??? cb.SetWidth(100);//聲明繼承類的對象
??? cb.SetHeight(100); //聲明繼承類的對象
??
? }
?2 不能被繼承的成員
? 構造函數,析構函數,用戶定義的新操作符,用戶定義的賦值操作,友員關系
?3 構造函數,析構函數的調用順序
class A{
?int a,b,c;
?public:
? A(int x,int y,int z)
{
?? a=x;
?? b=y;
? c=z;
}
};
class B{
? int d;
? public :
?? B(int xx):A(xx,xx+1,xx+2)(d=xx);//內聯構造函數
?? B(int x,int y,int z,int xx);//非內聯構造函數
? B:B(int x,int y,int z,int xx):A(x,y,z)
{
?? d=xx;
}
}
?
實例
class Currency
{
? poublic:
??? double par_value;
??? Currency(){per_value=0.0;}
??? Currency(double d){per_value=d;}
??? Currency(Currency &rc){par_value=rc.par_value;}
??
?? Currency & operator={Currency & c}
?? {
????? par_valuc=c.par_value;
????? return * this;
?? }
?? Currency & operator+(Currency & c)
? {
???? par_value+=c.par_value;
??? return *this;
? }
}
//人民幣
?class RMB:public Currency
?{
?? public:
?? RMB():Currency(){};//調用派生類的構造函數前,需要調用器基類的工造函數
?? RMB(double d):Currency(d){};
?? RMB(Currency& c):Currency(c){};
?? RMB(RMB& rmb):Currency(rnb){};
?? friend ostream& operator<<{ostream& os,RMB& rnb};//派生類的附加功能
?};
?ostream& operator<<{ostream& os, RMB& rmb} //output 運算符不能是一個類的成員函數
{
? os<<"¥"<<setiosflags(ios::shwopoint|ios:fixed)<<setprecision(2)rmb.par_value;
?return os;
}
void main()
{
?? RMB r_income(5000);
?? RMB r_balance;
?? r_balance=r_income=r_expense;
? cout<<"income"<<r_income<<endl;
}
4 將iostream 運算符重載
?1)ostream & operator<< (ostream &os,const Triangular & ths)
{
?? os<<"("<<rhs.beg_pos()<<","<<rhs.length()<<")";
? rhs.display(rhs.length(),rhs.beg_pos(),os);
}
ouutput 運算符不能夠設計成member function
?2)istream& operator>>(istream &is,Triangular &rhs)
?{
??? char ch1,ch2;
??? int bp,len;
?? //輸入 ch1='(',bp=3,ch3=',' len=6
?? is>>ch1>>bp>>ch2>>len;
?? rhs.beg_pos(bp);
?? rhs.length(len);
?? rhs.next_reset();
?? return is;
?}
Triangular tris;
cin>>tri2
4 虛基類
載繼承關系中,同一基類被繼承多次,不僅會引器歧異,而起可能浪費空間
?class A
{
?? public:
???? int value;
};
class B:public virtual A(); //虛基類 編譯器只產生一個基類版本。如果不定義為virtual 編譯器不知到調用那個value 了,當然
class C:public virtual A();//? 也可以return B::value;
class D:public b.public c
{
? public
?? int GetValue(){return value;}
};
void main()
{
? D dd;
? dd.GetValue();
}
5 多態形,和虛函數
class TFirst
{
?? public virtual void Display();
};
void TFirst::Display()
{
?? cout<<"first "
}
class TSecond:public TFirst
{
?? public:
???? virtual void Display();
};
void TSecond::Display()
{
?? cout<<"second"
}
void Display(TRist * pFirst)
{
?? pFisrt=>Display();
}
void main()
{
?? TFirst * pFirst=new TFirst;
?? TSecond * pSecond=new TSecond;
?? pFirst->Display();
?? pSecond->Display();
?? Display(pFirst);
?? Display(pSecond);
??? delet pfirst ;
??? delet pSecond;
??
??? getch();
}
c++ builder 中的集合的
? 1 集合的概念基本
?? Set<type,minval,maxval>
?? Set<char,'A','C'> s1
??
?? tydefef Set(char,1,255) UPPERCASet;
?? UPPERCASESet s1;
?? s1<<'A'<<'B'<<'C';
?? sysset.h 直接包含載vcl.h 中
? 2 集合的操作
?? #include<iostream>
?? #include<system.hpp>
?? #include<conio.h>
??
?? using namespace std;
? typedef Set<char,'B','Z'> UpperSet;
? typedef Set<char,'a','z'> LoverSet;
? typeder Set<char,'a','j'> HalfLowerSet;
? void set_example()
? {
????? LowerSet ae,ae2,ac,de;
????? UpperSet AE,AE2,AC,DE;
???? HalfLowerSet aj;
? }
異常處理
1 c++ 的異常處理
?#include <iostream.h>
?#include <conio.h>
?class Crange
?{
??? public:
????? int index;
????? CRange(int i){index=i;}
}
?? class CString
?? {
??????? char a[100];
??????? int len;
??????? public:
???????? CString(char * s)
??????? {
???????????? Len=strlen(s);
???????????? strcpy(a,s);
??????? }?
?????? char & operator[](int i)
????? {
?????????? if(i>0 && i<len) return a[i];
?????????? else throw CRange(i);
????? }
????
????? void DisplayNoCatch(CString & s)
???? {
???????????? int j;
???????????? for(j=0lj<20;j++)
?????????????? cout<<s[j]<<""endl;;
???? }
???
???? int DisplayHasCatch(CString & s)
??? {
??????? try{
?????????????? DisplayNoCatch(s);
??????? }catch(CRange r)
??????? {
??????????????? cerr<<r.index<<endl;
??????? }
???????? return 99;
??? }
?}
?2 多路捕捉
? #include<iostream.h>
? #include<conio.h>
? void MultiException()
? {
????? int i;
????? double d;
????
????? int no;
????? cout<<"(>0 and <5)";
????? cin>>no;
????? tyr
????? {
???????? switch(no)
??????? {
??????????? case 1;
?????????????? throw 123;
?????????????? break;
???????????? case 2:
?????????????? throw i;
?????????????? break;
???????????? case 3:
?????????????? throw d;
?????????????? break;
???????????? case 4:
?????????????? throw "Error";
??????????????? break;
????????????? default:
??????????????? cout<<"error";
??????? }
????? }
????? catch(int ie)
???? {
????????? cout<<"int"<<endl;
???? }
???? catch(double de)
???? {
???????? cout<<"double"<<endl;
???? }
???? catch(char* se)
??? {
????????? cout<<"char"<<endl;
??? }
? }
?3 bcb中的異常類
?? 1)卻省vcl 異常處理 無try catch 自動處理
???? int i=4,j=0;k;
???? k=i/k;
???? //下一句永遠不會執行
??? ShowMessage(k);
?? 2) 引發vcl 異常類
??? try{
??? }
??? catch(Exception &e)
??? {
????????? ShowMessage(e.,Message);
??? }
?? 不能引發如int double 等簡單類型的異常
?? 3) c++ 異常處理
??? try
??? {
???????? throw 2222;
??? }
??? catch(int ErrorCode)
?? {
???????? ShowMessage(ErrorCode);
??? }
?}
?4 單一捕捉異常
?? try{
????? k=10/0;
?? }catch(EDivByZero & E)
?? {
????? MessageDlg()?
?? }
? 5 捕捉所有異常
?? try
?? {
?? }catch(...) or(EExternalException &E)