Recent

Author Topic: i'm new in oop so guys i need help  (Read 2405 times)

whitehat

  • Jr. Member
  • **
  • Posts: 93
i'm new in oop so guys i need help
« on: November 19, 2016, 07:37:26 pm »
hello all coder i need help to write a simple program that show "hello world"; so i need help to write this code
Code: Pascal  [Select][+][-]
  1. program exObjects;
  2. type
  3.    hello = object
  4.    private
  5.       a: integer;
  6.  
  7.    public
  8.       procedure saisie(n: integer);
  9.       procedure affiche();
  10.  
  11. end;
  12. procedure hello.saisie( n:integer);
  13. begin
  14.   writeln('saisir n');
  15.   readln (n);
  16. end;
  17.  
  18. procedure hello.affiche ();
  19. var
  20.   i,n:integer;
  21.   begin
  22.     for i:=1 to n do
  23.     writeln('hello');
  24.     end;
  25. begin
  26.   hello.saisie(n);
  27.   hello.affiche();
  28.     end.
  29.  

derek.john.evans

  • Guest
Re: i'm new in oop so guys i need help
« Reply #1 on: November 19, 2016, 07:55:10 pm »
My advice, Read Pascal code everyday for at least a year.

Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   THello = object
  4.   private
  5.     FCount: integer;
  6.   public
  7.     procedure Saisie;
  8.     procedure Affiche;
  9.   end;
  10.  
  11.   procedure THello.Saisie;
  12.   begin
  13.     writeln('saisir n');
  14.     readln(FCount);
  15.   end;
  16.  
  17.   procedure THello.Affiche();
  18.   var
  19.     i: integer;
  20.   begin
  21.     for i := 1 to FCount do begin
  22.       writeln('hello');
  23.     end;
  24.   end;
  25.  
  26. var Hello: THello;
  27.  
  28. begin
  29.   Hello.Saisie;
  30.   Hello.Affiche;
  31. end.  
  32.  

whitehat

  • Jr. Member
  • **
  • Posts: 93
Re: i'm new in oop so guys i need help
« Reply #2 on: November 19, 2016, 08:13:13 pm »
thx you man i'm good in pascal but trust me it's my first hours in oop in pascal XD look my last script
but what i'm really need to know what mean this (^) i use it but i don't know what that mean i use it here (deux = ^deuxx ;)

Code: Pascal  [Select][+][-]
  1. program ex;
  2.  
  3. type
  4.  
  5.   deux = ^deuxx ;
  6.     deuxx = object
  7.       private
  8.       a,b:integer;
  9.       public
  10.       procedure multi(c,d:integer);
  11.  
  12.       end;
  13.   procedure deuxx.multi (c,d:integer);
  14.   begin
  15.     write(c*d);
  16.     end;
  17. var
  18.   hello:deux;
  19.   c,d: integer;
  20.   begin
  21.     new (hello);
  22.     hello^.multi(7,5);
  23.     dispose(hello);
  24.     end.                      
  25.  

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: i'm new in oop so guys i need help
« Reply #3 on: November 19, 2016, 09:27:17 pm »
The = ^ symbol combination is short for "defines a pointer to", so
Code: Pascal  [Select][+][-]
  1.  type deux = ^deuxx;

means "the type deux defines a pointer to deuxx".
BTW a good convention is to name all pointer types with an initial P. It is not obligatory, but it makes it clear that (say PDeux) is a pointer and not an integer or a record etc.
Using this naming convention you might have named your pointer type PDeux, and your hello pointer pHello.
When ^ is placed at the end of a pointer identifier it has a different meaning: "dereference this pointer". If pHello points to some dynamically allocated structure in memory (you created yours with a call to New() ), then pHello^ means the structure itself, not its memory address.

Objects (deuxx = object ... end;) are not the best place to start when learning OOP today. They are not deprecated, and have their limited uses. However, learning about classes (TDeux = class ... end;) will be far more fruitful for you, and you will find many, many more examples of good Pascal code to learn from that use classes rather than objects.
Using a class rather than an object your code (removing redundant bits, and improving the naming) looks like this:
Code: Pascal  [Select][+][-]
  1. program ex;
  2.  
  3. type
  4.  
  5.   TDeux = class
  6.   public
  7.     procedure Multi(c, d: integer);
  8.   end;
  9.  
  10.   procedure TDeux.Multi(c, d: integer);
  11.   begin
  12.     WriteLn('c*d = ',c*d);
  13.   end;
  14.  
  15. var
  16.   hello: TDeux;
  17.  
  18. begin
  19.   hello:=TDeux.Create;
  20.   hello.Multi(7, 5);
  21.   hello.Free;
  22. end.


 

TinyPortal © 2005-2018