OK so as you've probably guessed we're going to mitigate this problem of construction limitations by
moving the initialization from a constructor to some other structure and we're going to begin by using
the factory method design pattern so just as the name suggests what it does is it actually uses a method
or a member function to initialize the object.
So let me actually rename this from a struct to a class because what I'm going to do is I'm going to
do a bit of encapsulation just a little bit.
So what we're going to have is we're going to have public properties X and Y public fields.
So I'm going to put a public in here but we are going to have a private constructor.
So we're going to have a constructor which actually initializes the point just from the x and y coordinates.
So let's just recreate this once again and I'll get rid of public here because I actually won the constructor
to be private.
OK so what's going to happen now is we're going to add a couple of static member functions and these
member functions are going to allow us to actually construct the object on the basis of a certain coordinate
system.
So first of all we're going to have a static point new Cartesian factory method which is going to take
X and Y to X Y and we simply return a new result and you point with x and y.
Ok so this is how you would construct a new Cartesian point or point from cartesian coordinates in a
similar fashion.
You can have a factory method which also returns a point and which constructs a new polar point to a
point from polar coordinates.
So a new polar floats are and float theta and we return well with it.
Obviously those transformations so we return our times cause of theta and are sometimes sign of theta
like so.
OK so this as you can see is an implementation which actually disallows the use of the constructor.
So you cannot have a constructor as such or you do have it.
But it's only used internally in here.
And here it's not allowed to be used outside by the consumer.
So if you do want to make a point then you will not be able to write.
Point one column too that's not possible because the constructor is private so what you can do is you
can say auto p equals point of view poler for example.
And here you can provide the coordinates like are 5 and maybe theta off by 0 4 4 something to that effect.
And yeah this this definition actually it is a bit tricky because you have to define use math that finds
Annoyingly enough trying to be smart here.
Use math defines.
Hopefully this will fix this particular area and then you have a point.
And we can actually sort of print this point.
Actually we haven't generated any kind of stream outputs for the point but I'm going to do it in just
a moment.
It's going to be a public thing.
So here let's just generate Well I seem to have eaten a curly brace so let's go and generate the stream
up with the operator just outputting the X Y and we can actually run this so we can construct a point
from polar coordinates and then we can print it.
And hopefully this prints the actual Cartesian coordinates as they are converted because we're feeding
in polar coordinates but the output is obviously Cartesian because that's what the internal storage
mechanism is.
So here we have some coordinates for the X and the Y.
OK so this implementation is called a factory method essentially you have these static methods which
allow you to construct a particular object and they do use the private constructor.
So when you return curly braces what you're essentially returning is your turning point with curly braces
in C++ and the more than C++ you can actually emit the name of the tie because it's going to be inferred
from the return type of the method itself.
But apart from that this is how it works generally and this is the gist of the factory method design
pattern.