On Sat, Jan 8, 2022 at 10:46 AM Duke Normandin <dukeofp...@gmx.com> wrote:
> I've /never/ been fond of OOP, but if I'm to continue with Pike, I
> figure I should at least try to understand Pike's very of OOP.
> From: https://pike.lysator.liu.se/docs/tut/oop/creation_and_usage.md
> [quote]
> Creating and Using Objects
> Assuming that we have the class animal, we can define some
> variables that can be used to store animals. Remember that the
> class is also a data type. We can also create some animals to put
> in those variables. To create an animal, we use the syntax
> *classname*(), i e the name of the class followed by a pair of
> parentheses.
> animal some_animal;
> some_animal = animal();
> animal my_dog = animal();
> [/quote]
> The three statements are confusing to me.
> So a class called "animal" has been created and exists! OK ..
> "animal some_animal;" ?? Are we cloning the class here to create an
> object?

At that point, you're just declaring a variable, same as saying "int
some_number;" or "array some_collection;". Since you don't initialize
it, it defaults to 0 (specifically UNDEFINED, the special zero with
that flag).

> "some_animal = animal(); ??  So what's this than?

This is actually cloning the animal program, or instantiating the
animal class, or whatever you want to call it. This is actually
constructing an animal and assigning it to some_animal.

> "animal my_dog = animal(); ?? It seems to me that this should be
> the cloning statement to create a particular object of the animal
> class?

This is the combination of the previous two lines: declaring my_dog
and also constructing a dog.

> It's all a bloody muddle to me. I'm sure that I must be reading it
> wrongly.

The thing to bear in mind is that variable declarations are just a
hint saying "hey, this thing should store this type of thing". It
doesn't actually construct anything. You could put a generic "object"
there instead (or even "mixed") and it would behave the same way:

object some_animal;
some_animal = animal();
object my_dog = animal();

Calling a type constructs an object, just as it does in Python (and in
JavaScript if you use the "new" keyword with the call). You can then
assign that object to any compatible variable; notably, a variable
that wants a Foo can accept any object that inherits Foo (or if you
like: an object counts as all of its supertypes).


Reply via email to