jaberwacky Posted May 8, 2010 Share Posted May 8, 2010 (edited) First I'd like to say that I am not a c++ geek. HI! My question is this, if my program has two objects that use the same data then what should I do? Say object1 one uses $variable0 and object2 uses $variable0 also. Should I declare $variable0 as global? But wouldn't that defeat the purpose of OOP? Or should I pass that variable to each object? But wouldn't that violate the DRY principle? Hopelessly confused, Matt Hmmm, maybe that data should be public data so that any object can use it? I've read that you should avoid getter and setter functions though. Edited May 8, 2010 by jaberwocky6669 Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
Richard Robertson Posted May 8, 2010 Share Posted May 8, 2010 Declaring globally versus copy per instance depends on the context of the data. What kind of data is it? Link to comment Share on other sites More sharing options...
jaberwacky Posted May 8, 2010 Author Share Posted May 8, 2010 You know what, I need to take more time to study independantly. I appreciate it though. Thank you. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
jaberwacky Posted May 8, 2010 Author Share Posted May 8, 2010 (edited) Well, ok, lemme see, I'm making a program that takes a number of images; shrinks them all to the height of the shortest image; appends those into a panorama; and then shrinks the width of the panorama to 715px. I realize now that I should have an image object whose properties are height and width, I think. So: Object: Image Properties: Width Height Directory Location Image Name Format Method(s): Shrink Width Shrink Height Note: I'm trying to learn how to use autoitobject btw Here is what I have so far... Will someone be so kind to take a look and see if I am on the right track? Func ImageObject() Local $self = _AutoItObject_Create() _AutoItObject_AddMethod($self, "Height", "_SetHeight") _AutoItObject_AddMethod($self, "Width", "_SetHeight") _AutoItObject_AddMethod($self, "Format", "_SetFormat") _AutoItObject_AddMethod($self, "Name", "_SetName") _AutoItObject_AddMethod($self, "Directory", "_SetDirectory") _AutoItObject_AddProperty($self, "Height", $ELSCOPE_PRIVATE) _AutoItObject_AddProperty($self, "Width", $ELSCOPE_PRIVATE) _AutoItObject_AddProperty($self, "Format", $ELSCOPE_PRIVATE) _AutoItObject_AddProperty($self, "Name", $ELSCOPE_PRIVATE) _AutoItObject_AddProperty($self, "Directory", $ELSCOPE_PRIVATE) EndFunc Edited May 8, 2010 by jaberwocky6669 Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
Kip Posted May 8, 2010 Share Posted May 8, 2010 (edited) Func ImageObject() Local $self = _AutoItObject_Create() _AutoItObject_AddMethod($self, "Height", "_SetHeight") _AutoItObject_AddMethod($self, "Width", "_SetHeight") _AutoItObject_AddMethod($self, "Format", "_SetFormat") _AutoItObject_AddMethod($self, "Name", "_SetName") _AutoItObject_AddMethod($self, "Directory", "_SetDirectory") _AutoItObject_AddProperty($self, "Height", $ELSCOPE_PRIVATE) _AutoItObject_AddProperty($self, "Width", $ELSCOPE_PRIVATE) _AutoItObject_AddProperty($self, "Format", $ELSCOPE_PRIVATE) _AutoItObject_AddProperty($self, "Name", $ELSCOPE_PRIVATE) _AutoItObject_AddProperty($self, "Directory", $ELSCOPE_PRIVATE) EndFunc This way you'll add every member twice to the object: once as a method, and once as a property. You'd probably want to do something like this: _AutoItObject_AddMethod($self, "Height", "_SetHeight", $ELSCOPE_PRIVATE) _AutoItObject_AddMethod($self, "Width", "_SetHeight", $ELSCOPE_PRIVATE) .... Edited May 8, 2010 by Kip MailSpons: Fake SMTP server for safe email testing Dutch postcode & address API. Link to comment Share on other sites More sharing options...
jaberwacky Posted May 8, 2010 Author Share Posted May 8, 2010 Im beginning to think that maybe an image object isn't the way to go either. I might stick with procedural programming for now I guess... Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
Yoriz Posted May 9, 2010 Share Posted May 9, 2010 Dont give up its fun to play with, if your design isnt quite right yet but you can get the desired result does it really matter while your trying to grasp the concept of it. I've never used oop before this udf was made so i cant claim that this is 'the' way to do it, this is how i would approach it with the knowledge ive gained of it so far, im sure some one can look over it and improve on it. expandcollapse popup#include <AutoItObject.au3> _AutoItObject_Startup() Global $oError = ObjEvent("Autoit.Error", "_myErrFunc") Func _ErrFunc() ConsoleWrite("! COM Error ! Number: 0x" & Hex($oError.number, 8) & " ScriptLine: " & $oError.scriptline & " - " & $oError.windescription & @CRLF) EndFunc ;==>_ErrFunc $oPanorama = _oPanorama("My New Panorama") $oPanorama.AddImage("GoodPic") $oPanorama.AddImage("BadPic") $oPanorama.AddImage("UglyPic") $oPanorama.Create #region _oPanorama Func _oPanorama($hFileName) local $oSelf = _AutoItObject_Create() ; _oPanorama Property _AutoItObject_AddProperty($oSelf, "FileName", $ELSCOPE_PRIVATE, $hFileName) _AutoItObject_AddProperty($oSelf, "ImgCount", $ELSCOPE_PRIVATE, 0) _AutoItObject_AddProperty($oSelf, "MinHeight", $ELSCOPE_PRIVATE, 0) ; _oPanorama Method _AutoItObject_AddMethod($oSelf, "AddImage", "_oPanorama_AddImage") _AutoItObject_AddMethod($oSelf, "Create", "_oPanorama_Create") Return $oSelf EndFunc ;_oPanorama Methods Func _oPanorama_AddImage($oSelf, $hFileName) Local $iMinHeight $oSelf.ImgCount = $oSelf.ImgCount + 1 _AutoItObject_AddProperty($oSelf, "Image" & $oSelf.ImgCount, $ELSCOPE_PRIVATE, _oImage($hFileName)) $iMinHeight = Execute("$oSelf.Image" & $oSelf.ImgCount & ".Height") Select Case $oSelf.MinHeight > $iMinHeight $oSelf.MinHeight = $iMinHeight Case $oSelf.MinHeight = 0 $oSelf.MinHeight = $iMinHeight Case Else Return $oSelf EndSelect ConsoleWrite("new smallest height of " & $oSelf.MinHeight & " Found for Image" & $oSelf.ImgCount & " FileName = " & $hFileName & @CR) Return $oSelf EndFunc Func _oPanorama_Create($oSelf) Local $vJoinedImg, $hFileName For $i = 1 To $oSelf.ImgCount Step 1 Execute("$oSelf.Image" & $i & ".ReSize(" & $oSelf.MinHeight & ")") $hFileName = Execute("$oSelf.Image" & $i & ".FileName") $vJoinedImg &= "|" & $hFileName Next $vJoinedImg &= "|" ; do some resizing ConsoleWrite(@CR & "Save image as " & $oSelf.FileName & " = " & $vJoinedImg & @CR) EndFunc #endregion _oPanorama #region _oImage Func _oImage($hFileName) local $oSelf = _AutoItObject_Create() ; _oImage Property _AutoItObject_AddProperty($oSelf, "FileName", $ELSCOPE_READONLY, $hFileName) _AutoItObject_AddProperty($oSelf, "Height", $ELSCOPE_READONLY, 0) _AutoItObject_AddProperty($oSelf, "Width", $ELSCOPE_READONLY, 0) ; _oImage Method _AutoItObject_AddMethod($oSelf, "GetSize", "_oImage_GetSize") _AutoItObject_AddMethod($oSelf, "ReSize", "_oImage_ReSize") ; _oImage Contructor $oSelf.GetSize Return $oSelf EndFunc ; _oImage Methods Func _oImage_GetSize($oSelf) ; this would contain code that uses $oSelf.FileName to obtain and set $oSelf.Height & $oSelf.Width $oSelf.Height = Random(25, 100, 1) ; just to guess a size for example purpose $oSelf.Width = Random(25, 100, 1) ; just to guess a size for example purpose ConsoleWrite("[Inside _oImage_GetSize] FileName = " & $oSelf.FileName & " $oSelf.Height = " & $oSelf.Height & " $oSelf.Width = " & $oSelf.Width & @CR) EndFunc Func _oImage_ReSize($oSelf, $iHeight) If $oSelf.Height = $iHeight Then ConsoleWrite("No need to resize " & $oSelf.FileName & ", its already at smallest size" & @CR) Return EndIf Local $iPercentage = $iHeight / $oSelf.Height $oSelf.Height = $iHeight $oSelf.Width = round($iPercentage*$oSelf.Width) ConsoleWrite("[Inside _oImage_ReSize] FileName = " & $oSelf.FileName & " New $oSelf.Height = " & $oSelf.Height & " New $oSelf.Width = " & $oSelf.Width & @CR) EndFunc #endregion _oImage #cs console output [Inside _oImage_GetSize] FileName = GoodPic $oSelf.Height = 82 $oSelf.Width = 63 new smallest height of 82 Found for Image1 FileName = GoodPic [Inside _oImage_GetSize] FileName = BadPic $oSelf.Height = 98 $oSelf.Width = 35 [Inside _oImage_GetSize] FileName = UglyPic $oSelf.Height = 53 $oSelf.Width = 40 new smallest height of 53 Found for Image3 FileName = UglyPic [Inside _oImage_ReSize] FileName = GoodPic New $oSelf.Height = 53 New $oSelf.Width = 41 [Inside _oImage_ReSize] FileName = BadPic New $oSelf.Height = 53 New $oSelf.Width = 19 No need to resize UglyPic, its already at smallest size Save image as My New Panorama = |GoodPic|BadPic|UglyPic| #ce GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF. Link to comment Share on other sites More sharing options...
Richard Robertson Posted May 9, 2010 Share Posted May 9, 2010 Eh, AutoIt isn't the best place to learn OOP. In fact it's a pretty bad one. I don't see where you have a data field that would be the same on all objects though. Link to comment Share on other sites More sharing options...
jaberwacky Posted May 9, 2010 Author Share Posted May 9, 2010 Wow! That's really awesome of you to take the time to come up with that Yoriz! From what I can tell you have two objects, a panorama object and an image object. That's along the lines of what I was thinking. How did you go from not doing oop to being able to do oop so quickly? Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
Yoriz Posted May 9, 2010 Share Posted May 9, 2010 lots of readinggo here download 'Thinking in C++, 2nd edition, Volume 1'http://math.hws.edu/eck/cs124/downloads/OOP2_from_Univ_KwaZulu-Natal.pdfhttp://math-cs.gordon.edu/courses/cs211/ATMExample/ GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF. Link to comment Share on other sites More sharing options...
Yoriz Posted May 9, 2010 Share Posted May 9, 2010 I think this is more kind of like your original question, which i havent figured out myself yet. If you create a lightbulb object and a switch object, the lightbulb object can recieve a message volts in(%), the switch object can send a message volts out(%). create a circuit object that conatains a switch and a bulb object, how when you do $oCircuit.Switch.on does circuit pass on the message from the switch to the lightbulb ?. expandcollapse popup#include <AutoItObject.au3> _AutoItObject_Startup() Global $oError = ObjEvent("Autoit.Error", "_myErrFunc") Func _ErrFunc() ConsoleWrite("! COM Error ! Number: 0x" & Hex($oError.number, 8) & " ScriptLine: " & $oError.scriptline & " - " & $oError.windescription & @CRLF) EndFunc ;==>_ErrFunc $oBulb = _oLightBulb() $oPlainSwitch = _oPlainSwitch() $oBulb.VoltsIn($oPlainSwitch.On) $oBulb.VoltsIn($oPlainSwitch.Off) $oDimmerSwitch = _oDimmerSwitch() $oBulb.VoltsIn($oDimmerSwitch.SetDimmerPos(50)) $oBulb.VoltsIn($oDimmerSwitch.Off) $oBulb.VoltsIn($oDimmerSwitch.SetDimmerPos(75)) $oBulb.VoltsIn($oDimmerSwitch.On) $oCircuit = _oCircuit() $oCircuit.Switch.On ; want this to return message from lightbulb object 'Light bulb is On at 100%' #region _oLightBulb Func _oLightBulb() local $oSelf = _AutoItObject_Create() ; _oLightBulb Property (variables) _AutoItObject_AddProperty($oSelf, "State", $ELSCOPE_PRIVATE, "Off") _AutoItObject_AddProperty($oSelf, "Brightness", $ELSCOPE_PRIVATE, "0") ; _oLightBulb Method (functions) _AutoItObject_AddMethod($oSelf, "VoltsIn", "_oLightBulb_VoltsIn") _AutoItObject_AddMethod($oSelf, "OutPut", "_oLightBulb_OutPut") Return $oSelf EndFunc ; _oLightBulb Methods (functions) Func _oLightBulb_VoltsIn($oSelf, $iPercentage) $oSelf.Brightness = $iPercentage If $iPercentage < 1 Then $oSelf.State = "Off" If $iPercentage > 0 Then $oSelf.State = "On" $oSelf.OutPut EndFunc Func _oLightBulb_OutPut($oSelf) ConsoleWrite("Light bulb is " & $oSelf.State & " at " & $oSelf.Brightness & "%" & @CR) EndFunc #endregion _oLightBulb #region _oPlainSwitch Func _oPlainSwitch() local $oSelf = _AutoItObject_Create() ; _oPlainSwitch Property (variables) _AutoItObject_AddProperty($oSelf, "volts", $ELSCOPE_PRIVATE, 0) ; _oPlainSwitch Method (functions) _AutoItObject_AddMethod($oSelf, "On", "_oPlainSwitch_On") _AutoItObject_AddMethod($oSelf, "Off", "_oPlainSwitch_Off") Return $oSelf EndFunc ; _oPlainSwitch Methods (functions) Func _oPlainSwitch_On($oSelf) $oSelf.volts = 100 Return $oSelf.volts EndFunc Func _oPlainSwitch_Off($oSelf) $oSelf.volts = 0 Return $oSelf.volts EndFunc #endregion _oPlainSwitch #region _oDimmerSwitch Func _oDimmerSwitch() local $oSelf = _AutoItObject_Create(_oPlainSwitch()) ; inherit from _oPlainSwitch _AutoItObject_AddProperty($oSelf, "State", $ELSCOPE_PRIVATE, False) ; _oPlainSwitch Method (functions) _AutoItObject_AddMethod($oSelf, "On", "_oDimmerSwitch_On") ; overwrites plainswitch _AutoItObject_AddMethod($oSelf, "Off", "_oDimmerSwitch_Off") ; overwrites plainswitch _AutoItObject_AddMethod($oSelf, "SetDimmerPos", "_oDimmerSwitch_SetDimmerPos") Return $oSelf EndFunc ; _oDimmerSwitch Methods (functions) Func _oDimmerSwitch_On($oSelf) $oSelf.State = True Return $oSelf.volts EndFunc Func _oDimmerSwitch_Off($oSelf) $oSelf.State = False return 0 EndFunc Func _oDimmerSwitch_SetDimmerPos($oSelf, $iPercentage) If $iPercentage < 0 Then $iPercentage = 0 If $iPercentage > 100 Then $iPercentage = 100 $oSelf.volts = $iPercentage If $oSelf.State Then Return $oSelf.volts Else Return 0 EndIf EndFunc #endregion _oDimmerSwitch #region _oCircuit Func _oCircuit() local $oSelf = _AutoItObject_Create() ; _oCircuit Property (variables) _AutoItObject_AddProperty($oSelf, "Switch", $ELSCOPE_READONLY, _oPlainSwitch()) _AutoItObject_AddProperty($oSelf, "Bulb", $ELSCOPE_READONLY, _oLightBulb()) ; _oCircuit Methods (functions) _AutoItObject_AddMethod($oSelf, "Connect", "_oCircuit_Connect") Return $oSelf EndFunc ; _oCircuit Methods (functions) Func _oCircuit_Connect($oSelf) $oSelf.Bulb.VoltsIn($oSelf.Switch) EndFunc #endregion _oCircuit GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF. Link to comment Share on other sites More sharing options...
jvanegmond Posted May 9, 2010 Share Posted May 9, 2010 how when you do $oCircuit.Switch.on does circuit pass on the message from the switch to the lightbulb ?.The circuit tells the switch to pass on the message on his behalf. github.com/jvanegmond Link to comment Share on other sites More sharing options...
Yoriz Posted May 9, 2010 Share Posted May 9, 2010 The circuit tells the switch to pass on the message on his behalf.Would you mind giving an example using my code above, as althought i can tell that i want to make the return of switch send its message to bulb.I dont see how inside of $oCircuit i get the return value from switch when calling $oCircuit.Switch.on to be able to pass its return value into $oSelf.Bulb.VoltsIn inside of the oCircuit object. GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF. Link to comment Share on other sites More sharing options...
jaberwacky Posted May 9, 2010 Author Share Posted May 9, 2010 I ive begun to read the ATM Example now. Turns out I already had the thinking in c++ but I never got around to reading it! If you like puzzles then take a look at the 23 prisoners puzzle if you haven't already. See if you can turn it into an oo program. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
jvanegmond Posted May 9, 2010 Share Posted May 9, 2010 (edited) Would you mind giving an example using my code above, as althought i can tell that i want to make the return of switch send its message to bulb.I dont see how inside of $oCircuit i get the return value from switch when calling $oCircuit.Switch.on to be able to pass its return value into $oSelf.Bulb.VoltsIn inside of the oCircuit object.I've looked at your example a bit closer. I think the best options is this:You can create a relationship between the switch and the lightbulb (can be a relationship via circuit). But you must be careful not to introduce additional dependancies. You can safely assume that everything inside an electrical circuit will be an electrical device. The switch and the lightbulb can then inherit from an abstract device object, which has a current/voltage in and a current/voltage out (and thus also resistance).Edit: Although more closer to reality, it would be better to give each electrical device a resistance, and the power supply a voltage and let the circuit figure out the rest. But this is more computational intensive. Edited May 9, 2010 by Manadar github.com/jvanegmond Link to comment Share on other sites More sharing options...
jaberwacky Posted May 9, 2010 Author Share Posted May 9, 2010 (edited) Ok, I made my own lightbulb and switch class. So far it merely switches the light on and off. I'm assuming that OO will make it easy to add a dimmer switch into the mix. Because isn't that a part of what OO is about, ease of maintenance (wow for a sec. I forgot how to spell maintenance!), and ability to add to and change the classes without affecting the user? expandcollapse popup#include "AutoItObject.au3" _AutoItObject_Startup() Global $oError = ObjEvent("Autoit.Error", "_myErrFunc") Global $lightBulb = LightBulbObj() Global $Switch = SwitchObj() $Switch.Flip() $Switch.Flip() #region LightBulb Class Func LightBulbObj() Local $self = _AutoItObject_Create() _AutoItObject_AddProperty($self, "BulbState", $ELSCOPE_PRIVATE, False) _AutoItObject_AddMethod($self, "Output", "_declareBulbState", True) _AutoItObject_AddMethod($self, "Flip", "_changeBulbState") Return $self EndFunc ;==>LightBulbObj Func _declareBulbState($self) MsgBox(0, "Light Bulb", $self.BulbState) EndFunc ;==>_declareBulbState Func _changeBulbState($self) $self.BulbState = Not $self.BulbState $self.Output() EndFunc ;==>_changeBulbState #endregion LightBulb Class #region LightSwitch Class Func SwitchObj() Local $self = _AutoItObject_Create() _AutoItObject_AddProperty($self, "SwitchState", $ELSCOPE_PRIVATE, False) _AutoItObject_AddMethod($self, "Flip", "_flipSwitch") _AutoItObject_AddMethod($self, "Output", "_declareSwitchState", True) Return $self EndFunc ;==>SwitchObj Func _declareSwitchState($self) MsgBox(0, "Light Bulb", $self.SwitchState) EndFunc ;==>_declareSwitchState Func _flipSwitch($self) $lightBulb.Flip() $self.SwitchState = Not $self.SwitchState EndFunc ;==>_flipSwitch #endregion LightSwitch Class Func _ErrFunc() ConsoleWrite("! COM Error ! Number: 0x" & Hex($oError.number, 8) & " ScriptLine: " & $oError.scriptline & " - " & $oError.windescription & @CRLF) EndFunc ;==>_ErrFunc Edited May 9, 2010 by jaberwocky6669 Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
monoceres Posted May 9, 2010 Share Posted May 9, 2010 Shouldn't the switch be connected to the light bulb? Maybe the switch should take an light bulb instance in it's constructor and save it as an property? Broken link? PM me and I'll send you the file! Link to comment Share on other sites More sharing options...
jaberwacky Posted May 9, 2010 Author Share Posted May 9, 2010 (edited) I have no idea what constructor and desctructors are about or if you're even replying to my code lulz I did see that the lightbulb.state is independant of whatever state the switch is in though Edited May 9, 2010 by jaberwocky6669 Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
jaberwacky Posted May 9, 2010 Author Share Posted May 9, 2010 According to this site: (http://www.linuxtopia.org/online_books/programming_books/thinking_in_c%2B%2B/Chapter01_002.html) a lightbulb should have four methods: on, off, dim, bright. The light can cut itself on and off and dim and brighten itself? Or deosn't it need another object to do that? And then expand on that based on what Manadar said, use resistance and voltage to determine the light bulb's state. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
jaberwacky Posted May 9, 2010 Author Share Posted May 9, 2010 ooookkkkkk, when I instantiate an object of the switch object then automagically an object of light bulb class is instantiated and then the ligthbulb is set according to the state of the switch? Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now