Re: Need help with OOP

From: Icarus Sparry (usenet_at_icarus.freeuk.com)
Date: 11/07/03


Date: Fri, 07 Nov 2003 06:47:20 GMT

sinisam wrote:

> Object oriented programming is not a new term to me, but when I tried
> to do some of it... uhh... it sure looked (and felt) like Hell :) I
> won't bother about things like "help me learn it" (but wouldn't mind
> if someone recommends a good fresh-start tutorial), instead I'll ask
> for help...
 
> I did my share of programming several years ago... 6 or 7 to be more
> specific, and as I'm pretty proud of my achievements ;)
> I continued on the same path. Several weeks ago, Python showed up.
> I got the 'assignment' to start learning it... almost professionally :)
>
> OK, I did my part, but now the tests are becoming more than fair :(
>
> Next part (even though I said I know *nothing* about OOP) is:
>
> "Make class for the IP address. It should be initialized from the
> string. Later on, we can add functions to it."
>
> [1] What in the world I have to do here?
> [1a] How to make class for the IP address? What does it mean?
> [2] "Initialization from the string" means something like passing
> arguments from the command line...?

Warning - oversimplification ahead!

With OOP, the idea is to make "Objects". In this case you want to make an
object which represents an IP address. You will want (eventually) to have a
list of things that you want to do to an IP address object, like create
one, convert one to a name via the DNS, destroy one, pretend that CIDR
doesn't exist and get the netmask for one and so on. In order to do these
things you will need to have some data associated with the object. So all
we need to do is package these things together (the functions you want to
use with the object and the persistant data you need). This packaged
"thing" is called a "class".

In python, you define a function called __init__ which makes the objects,
this is the initialization.

So you fire up a copy of python. and get a prompt. You then type

class ipaddress:
        def __init__(self,str):
                self.string_rep=str

and press return some more times until you get the >>>> prompt back.
This has defined a class called 'ipaddress', and made an initialization
routine for it. This routine takes 2 parameters, the first is traditionally
called 'self' and refers to the object itself. The second, named 'str',
represents the string parameter. The action of the initialization routine
is to store this string into an internal variable (called string_rep).

You can now create an ipaddress object by typeing

an_ip=ipaddress("192.168.1.1")

and you can do things like

print an_ip.string_rep

Later on you will want to define other functions, and maybe add some more
data. For instance you may want to define __str__(self) so you can just
print out an object.

It is worth working through the python tutorial if you have not already done
so.

To make life more interesting, OOP uses different words, like "method" to
refer to one of the functions defined in the class. Above we created a
variable called 'an_ip' which is an "instance" of the ipaddress class, we
could make a second instance

another_ip=ipaddress("192.168.1.2")

Normally the __init__ method would do rather better error checking, so it
made sure that it was being given a sensible string value,

When you have more methods defined, you can use tham as

an_ip.netmask()

which would be written as
        netmask(an_ip)
in most languages which are not OOP languages. Hope this very brief and
superficial introduction helps.



Relevant Pages

  • Re: I think C# is forcing us to write more (redundant) code
    ... static void F(out string s) ... >> compiler enforced). ... locals here and locals are stack allocated, ... I don't have a problem with this explicit initialization in the current ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Trouble with constructor(code included)
    ... omitted it earlier in my example initialization list, ... it would be better off as a string. ...
    (comp.lang.cpp)
  • Re: Freeing a record which contains a string field leads to a memory/string leak ?!
    ... Don't use GetMem. ... The memory that GetMem returns is not ... Delphi will try to free the "string" that is already there. ... initialization, such as strings. ...
    (alt.comp.lang.borland-delphi)
  • Re: Where to place a constant?
    ... Lasse Skyum wrote: ... > I have my own string class that I'm generally verry fond of compared to ... > thought it would be a good idea to share en empty string allocation between ... If the order of static variable initialization is important, ...
    (comp.lang.cpp)
  • Re: Problems sending emails
    ... I replaced the ipaddress with localhost and I recieved the following error ... String[] namedParameters) at System.RuntimeType.InvokeMember(String name, ... BindingFlags invokeAttr, Binder binder, Object target, Objectargs, ...
    (microsoft.public.dotnet.framework.webservices)