Constructors Brief Overview

This is a quick overview on constructors. Constructors instantiate objects and are created in a similar fashion across different languages.

This example is a constructor for a Java object. Calling FooBar.foo will display the string “Bar”.

class FooBar{
  String foo;

  public FooBar(){
    foo = "Bar";
  }
}

Next stop, Python. A constructor in Python is written with __init__ method.

class FooBar:
  def __init__(self):
    self.foo = "Bar"
  

Like in python everything in PowerShell is an object and they have constuctors too. This is how you can code one.

$FooBar = @{ foo = 'bar'}

The last example is this FooBar object coded in c++. Besides defining a constructor you can also create a destructor. If you couldn’t guess a destructor handles how the object will be delete. To be honest I haven’t coded C++ in awhile and this one might have some errors. Later today, I’ll quickly try creating this FooBar object together and check how close I’m getting this one write.

class FooBar {
  string foo;
  
  public: 
    FooBar();
    ~FooBar();
};

//Consturctors
FooBar::FooBar(){
  foo = "Bar";
}

//Destructors
FooBar::~FooBar(){
  delete foo;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s