Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Arrays and classes...
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
vexd




PostPosted: Tue Nov 25, 2003 6:45 pm   Post subject: Arrays and classes...

maybe i missed somethnig in class but check the code first:
code:
class omg
    export hi, print
    var a : array 1 .. 5 of int

    proc hi (x : int)
        for i : 1 .. 5
            a (i) := x + i
        end for
    end hi
   
    proc print
        for i : 1 .. 5
            put a (i)
        end for
    end print
end omg

var x : ^omg
var y : ^omg
new y
new x
y -> hi (5)
x -> hi (2)
y->print
put " "

x->print

i just wrote that now...
ok maybe i missed something in class or something but could someone explain why this works

y -> hi (5) -> i send 5, so the "a"'z elements will be set accordingly with + 5
x -> hi (2) -> when i send this it should REPLACE THE values of the ELEMENTS with 2... but it DOSENT, yes i know its because its a new pointer, but SO WHAT? maybe i missed something about pointers in class but according to me, how come it creates a new "storage place" in memory for just that pointer, ..i hope u understand..why dosent the value of "a" change from +5 to +2 ...so when i do:
y->print
x->print
it should return the same answer since a's value has been replaced..like i can get use to this, but i just wanna get this clear..and if this makes no sense, maybe its cuz im not thinking right today.
so at first itll be:
a[1] = 6
a[2] = 7
a[3] = 8
a[4] = 9
a[5] = 10
but when i call it again shouldnt it become:
a[1] = 3
a[2] = 4
a[3] = 5
a[4] = 6
a[5] = 7
so then when i print it, that comes up, but for some reason using differnt pointers theres different storage areas for the SAME array ..
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Tue Nov 25, 2003 7:20 pm   Post subject: (No subject)

thats because X and Y are separate objects.

it is not the same array at all. Pointer is just the -> part, which points to the class. The object is then constructed from that class. You now have an object. That object has whatever methods and variables are included in that class.

In simple terms...
code:

var x:int
var y:int


both X and Y are integers, but they're not the same.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: