About objects

In Pascal, is it possible to have derived objects like the derived classes in C++? Also, is it possible to make the method for a particular object different from the default method for that object type?

Comments

  • : In Pascal, is it possible to have derived objects like the derived classes in C++? Also, is it possible to make the method for a particular object different from the default method for that object type?
    :

    Hi there,

    To answer your question, it is possible to model some of the OOP 'type things' like in C++ with pascal. Instead of explaining it all here i had a look and found a site that goes some way to explaining how it can be done. Have a look at:

    http://www.geocities.com/codeteacher/

    click on pascal on the left hand side then on lesson three on the right hand screen. I think that the first four chapters could be some use.

    Hope that helped!

    Wez
  • Thanks. That did help, but it didn't answer all of my questions. Is it possible to do something like this:

    type
    A = object
    private
    z: integer;
    protected
    x, y: integer;
    public
    procedure foo(a: integer);
    function bar(b: integer):integer;
    end;

    procedure A.foo(a : integer);
    begin
    writeln('Something');
    end;

    var
    something: A;

    something.foo
    begin
    writeln('Something else');
    end;
    :
    :
    :


    What I'm trying to do is make something's foo procedure be different from the default for object A.
  • : Thanks. That did help, but it didn't answer all of my questions. Is it possible to do something like this:
    :
    : type
    : A = object
    : private
    : z: integer;
    : protected
    : x, y: integer;
    : public
    : procedure foo(a: integer);
    : function bar(b: integer):integer;
    : end;
    :
    : procedure A.foo(a : integer);
    : begin
    : writeln('Something');
    : end;
    :
    : var
    : something: A;
    :
    : something.foo
    : begin
    : writeln('Something else');
    : end;
    : :
    : :
    : :
    :
    :
    : What I'm trying to do is make something's foo procedure be different from the default for object A.
    :
    That is possible, but not in the way you are trying it. You need to create a new oject and override the procedure, which must be declared as virtual. Here is an example:
    [code]
    type
    A = object
    private
    z: integer;
    protected
    x, y: integer;
    public
    procedure foo(a: integer); [red]virtual;[/red]
    function bar(b: integer):integer;
    end;

    B = object(A)
    procedure foo(a: integer); override;
    end;

    procedure A.foo(a : integer);
    begin
    writeln('Something');
    end;

    procedure B.foo(a : integer);
    begin
    writeln('Something else');
    end;

    var
    something: A;
    begin
    Something := B.Create;
    Something.Foo(4); { should write "something else" }
    i := Something.Bar(2); { call to the inherited A.Bar() }
    Something.Destroy;
    end.
    [/code]
  • I see. If you hadn't made Something equal to B.Create, would it have been for Something to have used the default foo procedure from object A?
  • : I see. If you hadn't made Something equal to B.Create, would it have been for Something to have used the default foo procedure from object A?
    :
    Yes.
  • [b][red]This message was edited by FlyingNosehair at 2002-11-26 20:30:4[/red][/b][hr]
    Oh, so it would have been possible? Well, thanks!

    EDIT: Is it possible to make an object be derived from more than one other object?

  • : [b][red]This message was edited by FlyingNosehair at 2002-11-26 20:30:4[/red][/b][hr]
    : Oh, so it would have been possible? Well, thanks!
    :
    : EDIT: Is it possible to make an object be derived from more than one other object?
    :
    :
    No, but you can create a shell-object, which holds several objects in one:
    [code]
    type
    TA = object
    A_Field: integer;
    procedure Foo;
    end;

    TB = object
    B_Field: integer;
    procedure Foo;
    procedure Bar;
    end;

    TC = object
    private
    A: TA;
    public
    B: TB;
    constructor Create; override; { I'm not sure about this override }
    destructor Destroy; override;
    function GetA_Field: integer;
    procedure SetA_Field(Value: integer);
    procedure Foo;
    procedure Bar;
    procedure FooBar;
    end;

    constructor TC.Create;
    begin
    inherited Create;
    A := TA.Create;
    B := TB.Create;
    end;

    destructor TC.Destroy;
    begin
    A.Destroy;
    B.Destroy;
    inherited Destroy;
    end;

    procedure TC.Foo;
    begin
    A.Foo;
    end;

    procedure TC.Bar;
    begin
    B.Bar;
    end;

    procedure TC.FooBar;
    begin
    B.Foo;
    end;

    function TC.GetA_Field: integer;
    begin
    GetA_Field := A.A_Field;
    end;

    procedure SetA_Field(Value: integer);
    begin
    A.A_Field := Value;
    end;
    [/code]
    TC is the shell-object. I suggest that it should create all internal objects only in the constructor and destroy them only in the destructor. All the methods you wish to make public need to be made public in the shell and should call the correct method of the correct internal object. In case of fields, you could make the internal objects public, or you could use a single function and a single procedure to get and set the field value. (See example).
  • I see. Well, thanks!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories