Want Inspiration for Achieving Your New Year’s Resolution? 3 Stories of People Who Persevered

As December draws to a close, many people resolve to change their lives over the upcoming year.  For instance, they might commit to exercising more, eating healthier, finding a new job, etc.  However, statistics show that few people succeed at achieving their new year’s resolutions—typically, the figure is about 8%.  If you’ve vowed to change in previous years and failed, take heart!  We have stories of people who persevered that will help to inspire your success in 2016:

  1. Walt Disney.  Today we know him as the successful founder of The Walt Disney Company, but it wasn’t always that way.  In his earlier years, Disney was fired for a “lack of creativity.”  He then went on to form his own company, but it quickly went out of business.  For awhile, he could barely pay his rent and resorted to eating dog food.  Although broke, he decided to spend his few remaining dollars on a ticket to Hollywood where he ultimately went on to found The Walt Disney Company with his brother, Roy.

  2. Richard Branson.  Undiagnosed dyslexia caused academic problems for Branson, but he was determined not to let those difficulties prevent him from becoming a successful entrepreneur.  He started Virgin Records in the basement of a local church, ignoring those “experts” who told him that the record business was overcrowded and he would never make money.  Since then, he’s gone on to found Virgin Airways and Virgin Mobile, and his personal net worth is more than $5 billion.

  3. Colonel Sanders.  At the age of 65, Colonel Sanders received a Social Security check for $105.  Angry about the paltry sum, he decided to do something about it.  He believed that restaurant owners would love his fried chicken recipe, the recipe would increase their sales, and they’d be willing to give him a percentage.  So, he drove around the country—sometimes sleeping in his car—looking for someone willing to give him and his fried chicken a chance.  Amazingly, he heard a whopping 1009 (!!!) no’s before he finally got a yes!

Object-Oriented Programming: Operator Overloading - Assignment Operators

In this article we're going to learn how to overload assignment operators.  This is something which was already mentioned in one of the previous articles, when we talked about the "rule of three" of object-oriented programming (we said that we always write the copy constructor and destructor, and overload the assignment operator when we are working with dynamic memory in our class).  The code of the overloaded assignment operator will also be very similar to a copy constructor.

We will continue using our complex numbers example.
Let's overload the operator, and then we will explain what we did.

Complex& operator=(const Complex &z)
{
    re = z.re;
    im = z.im;
    return *this;
}
There is nothing new in the code above: the assignment operator is overloaded as a member function, which returns a reference to a complex number object.  The parameter is also a Complex reference, which is something we have discussed before.  Finally, we dereference the this pointer to return the object on which the method was called - this is the difference between this method and the copy constructor.  Copy constructor creates another object, while the assignment operator assignes values to an already existing object.

 

Often, we would also check whether we are trying to assign the object to itself:

Complex& operator=(const Complex &z)
{
    if (this == &z )
       return *this;
    re = z.re;
    im = z.im;
    return *this;
}

 

We have noticed the similarities between the assignment operator and the copy constructor.  It might be useful to also mention that there are ways to write these functions more efficiently and avoid code duplication by writing another function where the actual copying of data is performed, and calling that function from both assignment operator and copy constructor.
This is how that would look in general:

   // Copy constructor
  MyClass(const MyClass& c)
  {
    MakeDeepCopy(c);
  }

  // Assignment operator
  const MyClass& operator=(const MyClass& c)
  {
    MakeDeepCopy(c);
  }

private:
  // Common function where the actual copying happens.
  void MakeDeepCopy(const MyClass& c)
  {        
    //copying the data...
  }

 

Finally, it is important to know that other assignment operators such as "+=" or "-=" aren't automatically overloaded if we overload "+" or "-", but the concept is the same for all assignment operators.

New Year’s Resolutions – What the Statistics Say

With 2016 just around the corner, have you given any thought to creating a resolution for the new year?  About 45% of Americans do and for the most part, their resolutions center on fitness and money.  In fact, here are the most frequent resolutions that people in the US make:

21% vow to lose weight

14% want to improve their financial situation

14% plan to exercise more

10% set a goal of getting a new job

7% plan to eat healthier

Yet unfortunately, by the time March rolls around, most people have abandoned their resolutions.  What makes the 8% of people who succeed different from the rest of us?  While it’s tempting to conclude they have more willpower, that’s not entirely the case.

Research has found that willpower is malleable.  Essentially, scientists have discovered that people who believe they can, can—that your belief in your ability to achieve your goal is a self-fulfilling prophesy.

To increase the likelihood of your own resolution being successful, experts recommend that you do the following:

  • Hold Yourself Accountable.  Tell people about your goal!  The more people who know about it, the less likely you’ll quit midstream.

  • Set Specific Goals.  Rather than just setting a goal of “eating healthy,” define what that means.  Do you plan to eat 5 servings of fruits and vegetables a day?  Ingest a certain number of grams of fiber?  By being specific, you’ll have a clear roadmap for achieving your goal.

  • Consider Obstacles.  Once you’ve developed your resolution, think about any challenges that you’re likely to face.  For instance, if you plan to exercise 5 times a week and travel a lot, think about ways that you’ll get your cardio in while you’re on the road.  Will you swim in the hotel pool?  Wake up early to hit the treadmill?  Once you’ve identified potential obstacles and determined a way to deal with them, you’ll be far more likely to reach your objective. 

Thought For The Day

“The discovery of electrical waves... has had a profound influence on civilization; it has been instrumental in providing the methods which may bring all inhabitants of the world within hearing distance of each other and has potentialities social, educational and political which we are only beginning to realize.”
Joseph John Thomson

 

 

Object-Oriented Programming: Operator Overloading - Arithmetic Operators

The first group of operators that we are going to overload will be arithmetic operators - unary and binary.  Overloading arithmetic operators is simple and intuitive, which makes them suitable for illustrating the purpose of operator overloading.
We will continue using the example from the last article, where we wrote a class which represents complex numbers.

     
class Complex
{
public:

    double re, im;

    Complex() {re = im = 0;}
    Complex(double r, double i)
    {
        re = r;
        im = i;
    }
};
We also wrote a method for adding two complex numbers:
Complex add(Complex c)
{
    Complex result;
    result.re = this->re + c.re;
    result.im = this->im + c.im;
    return result;
}
        

Like we said last time, using this method would be inconvenient, which is why instead, we are going to overload the "+" operator to do the addition.  Now we're going to learn how to do that.

friend Complex operator+ (const Complex &z1, const Complex &z2)
{
    Complex result;
    result.re = z1.re + z2.re;
    result.im = z1.im + z2.im;
    return result;
}

We see here that the code looks almost exactly the same as the add method from above.  Now, we can simply write a + b, because we have defined a way to add complex numbers.  We could also define subtraction, multiplication etc. the same way.

 

Note: Notice that we overloaded the "+" operator as a friend function.  This is the standard way of overloading binary arithmetic operators . We also could have overloaded our operator as a member function, in which case the method would only have one parameter, but this approach can produce unwanted effects and is generally not recommended.

 

Now, let's also overload the unary "-" operator, so that we can easily get the negative value of our complex number.  The principle is the same, but considering the nature of this operator, it would make sense to overload it as a member function:

Complex operator- ()
{
    Complex w;
    w.re = -this->re;
    w.im = -this->im;
    return w;
}
Now, when you write -b, you will get the negative value of the b complex number.

 

In the next article, we will learn how to overload assignment operators (=, +=, -= etc), which will also allow us to complete our "rule of three" which we talked about earlier.

You can run sample code online here: C++ shell