C++ Programming: Order of Execution

Today we are going to talk about an important part of the object-oriented philosophy - the order of execution when it comes to base and derived constructors, destructors, virtual inheritance etc.  Although there are some variations between different compilers (even between different versions of the same compiler) due to optimizations, concerning constructor and copy constructor calls, there are some rules which are always followed.

The order of execution when a derived class constructor is called is the following:

  • the constructor of a class from which we derived virtually
  • the constructor of a base class from which we derived non-virtually
  • construction of non-static member objects
  • body of the constructor
With both virtual and regular inheritance, if there are multiple base classes, their constructors are called in the order in which the base classes were listed in the inheritance statement. 
We can see that, in order to construct an object of a certain class, we first need to call the constructors of all the classes which are above our class in the hierarchy.  When calling these base class constructors, the order of execution mentioned above will also be followed.

 

Let's see an example:

class Base
{
public:
  Base() 
  { 
    cout << "cBase" << endl;
  }
};

class Derived: public Base
{
public:
  Derived()
  {
    cout << "cDerived" << endl;
  }
};

int main()
{
  Derived d;
  return 0;
}
The output will be the following (online example is here http://cpp.sh/9ab2):
cBase
cDerived
because the base class constructor is called first.

 

Let's make it a bit more complicated:

class Base
{
public:
  Base() 
  { 
    cout << "cBase" << endl;
  }
};

class C
{
public:
  C()
  {
    cout << "cC" << endl;
  }
};

class Derived: public Base
{
  C c;
public:
  Derived()
  {
    cout << "cDerived" << endl;
  }
};

int main()
{
  Derived d;
  return 0;
}
Now the output will be (Online example is here: http://cpp.sh/3t6y):
cBase
cC
cDerived
If there were classes from which the Derived class was virtually inherited, constructors of those classes would be called first.
Note: static member objects are created when they are declared.

 

Unlike the order of constructor calls, the destructor of the derived class is called first, and then the destructors of classes above.  Also, destructors are called whenever a statically allocated object goes out of scope, or delete is used to call the destructor explicitly.

For example:

class Base
{
public:
  Base() 
  { 
    cout << "cBase" << endl;
  }

  ~Base() 
  { 
    cout << "dBase" << endl;
  }
};

class Derived: public Base
{
public:
  Derived()
  {
    cout << "cDerived" << endl;
  }

  ~Derived()
  {
    cout << "dDerived" << endl;
  }
};

int main()
{
  Derived *d = new Derived;
  delete d;
  return 0;
}
Output (online example is here: http://cpp.sh/4ou5):
cBase
cDerived
dDerived 
dBase

 

5 Interesting Facts about Memorial Day

Each year, we celebrate Memorial Day on the last Monday in May.  While you probably already know that this federal holiday commemorates men and women who died in military service for our country, you may not be aware of the following 5 interesting facts:

  1. Memorial Day Was Originally Known as Decoration Day.  Although it’s uncertain which city started Decoration Day, this holiday can be traced to the Civil War (1861-1865).  During that time frame, people would honor dead soldiers by decorating their graves with flowers.

  2. Today We Have a National Moment of Remembrance.  President Clinton signed the National Moment of Remembrance Act in December 2000, designating 3PM on Memorial Day as a National Moment of Remembrance.

  3. Red Poppies Are a Symbol of Remembrance.  It’s traditional to wear red poppies on Memorial Day to honor those who died in war.

  4. Many States Observe Confederate Memorial Day.  Nine states officially celebrate Memorial Day, as well as a Confederate Memorial Day that specifically honors those who died fighting for the Confederacy in the Civil War.  Those states are: Texas, South Carolina, North Carolina, Alabama, Virginia, Louisiana, Mississippi, Tennessee, and Georgia.  The dates of Confederate Memorial Day vary, although only Virginia celebrates it on the same day as the federal holiday.

  5. More Soldiers Died in the Civil War than in Any Other Conflict.  According to CNN, US war casualties are as follows:

Civil War - Approximately 620,000 Americans died. The Union lost almost 365,000 troops and the Confederacy about 260,000. More than half of these deaths were caused by disease.

World War I - 116,516 Americans died, more than half from disease.

World War II - 405,399 Americans died.

Korean War - 36,574 Americans died.

Vietnam Conflict - 58,220 Americans died.

Operation Desert Shield/Desert Storm - 383 U.S. service members died.

Operation Iraqi Freedom - 4,424 U.S. service members died.

Operation New Dawn - 66 U.S. service members died.

Operation Enduring Freedom - 2,355 U.S. service members have died.

Thought For The Day

“Write it on your heart that every day is the best day in the year”

Ralph Waldo Emerson

 

4 Ways that Blogging Benefits Your Business

Do you have a blog for your business but only maintain it sporadically?  Alternatively, are you considering a business blog but haven’t decided yet if it’s worth the effort?  If you’re on the fence about blogging, we have some information to share with you about how blogging can benefit your business:

  1. Blogging Drives Web Traffic.  Every blog post on your site means 1 more indexed page on your website.  Why is that important?  It’s another opportunity to appear in search engine results or to have users find you by doing an organic search.  Blogging also increases the likelihood that your website will be visited by people who discovered your brand via social media, because your blog was shared via Twitter, Facebook, LinkedIn, etc.

  2. Blogging Is Great for Email Capture.  Email marketing has become increasingly common over the years.  However, it’s not just enough to have a generic email list, instead you want to have a list of customers who are familiar with your brand.  That’s where blogging can come in.  Blogging offers a great opportunity to ask potential customers for their email address to receive brand communications, promotion announcements, notification of new blog posts, etc.

  3. Blogging Allows You to Establish Yourself as an Authority.  By establishing yourself as an expert on a particular topic, you become the go-to whenever people want to learn more about that subject.  Ultimately, as people start to trust you to provide helpful, informative content, you can expect blogging to lead to greater conversion rates.

  4. Blogging Offers the Personal Touch.  All too often, companies seem like faceless bureaucracies.  As Social Media Today puts it, blogging allows you to show your personality, humanizing your company and making your business more appealing to potential customers.

Thought For The Day

“It's fun to sit at a terminal and let the code flow. It sounds strange, but it just comes out my brain; once I'm started,
I don't have to think about it.”

Gary Kildall