将C++ 转为 Pascal/Delphi -- 转
将C++ 转为 Pascal/Delphi
==============================
目录
概论
数据型态
关键词
叙述结尾
变量宣告
字符串
数组
数值的指定与比较
常数宣告
函数与程序
with ... do叙述
批注
流程控制
对象导向结构
Containers
例外处理
资料流(Streaming)
项目档的设计
如何转换
结论
=================================
概论:
这篇文章的目的是让您了解C++与Object Pascal的不同
也让您能够有能力将C++转为Object Pascal(以下称为OP)
=================================
数据型态
这一章可学到如何将 C++ 的数据型态 转成 OP 的数据型态.
变量宣告时可参考下列对照表
C++ OP 大小 (bytes) 值范围
==================================================================
char ---- 1 -128 to 127
---- char 1 1 ASCII character
int integer 2 -32,768 to 32,767
short ---- 2 -32,768 to 32,767
---- shortint 1 -128 to 127
long longint 4 -2,147,483,647 to 2,147,483,647
unsigned char byte 1 0 to 255
unsigned int word 2 0 to 65,535
unsigned short word 2 0 to 65,535
unsigned long ---- 4 0 to 4,294,967,295
float single 4 3.4E-38 TO 3.4E+38
double double 8 1.7E-308 TO 1.7E+308
long double extended 10 3.4E-4932 TO 3.4E+4932
---- comp 8 1.7E-308 TO 1.7E+308
---- real (for backwards compatibility only -- use double)
void pointer 8 n/a -- an untyped pointer
---- boolean 1 True or False
(C++ may soon have a boolean type)
String ---- a C++ standard object
---- string an array of up to 255 ASCII characters
---- PChar pointer to a null-terminated string
=================================================================
=================================
关键词
C++ 有 59 个关键词, OP 有 60 个关键词. This does not include the many vendor
C++ 大小写有分 OP 大小写不分
C++的关键词
asm auto break case catch cdecl char class const const_cast continue
default delete do double dynamic_cast else enum extern far float for
friend goto huge if inline interrupt int near new operator pascal
private
protected public register reinterpret_cast return short signed sizeof
static static_cast struct switch template this throw try typedef
typeid
union unsigned virtual void volatile wchar_t while
OP的关键词
and as asm array begin case class const constructor destructor div do
downto else end except exports file finally for function goto if
implementation
in inherited inline initialization interface is label library mod nil
not
object of or packed procedure program property raise record repeat set
shl
shr string then to try type unit until uses var while with xor
=================================
叙述结尾
C++叙述结尾
大部份的叙述都用 ; 号结尾
有一些例外如:
#include
#define MAXNAMELENGTH 35
OP叙述结尾
所有的叙述都用 ; 号结尾
=================================
变量宣告
C++变量宣告
变量名称只有前32个字有效
变量名称宣告可在程序的任何地方宣告(当然,宣告后才可用)
// ... 如在程序中宣告
{
char i;
for (i=0;i<10;i++)
{
char sName[10]; // 在区块中也可宣告变量
int iCount, iLoop, iValToReach;
double dMaxLoan, dTotal;
float fMaxRate = 123.875;
procedure LoanC.SetNewIntRate;
begin
...
end;
两个语言可以在参数传递时采用 pass by valye 或 pass by reference:
C++ pass by value ... double IntSinceLastAddDate_d(double dAvailCash);
OP pass by value ... function IntSinceLastAddDate_d(dAvailCash:
double): double;
C++ pass by reference ... double IntSinceLastAddDate_d(double
&dAvailCash);
OP pass by reference ... function IntSinceLastAddDate_d(var
dAvailCash: double): double;
C++ pass constant ... double IntSinceLastAddDate_d(const double
dAvailCash);
OP pass constant ... function IntSinceLastAddDate_d(const dAvailCash:
double): double;
=================================
with ... do叙述
C++无 with .. DO叙述
在C++中当您要取用资料时:
poC.oStock.aoTradesLast130Days[0].lVol = 0;
poC.oStock.aoTradesLast130Days[0].dHigh = 0;
poC.oStock.aoTradesLast130Days[0].dLow = 0;
poC.oStock.aoTradesLast130Days[0].dClose = 0;
但在OP中可以简化成:
with poC.oStock.aoTradesLast130Days[0] do
begin
lVol := 0;
dHigh := 0;
dLow := 0;
dClose := 0;
end;
=================================
批注
C++
有两种
// 双斜线后为批注
/* 批注 */
OP
有三种
// 双斜线后为批注
{ 批注 }
(* 批注 *)
=================================
流程控制
有五种流程控制指令,C++与OP都有,用法满像的.
~~~~~~~~~~~~~~~~~~~~~~~~~
1) if ... else 指令
C++
if(<逻辑表达式>) // 一定要有()号
{ ...
~~~~~~~~~~~~~~~~~~~~~~~~~
3) for ... loop 指令
C++
for(iCount = 0; iCount <= 10; iCount++)
{
// iCount++, 每次循环值加一
...
break; // 中断循环
continue; // 跳到下一个循环
...
在OP理,建立者与class不同名.
ie:
constructor MyLoanOne; {no parameters}
constructor MyLoanTwo(var dCurrentBal: double);
constructor MyLoanThree(var dBalOne, dBalTwo: double);
C++与OP都提供解构者(destructors),也可称为释放内存(free memory)
在C++理, 解构者与建构者一样,与class同名.
如:
~LoanC(); // C++ destructor
在OP理, 解构者与class不同名.
如:
destructor LoanC.Destroy;
begin
oLoanDate.Free;
...
inherited Destroy; {"inherited" is a keyword}
end;
建立一个新对象,宣告一个变量:
double dAmount = 1515.75;
LoanC oMyLoan(dAmount);
如此做会配置一块内存,
如果用只标方式可以如下宣告:
double dAmount = 1515.75;
LoanC * poMyLoan = new LoanC(dAmount);
在OP理则不同,每一个都是指标.
var
dAmount: double;
oMyLoan: LoanC;
begin
{oMyLoan does not yet exist!}
dAmount := 1515.75;
oMyLoan := LoanC.MyLoanTwo(dAmount); {now it does}
在OP理也可也 Override.
type
LoanC = class
...
constructor Create; {overrides TObject's Create}
...
end;
... and call the inherited Create constructor in the LoanC
definition...
constructor LoanC.Create;
begin
inherited Create; {calls TObject's Create}
...
end;
C++与OP理引用内部元素采用如下语法:
oMyLoan.dPrincipal;
在C++中,使用指针引用内部元素采用如下语法:
poMyLoan->dPrincipal;
在C++中有三个运操作数:
1. & 取地址
2. * 取指标
3. -> 印用元素
在OP中都用.(逗点号)
=================================
Containers
C++:Containers
typedef TISetAsVector tdCompanySet;
typedef TISetAsVectorIterator tdCSetIter;
...
int OwlMain(int, char*[])
{...
tdCompanySet oCompColl;
...
3) C++的每个if要加 then
如:
C++
if(oE.enDirection == Up &&
oE.uNumMosUpYr >= oE.uNumMosDownYr)
{
...
12) 那多重继承呢?( multiple inheritance).
用Class B继承Class A,再用class C继承 B的方式 .
=================================
结论
C++ 是简短的语言, 而 OP 比较像英文.
然而C++较紧密,但不易阅读.?