Computer Science Canada Declaring an object outside if statement? |
Author: | Luffy123 [ Sun May 13, 2012 3:26 pm ] |
Post subject: | Declaring an object outside if statement? |
I have run into a problem where it says obj isn't recognized im guessing its cuz i declared the var obj in an if statement. so I'm wodnering how I can decalre it outside and if I eve ncan? import Heart in "C:/Users/Hamza/Desktop/classes/classes/heart.tu", Diamond in "C:/Users/Hamza/Desktop/classes/classes/diamond.tu" setscreen ("graphics:640;480,nocursor") var objX, objY, objHeight, objWidth, objColour : int var objName : string put "Do you want a diamond or a heart?" get objName put "Enter the x coordinate of the heart." get objX put "Enter the y coordinate of the heart." get objY put "Enter the height of the heart." get objHeight put "Enter the width of the heart." get objWidth put "Enter the colour of the heart." get objColour if objName = "heart" then var obj : pointer to Heart new obj end if if objName = "diamond" then var obj : pointer to Diamond new obj end if obj -> setCentre (objX, objY) obj -> setHeight (objHeight) obj -> setWidth (objWidth) obj -> setColour (objColour) obj -> draw I wanna save code so I'm wondering how I can do this. |
Author: | Dreadnought [ Sun May 13, 2012 3:50 pm ] | ||
Post subject: | Re: Declaring an object outside if statement? | ||
Every class in Turing inherits from the anyclass class so we can write the following:
|
Author: | Luffy123 [ Sun May 13, 2012 3:56 pm ] | ||
Post subject: | Re: Declaring an object outside if statement? | ||
Dreadnought @ Sun May 13, 2012 3:50 pm wrote: Every class in Turing inherits from the anyclass class so we can write the following:
I don't understand why it isn't working? var obj : pointer to anyclass if objName = "heart" then new Heart, obj end if if objName = "diamond" then new Diamond, obj end if obj -> setCentre (objX, objY) obj -> setHeight (objHeight) obj -> setWidth (objWidth) obj -> setColour (objColour) obj -> draw |
Author: | Dreadnought [ Sun May 13, 2012 4:26 pm ] | ||
Post subject: | Re: Declaring an object outside if statement? | ||
Remember that obj is a pointer to anyclass not to diamond or hearts. Therefore, Turing can't be sure that setCentre, setheight exist for the type of object that obj will point to (obj could point to some other class). It can only be sure that things in the export list of anyclass exist, but setCentre and setHeight are in the export list of a different class. You can either use a different syntax:
Or, if you understand how inheritance works, you can make a class from which your hearts and diamonds will inherit functions that are common to both (and then you can use these functions with the '->' operator.) Hope this helps. |
Author: | Raknarg [ Sun May 13, 2012 6:01 pm ] |
Post subject: | RE:Declaring an object outside if statement? |
I think that you shouldn't keep these as separate objects, like you appear to be doing. They're technically the same object, the only difference being the suit. At least have different subclasses of the same class (with inheritance) |