IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Cross Ref Guide
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesC++/CLIOOP Basics  Print This     

Cross Ref > OOP Basics

By Mike Prestwood

C++/CLI versus Delphi Prism: A side by side comparison between C++/CLI and Delphi Prism.

 
OOP Basics
 

Some languages support object-based concepts such as Paradox, Access, and VB Classic. Other languages have OO extensions and fully support object orientation in a hybrid fashion (such as C++ and Dephi for Win32). Finally, some lanages such as C#, VB.Net, Prism, and Java are entirely written in OO. Meaning, every line of code written must occur within a class).

Base Class

[Other Languages] 

Languages Focus

When you create a class, it is either a base class or inherits from another class. Some languages require all classes to inherit from a common base class and some do not.

C++/CLI:   System::Object

In C++/CLI, the Object keyword is an alias for the base System::Object class and is the single base class all classes ultimately inherit from.

More Info / Comment
Delphi Prism:   System.Object

In Prism, the Object keyword is an alias for the base System.Object class and is the single base class all classes ultimately inherit from.

Syntax Example:  
//Specify both namespace and class:
Cyborg = class(System.Object)
end;
  
//Use Object keyword for System.Object.
Cyborg = class(Object)
end;
  
//When none, default is System.Object.
Cyborg = class
end;




Class..Object

[Other Languages] 

Languages Focus

In short, a class is a data type, and an object is an instance of a class type. A class has methods (routines), properties (member variables), and a constructor. The current values of the properties is the current state of the object. The UML is one of the diagraming disciplines that allows you to document the various changing states of a series of objects.

[Not specified yet. Coming...]
Delphi Prism:   class..end..new

Declare your class in the Interface section. Then implement the class in the Implementation section. To create an object instance, use the New keyword. Optionally, you can use Create for backword compatibility with Delphi if you turn it on in the compatibility options. Since Prism does have a garbage collector, you do not have to free the object. If you need to free either unmanaged resources or resources where "timing" is important, implement IDisposable and take control of freeing the object yourself using Dispose.

Syntax Example:

In the interface section:

Cyborg = class(System.Object)
public method IntroduceYourself();
end;

In the Implementation section:

method Cyborg.IntroduceYourself();
begin
MessageBox.Show("Hi, I do not have a name yet.");
end;

On some event like a button click:

var T1: Cyborg;
begin
T1 := New Cyborg;
T1.IntroduceYourself;
  //No need to clean up with managed classes.
  //The garbage collector will take care of it.
end;




Inheritance

[Other Languages] 

The concept of a class makes it possible to define subclasses that share some or all of the main class characteristics. This is called inheritance. Inheritance also allows you to reuse code more efficiently. In a class tree, inheritance is used to design classes vertically. (You can use Interfaces to design classes horizontally within a class tree.) With inheritance, you are defining an "is-a" relationship (i.e. a chow is-a dog). Analysts using UML call this generalization where you generalize specific classes into general parent classes.

[Not specified yet. Coming...]
Delphi Prism:   =class(ParentClass)

In Prism, like Delphi, you use the class keyword followed by the parent class in parens. If you leave out the parent class, your class inherits from System.Object.

Syntax Example:

In the following example, a terminator T-600 is-an android. 

Android = public class
end;
 
T-600 = public class(Android)
end;




Member Event

[Other Languages] 

A custom event added by a programmer to a class. Custom created events need to be processed, usually by an event dispatcher within a framework.

[Not specified yet. Coming...]
Delphi Prism:  "Member Events" event

Like all .Net languages, Prism events are a separate type of class member. You define a member event by using the event keyword. Events depend on Delegates to define the signature (the type) of the event they represent and they maintain a list of multiple subscribers - unlike in Delphi for Win32, where each event can only have one handler

More Info / Comment




Member Field

[Other Languages] 

Also known as a Class Field.

A class variable defined with a specific class visibility, usually private visibility. A member property is different than a member field. A member property uses a member field to store values through accessor methods (getters and setters). For example, it is common to use a private member field to store the current value of a property. The current values of all the class member fields is the current state of the object.

Languages Focus

What modifiers apply to member fields, if any? Typical member field modifiers include scope modifiers (private, protected, etc.) and read-only. Can you initialize the value of a member field when declared ensuring a default value?

[Not specified yet. Coming...]
Delphi Prism: 

In Prism you can set the visibility of a member field to any visibility: private, protected, public, assembly and protected or assembly or protected.

Prism supports the readonly modifier for member fields which is handy for constant like data. In this case, I chose not to preface my read-only member field with "F" so it's usage is just like a read-only property.

Prism also support the class modifier (static data) for member fields.

Delphi developers should notice the use of := to initialize a member field (in Delphi you use an =).

Syntax Example:

Cyborg = class(System.Object)

private

FSerialNumber: String:="A100";

public

FCyborgName: String;

FCyborgAge: Integer:=0;

class SeriesID: Integer:=100; readonly;

end;





Member Method

[Other Languages] 

Also known as a Class Method.

A code routine that belongs to the class or an object instance (an instance of the class). Methods that belong to the class are called class methods or static methods. Methods that belong to an object instance are called instance methods, or simply methods.

When a method returns a value, it is a function method. When no value is returned (or void), it is a procedure method.

Methods frequently use method parameters to transfer data. When one object instance calls another object instance using a method with parameters, you call that messaging.

[Not specified yet. Coming...]
Delphi Prism:   method, procedure, function

Prism uses the keyword method for member methods and is the preferred syntax over the legacy procedure and function keywords. Although method is preferred, you can use procedure or function if you want the compiler to make sure all functions return a value and all procedures do not.

Syntax Example:

In the interface section:

Cyborg = class(System.Object)
  public method IntroduceYourself();
end;

In the Implementation section:

method Cyborg.IntroduceYourself();
begin
  MessageBox.Show("Hi, I do not have a name yet.");
end;
 

On some event like a button click:

var T1: Cyborg;
begin
  T1 := New Cyborg;
  T1.IntroduceYourself;
end;




Member Modifier

[Other Languages] 

Languages Focus

Traditional private, protected, public, etc. member modifiers are documented under the member visibility topic of the Cross Reference Encyclopedia. With member modifiers here, we address additional member modifiers such as method and field modifiers.

[Not specified yet. Coming...]
Delphi Prism:  "Member Modifiers"

Prism supports a full suite of member modifiers. Prism virtuality modifiers are virtual, override, final, and reintroduce.

Prism general modifiers are abstract, empty, async, external, locked, unsafe, implements, and iterator.

Not all member types support all member modifiers. For example, member fields support only readonly and implements.

Syntax Example:
Cyborg = public class(System.Object)
public
  method Speak(pMessage: String); virtual;
end;
 
Series888 = public class(Cyborg)
public
  method Speak(pMessage: String); override;
end;




Member Property

[Other Languages] 
[Not specified yet. Coming...]
Delphi Prism:   property..read..write

Like Delphi, Delphi Prism uses a special property keyword to both get and set the values of properties. The read and write keywords are used to get and set the value of the property directly or through an accessor method. For a read-only property, leave out the write portion of the declaration.

Prism also supports a shortcut syntax called implicit fields (known as auto-generated properties in C#):

property CyborgAge: Integer;

You can give properties any visibility you wish (private, protected, etc). It is common in Delphi and Delphi Prism to start member fields with "F" (FCName in our example) and drop the "F" with properties that manage member fields (CyborgName in our example).

Syntax Example:
Cyborg = class(System.Object)
private
  FCName: String;
public
  property CyborgName: String read FCName write FCName;
end;




Member Visibility

[Other Languages] 

General Info: Class Visibility Specifiers

In OOP languages, members of a class have a specific scope that indicates visibility. Standard visibility includes private, protected, and public. Private members are usable by the defining class only (fully encapsulated). They are invisible outside of the class except by friendly classes. Protected members are usable by the defining class and descendant classes only (plus friendly classes). Public members are usable wherever its class can be referenced.

Languages Focus

Traditional member visibility specifiers for fully OOP languages are private, protected, and public. Many modern OOP languages implement additional member visibilities.

Additional member modifiers are documented under the Member Modifiers topic.

[Not specified yet. Coming...]
Delphi Prism:  "Class Member Visibility Levels"

In Prism, you specify each class and each class member's visibility with a Class Member Visibility Level preceding the return type. Like Delphi, you group member declarations as part of defining the interface for a class in the Interface section of a unit.

Unlike Delphi, Prism supports a traditional OO approach to member visibility with additional .Net type assembly visibility. For example, private members are truly private to the class they are declared in. In Delphi for Win32, you use strict private for true traditional private visibility.

Prism also supports assembly and protected and assembly or protected which modify the visibility of protected members to include only descendants in the same assembly (and) or publicly accessible from assembly and descendant only outside (or). OO purist might object to assembly and protected and assembly or protected and I suggest you choose the traditional private, protected, and public as your first chose at least until you both fully understand them and have a specific need for them.

Syntax Example:
Cyborg = public class(System.Object)
private
  //private properties, methods, etc. here.
  FName: String;
protected 
  //protected properties, methods, etc. here.
assembly and protected
  //properties, methods, etc. here.
assembly or protected
  //properties, methods, etc. here.
public
  //properties, methods, etc. here.
end;




Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]