Comparison of object Pascal and C object-oriented features
Comparisons Of Object-Oriented Features Between Object Pascal and C
Two rivers
Liangjiang@jxmail.com.cn
?
Abstract: Discussion on Object Pascal and C 's two programming languages, object-oriented features, and make a certain comparison.
Abstract: The Features of the Two Object-Oriented Programming Languages - Object Pascal And C - Are, And Also made Some Contrasts BetWeen THEM.
Keywords: object-oriented? Object Pascal? C
Key Words: Object-oriented? Object Pascal? C
?
I. Introduction
Delphi is an excellent rapid development tool whose core language is Object Pascal. Delphi has a lot of a bit, the language is rigorous, the code is beautiful; the compilation speed is fast, more than other programming tools; powerful database program development capabilities, very much control; fully visualization; at the same time. Delphi's entry is very easy. Many programmers can quickly write differentiated code, but they ignore object-oriented features of Object Pascal. Most of these introductions object-oriented books are used as examples of example language (of course, this stage is also constantly appearing on object-oriented books in two pure object-oriented languages in Java or C #. In fact, object-oriented features of Object Pascal are also very complete.
The so-called object-oriented method is a way to model the real world to focus on computer software, which is an abstraction of the real world. Object-oriented include many concepts: class, instance, message, method, attribute, package, inheritance, polymorphism, and overload. These concepts have corresponding embodiments in Object Pascal and C .
?
Second, the specific comparison
1. Class: Class is a set of objects with the same properties and the same operation. Take a bank account class as an example.
Definition in C (below C ):
Class Taccount
{
?????? private:
????????????? char userpsw [8] ;? // user password
????? protected:
????????????? Double Balance; ???????????? // account balance
?????? public:
????????????? char accountid [20]; ???? // account
????????????? Double getBalance (char * aid, char * UPSW); ????? // get the balance
}
?
Definition in Object Pascal (ourselves Object Pascal):
Taccount = Class
Private
?????? userpsw: string; ?? // user password
protected
?????? balance: real; ????????? // account balance
PUBLIC:
?????? accountid: string; // account
?????? real getBalance (AID: String; UPSW: String); ??????? // Get balance
END;
It can be seen that the definition of the two is basically the same.
?
2. Example: A specific object C : taccount account1;
Object Pascal: Account1: Taccount;
?
3. Message: Communication information transmitted by inter-object interaction
C : Account1.getBalance (A, B);
Object Pascal: Account1.getBalance (A, B);
An example Account1 will perform a getBalance operation after receiving this message.
?
4, method (function): Operation needs to be executed after the response message
Such as getBalance verifies the account, password, and then return the balance implementation code, which is the specific implementation on the program language code, so it does not make specific comparison.
4.1 Constructor, destructor: The constructor is used to assign space to object assignment, which gives its member variables. The destructor is just the opposite, which is used to release space and other sweeping work.
C :
Class Taccount
{
?????? public: ???? // typically constructor, the destructor is defined in this part
?????? taccount (...) {...} // constructor
?????? ~ taccount () {...} ?? // destructor (no parameters), usually not explicitly call
}
?
Object Pascal:
Taccount = Class
public
Constructor create (...); virtual; ???? // constructor (ie constructor)
DESTRUCTOR DESTROY; OVERRIDE; ?????? // Destructor (ie, the tangible function)
END;
In the new object-oriented language such as Java, C #, there is no designer that the sectula is introduced in the Garbage Collection concept.
4.2 Static member functions:
C : The static member function is not a specific object instance, but is a whole class sharing. When calling the function, you need to specify a class.
Class Taccount
{
?????? public:
?????? static char * BankName ();
}
When calling: taccount :: BankName ();
?
Object Pascal: The function is static, static, and the static function is called, and the invocation of the function is determined when compiling.
Taccount = Class
?????? procedure? Draw (Amount: REAL);
END;
Tcreditaccount = Class
?????? procedure? Draw (Amount: REAL);
END;
When calling:
VAR
?????? account1: taccount;
?????? caccount1: tcreditaccount;
Begin
?????? account1: = tcreditaccount.create;
?????? account1.draw (100); ??? // called TACCOUNT DRAW method
?????? tcredtiaccount (account1) .draw (100); ????????????? // called TCREDTICCOUNT DRAW method
?????? account1.destory;
END;
?
5, attribute: The data value defined in the class is also called a member variable. Each instance of the class has its own attribute value. For example, the balance attribute defined in '1, class'.
?
6, package: The specific implementation of the class is to be packaged, and the user can only access the object of the class through the interface provided.
6.1 Access Permissions
C : Provide three access rights
Public public, public members can be accessed by any function in the same scope in the same scope.
Protected and protected members can only be accessed by the class and sub-member functions and friend function;
Private private, private members can only be accessed by the member functions of this class and the friend function of the class.
?
Object Pascal: Also provided three access rights
Public public, public members can be accessed by any part of the program code;
Protected, protection members can only be accessed by units of current class and subclasses and statements;
Private private, private members can only be accessed in units of declared classes.
It can be seen that the range of accessible private members in Object Pascal is larger than C , as long as the same unit of this class can be accessed. In addition, there is no concept of friends in Object Pascal. There is also a fourth access authority Published (published) in Object Pascal. It is similar to public access rights, and it is different that it is runtime information RTTI (Runtime Type Information), RTTI allows the program to dynamically access the field and properties of the object, And determine the entrance of the member function. The attributes and events in the Delphi component are usually published.
6.2 Self-reference pointer: As the name suggestion, it is valid in the category inside the class and is valid in the non-static member function.
C : this-> DRAW (100); ????????????? // DRAW is a member function in the class
Object Pascal: Self.draw (100);
6.3 Friends: A class friend function can directly access the private data of the class. There is this concept in C , there is no Object Pascal, but the foregoing mentioned, private members in the same unit can be accessed each other.
C : ??? Class Taccount; ??????????? // Define forward reference
????????????? Class Tfaccount
{
?????? friend void func (...); ??????????? // friend function
}
Class Taccount
{
?????? friend void func (...); ??????????? // friend function
}
The private member of the class taccount and class TFaccount can be accessed in the FUNC method.
?
7. Inherit: Create new classes based on existing classes, new categories can use some or all of the contents of the existing class, and you can have your own characteristics, which is convenient for the code.
7.1 (single) inheritance
C : ??? Class Tcreditaccount: Public Taccount
...
This will give a Tcredit class from the Taccount class, and the inheritance mode is public. The inheritance mode must be public, private, and the permissions after inheritance are as follows.
right
limit
Follow
Committee
square
formula
Parent Class PUBLIC PUBLIC PUBLIC Private PRIVATE PRIVATE Can't access the private public private protected private private private Can not be directly accessed?
Object Pascal: Tcreditaccount = Class (Taccount)
????????????????????? ...
Without a variety of inheritance methods like C , its inheritance principle is that the parent class Public and protected members are still in public and protected, parental private elements cannot be accessed in subclasses. However, if the parent class class is in the same unit, all its members can access.
7.2 multiple inheritance
A class may need to inherit multiple classes, which leads to multiple inheritance.
C : ??? Class Taccounta; ????????? // definition 省 省
????????????? class taccountb; ????????? // definition 省 省
?????????????? class taccountc: public taccounta, public taccountb ????? // class C inherited A and B
????????????? ...
?
Object Pascal: Using the interface (interface) to implement multiple inheritance, you can define multiple inheritance methods to define in the interface, then let it inherit the parent class and interface when defining subclats.
Ibank = interface
?????? procedure? Square ();
END;
Tcreditaccount = Class (taccount, ibank)
...
?
8, polymorphism: Detective class object can be used as the base class object, the same message can be sent to the base class object can also be sent to the derived class object.
8.1 virtual function: The virtual function defined in the base class, the derived class will be redefined.
C : ??? Class Taccount
????????????? {...
???????????????????? virtual void Square ();
}
Class Tcredtiaccount: Public Taccount
{...
?????? void square () {...}
}
?
Object Pascal: ???? Taccount = Class
????????????????????????????????? procedure square; Virtual
?????????????????????????? end;
???????????????????????????? Tcreditaccount = Class (TACCOUNT)
??????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????????????????????????????
?????????????????????????? end;
8.2 Abstract Method: The basic class method does not define the implementation of the method, and the subclasses need to be defined.
C : pure virtual functions, ie the virtual function does not define the implementation of the function, the subclass must give its own definition, the above example is both.
?
Procedure Square; Virtual; ABSTRACT;
?????????????????????? Subclass
?
9. Overload: The same function name in the class, the parameter table is different, and different operations are achieved.
C : Void Draw (Double Amount) ????????? Void Draw (Char Curtype, Double Amount)
?
Object Pascal: Procedure Draw (Amount: REAL); OVERLOAD;
??????????????????????? procedure Draw (CURTYPE: CHAR; AMOUNT: REAL); OVERLOAD
?
Third, conclude
Through the above comparison, the object-oriented characteristics in Object Pascal are quite complete, and substantially similar to C , the small part is different. There are still some features in this article that are not mentioned, such as operator overload in C , dynamic methods in Object Pascal, etc., these are all specific, so there is no contrast. In short, Object Pascal and C have a thousand autumn, how to make full use of the object-oriented characteristics provided, designing excellent object-oriented programs, is our goal.
?
references
[1] Marco Cantu Delphi6 From Getting Started to Jing [M]. Beijing: Electronic Industry Press, 2002-04
[2] Zhang Haofan, Yong Yongimin's object-oriented programming tutorial [M]. Beijing: Tsinghua University Press, 2002-06
[3] Li Tao Shen, Zhao Wenjing Object-Oriented Program Design and Method [M]. Beijing: Wuhan University of Technology Press, 2003-08
?