• Homework
  • Thirds
  • Upper Thirds
  • Fourth
  • Divisions
    • ICT
    • Computer Science
  • Fifth
  • Lower Sixth
  • Upper Sixth
    • Upper Sixth Theory
  • Blog
  • Careers
  • Unity
  • Networking

Some lovely object code to get us started

First, set up the class.  This is nothing but a blueprint.  We add two variables (attributes) and three procedures (methods)
type
  myclass = class
  public
     name:string;
     score:integer;
     procedure output();
     procedure clearall();
     procedure doublescore();
  end;
Next.  Create the object variable.  This doesn't actually create the object, which is fun :-(
var
  myobject : myclass;

Now set up the procedures (methods) that we've announced in our class definition
procedure myclass.output();
begin
  writeln('Name is:  ',self.name);
  writeln('Score is: ',self.score);
end;
procedure myclass.clearall();
begin
  self.name := '';
  self.score :=  0;
end;
procedure myclass.doublescore();
begin
  self.score := self.score * 2;
end;
We are ready to write the code.  There's one absolutely vital thing to do first.  Actually create the object.
begin
  myobject := myclass.Create;
OK, we've done the housekeeping.  Now we can write to the attributes and call the methods.  Let's see what happens
  myobject.name := 'Harold';
  myobject.score := 23;
  myobject.output;
  myobject.doublescore;
  myobject.output;
  myobject.clearall;
  myobject.output;
  writeln('Did we learn anything at all?');
  readln;

end.
Powered by Create your own unique website with customizable templates.