POV-Ray : Newsgroups : povray.off-topic : OO theory? Python. : Re: OO theory? Python. Server Time
28 Jul 2024 22:16:29 EDT (-0400)
  Re: OO theory? Python.  
From: Le Forgeron
Date: 13 Oct 2012 14:33:26
Message: <5079b3f6$1@news.povray.org>
Le 13/10/2012 19:49, Shay nous fit lire :
> In Python (and I assume every other language), things that look like
> 
> instance.method()
> 
> return None and change the state of instance.

Well, stop. Stay away of python if you are hoping to learn OO with it:
it's a snake that would swallow you and as such hide the sun.

For instance, your addition in C++ would probably something along:


// Untested, typo might be there too
class Vector {
private:
  double internal_storage[3];
public:
  Vector(); // allow unspecified value
  Vector(double a, double b, double c);
// constructors are implemented elsewhere to protect the innocent

  Vector & operator +(const Vector &v2) const
  {
     return Vector(
this.internal_storage[0]+v2.internal_storage[0],
this.internal_storage[1]+v2.internal_storage[1],
this.internal_storage[2]+v2.internal_storage[2] );
  }
};

(should rather be name Vector3d... )

Usage

Vector c = Vector(1,2,3)+Vector(4,5,6);
(Bad, I should have also a copy constructor to allow:
 Vector c(Vector(1,2,3)+Vector(4,5,6));
)

Or

Vector c;
Vector a(1,2,3);
Vector b(4,5,6);
c = a+b;

Caveat: there is place for more code, and optimisations too.
(especially with move constructor and copy constructor, but first
understanding reference and const is recommended)

About inheritance, do not try to use it everywhere. A thumb is a finger,
but not all fingers are thumbs, and there is something called head which
is neither. Not talking about the tank of the car.

The importance of OO: it's like a cell from your body, there is an outer
interface and there is the inner content. The inner content must not be
visible from outsiders so that a radical change of it does not propagate
to an overhaul rewrite of all others pieces. If you can keep the outer
interface, there should be very low impact.
(but sometimes you need to nevertheless tie two or more objects... as a
pack of close friends, for efficiency purpose; If you tie everything,
you loose the OO game)

Hint: instead of python, have a look at Java or C++
(they differs: Java has only single inheritance in a row, with interface
to glue additional branches, and C++ allows the dreaded diamond shape,
not for the children)

Holy war java vs C++: fight!


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.