Class Inheritance and Naming in For Loops
Author |
Message |
lapo3399
|
Posted: Thu Jul 13, 2006 9:24 pm Post subject: Class Inheritance and Naming in For Loops |
|
|
Hi,
I have recently finished reading Cervantes' tutorials on classes and completely finished the walkthrough, and I am wondering about a couple of things:
1. Can one allow a user to specify for the creation of an object with a pointed to a specific class? For example, I would like to allow users to tell the program "how many" additional things they need to have objects made for, and the program to make a certain number of objects based on their answer. e.g.
code: |
for i : 1..number_of_objects
var whatever: pointer to generic_class_1
new generic_class_1, whatever
end for
|
As you might have already seen, I did not incorporate i into the actual object name. Herein lies problem 2.
2. In a for loop, how do you name a new object (if it is possible to make on)? My original attempt tried to make a generic name with intstr(i) concatenated to the end, however double declaration of the variable name, as well as inability to actually use the generic name for the object name, stopped me. If it is possible to dynamically name a variable; that is, name it depending on user input (e.g. create a variable named "georgette" based on "get"), then the problem is solved; otherwise, I may have to resort to some other crazy (and possibly impossible) method such as a flexible array of objects using i as the index .
Thank anybody who responds for their help, thanks to Cervantes for his excellent tutorials on classes and objects.
lapo3399
p.s. All variable names in my sample code were generic and not actual. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Cervantes
|
Posted: Thu Jul 13, 2006 9:49 pm Post subject: Re: Class Inheritance and Naming in For Loops |
|
|
lapo3399 wrote: 1. Can one allow a user to specify for the creation of an object with a pointed to a specific class? For example, I would like to allow users to tell the program "how many" additional things they need to have objects made for, and the program to make a certain number of objects based on their answer. e.g.
code: |
for i : 1..number_of_objects
var whatever: pointer to generic_class_1
new generic_class_1, whatever
end for
|
I'm not sure I understand the question. Are you looking for something like the user inputs a string (from get) and you create an object from the class by the same name as that which the user input? If so, that would require a large if structure or better yet, a hash (which in Turing we must implement as an array of a record containing a key and a value).
If that is indeed your question, I'll provide code, if desired.
lapo3399 wrote:
2. In a for loop, how do you name a new object (if it is possible to make on)? My original attempt tried to make a generic name with intstr(i) concatenated to the end, however double declaration of the variable name, as well as inability to actually use the generic name for the object name, stopped me. If it is possible to dynamically name a variable; that is, name it depending on user input (e.g. create a variable named "georgette" based on "get"), then the problem is solved; otherwise, I may have to resort to some other crazy (and possibly impossible) method such as a flexible array of objects using i as the index .
An array of objects is a dandy thing!
But no, I don't think you can dynamically set a variable like that. This is another place that Ruby shines.
lapo3399 wrote:
thanks to Cervantes for his excellent tutorials on classes and objects.
You're most welcome! |
|
|
|
|
|
lapo3399
|
Posted: Thu Jul 13, 2006 9:54 pm Post subject: Re: Class Inheritance and Naming in For Loops |
|
|
Cervantes wrote:
Are you looking for something like the user inputs a string (from get) and you create an object from the class by the same name as that which the user input? If so, that would require a large if structure or better yet, a hash (which in Turing we must implement as an array of a record containing a key and a value).
If that is indeed your question, I'll provide code, if desired.
You must have misunderstood me.
Sorry for my bad explanation.
I actually was attempting to ask whether the program could create a set number of objects (all of the same class), with the user inputting only the number of objects to be created.
Thanks again for your help. |
|
|
|
|
|
Cervantes
|
Posted: Thu Jul 13, 2006 10:16 pm Post subject: (No subject) |
|
|
Oh, sure!
code: |
class Foo
end Foo
var n : int
put "Enter number of objects to make: " ..
get n
var obj : array 1 .. n of ^Foo
for i : 1 .. n
new Foo, obj (i)
end for
|
Like that? |
|
|
|
|
|
lapo3399
|
Posted: Fri Jul 14, 2006 7:43 am Post subject: (No subject) |
|
|
That's exactly what I was trying to do, thank you.
I thought it had to do with arrays of some kind, but I am not 100% familiar with classes yet so I was unsure as how to actually program it.
Thanks for your help!
lapo3399 |
|
|
|
|
|
Delos
|
Posted: Fri Jul 14, 2006 9:50 am Post subject: Re: Class Inheritance and Naming in For Loops |
|
|
Cervantes wrote:
I'm not sure I understand the question. Are you looking for something like the user inputs a string (from get) and you create an object from the class by the same name as that which the user input? If so, that would require a large if structure or better yet, a hash (which in Turing we must implement as an array of a record containing a key and a value).
If that is indeed your question, I'll provide code, if desired.
Hashes in Turing? How!? I've only briefly introduced myself to these things...they seem interesting...care to expand? |
|
|
|
|
|
Cervantes
|
Posted: Fri Jul 14, 2006 6:36 pm Post subject: (No subject) |
|
|
We have to represent our hash as an array, because that's all we've got. But I think that's accurate. I imagine that a hash is actually stored in memory very much like an array. Can anyone clarify this?
Most basically:
code: |
type hash_pair :
record
key, value : string
end record
var hsh : flexible array 1 .. 1 of hash_pair
hsh (1).key := "Delos"
hsh (1).value := "Elegiac Mod"
|
But we don't have any way to work with our hash as a hash should be worked with. It really makes sense to make a hash object and give it the necessary methods.
code: |
class StringHash
export add, fetch
var data : flexible array 1 .. 0 of
record
key, value : string
end record
proc add(k, v : string)
new data, upper (data) + 1
data (upper (data)).key := k
data (upper (data)).value := v
end add
fcn fetch(k : string) : string
for i : 1 .. upper (data)
if data (i).key = k then
result data (i).value
end if
end for
result ""
end fetch
end StringHash
|
And of course we have to make new classes if we want to use different types of data structures in our hash.
All code untested. |
|
|
|
|
|
Clayton
|
Posted: Fri Jul 14, 2006 7:06 pm Post subject: (No subject) |
|
|
or you could incorporate enumerated types to your program , this could make things much easier and more readable, bonus!
as for the array of objects, why not have a flexy array? wouldnt that make much more sense? i mean then the user could just keep going until they activate an exit condition without them having to worry about how many they are going to make before hand |
|
|
|
|
|
Sponsor Sponsor
|
|
|
[Gandalf]
|
Posted: Fri Jul 14, 2006 7:09 pm Post subject: (No subject) |
|
|
Very nifty implementation there, Cervantes. I imagine it could work quite well in Turing.
Two things you overlooked in your code, set is a reserved word, and you need to export those functions in order to use them. If I ever do something sizable in Turing again, this is something I'll make sure to try out. |
|
|
|
|
|
Cervantes
|
Posted: Fri Jul 14, 2006 7:33 pm Post subject: (No subject) |
|
|
[Gandalf] wrote: Two things you overlooked in your code, set is a reserved word, and you need to export those functions in order to use them. If I ever do something sizable in Turing again, this is something I'll make sure to try out.
Thanks for pointing those out. They've been fixed.
Also, I should add that when you examine a hash (at least in Ruby) the data is not in the order that you added it. It would be good to modify the fetch function to use a binary search rather than a sequential search, and modify the add function to do a sort of "binary insertion": using the binary search technique to determine where to insert the data.
It could also use other methods to manipulate the hash, of course. |
|
|
|
|
|
Cervantes
|
Posted: Fri Jul 14, 2006 7:45 pm Post subject: (No subject) |
|
|
SuperFreak82 wrote: or you could incorporate enumerated types to your program , this could make things much easier and more readable, bonus!
The problem here, I think, is that enumerated types aren't dynamic: You couldn't add to your hash. |
|
|
|
|
|
|
|