This is 3rd blog post about introduction to c++ 11 standard with examples. In this post we will study about c++ 11 features enum call,static_assert and delegating constructor.

introduction to c++ 11 standard with examples (tutorial 3)introduction to c++ 11 standard with examples (tutorial 3)

enum class

In c++ 03 enum is an integer. so following code will give output “apple is equal to dog”.

#include<iostream>
#include<vector>
using namespace std;

enum animal {
	dog, cat
};
enum fruit {
	apple, mango
};
int main() {
	animal obj = dog;
	fruit f_obj = apple;

	if (obj == f_obj) {
		cout << "apple is equal to dog\n";
	} else {
		cout << "apple is not equal to dog\n";
	}
	return 0;
}

In c++ 11 enum class is an class. so following code will give compile time error “error: no match for ‘operator==’ (operand types are ‘animal’ and ‘fruit’)”. The error happens because we have not provided == operator overloading.

#include<iostream>
#include<vector>
using namespace std;

enum class animal {
	dog, cat
};
enum class fruit {
	apple, mango
};
int main() {
	animal obj = animal::dog;
	fruit f_obj = fruit::apple;

	if (obj == f_obj) {
		cout << "apple is equal to dog\n";
	} else {
		cout << "apple is not equal to dog\n";
	}
	return 0;
}

c++ 11 static_assert ( compile time assert )

c++ 03 provides run time assert.

#include<iostream>
#include<vector>
#include<assert.h>
using namespace std;

char *name = nullptr;
int main() {
	//run time assert
	assert(name != nullptr); // runtime error if (name != nullptr) becomes false
	return 0;
}

c++ 11 provides compile time assert through static_assert keyword.  static_assert performs compile time assertion checking. If the assertion is false, the complier will output the specified error message.

static_assert declaration syntax

static_assert(constant-expression,string)
#include<iostream>
#include<assert.h>
using namespace std;

int main() {
	static_assert(sizeof(long) >= 8, "64-bit code generation not enabled/supported.");
	return 0;
}

When expression (sizeof(long) >= 8 ) would fail, compiler shows error message “64-bit code generation not enabled/supported.” at compile time.

c++ 11 delegating constructor

c++ 11 delegating constructor is a very simple concept. Before going into the details let us make a very simple use case for designing a class. we will design the class in c++03 standard first and later on in c++ 11.

Suppose we are going to design a class contact with three data members name, phone number and email address. so our class may look like as

class contact
{
  string name;
  int phone_number;
  string email_address;
}

The above class is not a good design of a class because we have not provided a constructor. what constructor does? it initializes its data members.

class contact {
	string name;
	int phone_number;
	string email_address;
public:
	contact(string person_name, int person_phone, string person_email) {
		name = person_name;
		phone_number = person_phone;
		email_address = person_email;
	}
};

So far we have provided a constructor in our class contact. it would initialize its data members name, phone_number and email_address.

can you think what is issue with this design?

The design ensures that it will create an object of class contact provided all information name, phone number and email is present. what if there is no email or phone or name is present? so in this case we have to provide other constructors to make an different object of class contact.

class contact {
	string name;
	int phone_number;
	string email_address;
public:
	contact(string person_name, int person_phone, string person_email) {
		name = person_name;
		phone_number = person_phone;
		email_address = person_email;
	}
	contact() { // constructor with blank object
		name = "";
		phone_number = 0;
		email_address = "";
	}
	// contact with no phone number
	contact(string person_name, string person_email) {
		name = person_name;
		phone_number = 0;
		email_address = person_email;
	}
	// contact with no email address
	contact(string person_name, int person_phone) {
		name = person_name;
		phone_number = person_phone;
		email_address = "";
	}
};

Reimplementing above design using c++ 11 delegating constructor

Delegating constructors prevent code duplication. The meaning of delegating a constructor means one constructor can call another to initialize the object.

#include<iostream>
using namespace std;

class contact {
	string name;
	int phone_number;
	string email_address;
public:
	contact(string person_name, int person_phone, string person_email) {
		name = person_name;
		phone_number = person_phone;
		email_address = person_email;
	}
	// constructor with blank object
	contact() :
		contact("", 0, "") {
	}
	// contact with no phone number
	contact(string person_name, string person_email) :
		contact(person_name, 0, person_email) {
	}
	// contact with no email address
	contact(string person_name, int person_phone) :
		contact(person_name, person_phone, "") {
	}
};

int main() {

	contact c = { "Mr. Foo", 89898988, "foo@xyx.com" };
	contact b = { "Mr. Loo", "loo@asd.com" };
	return 0;
}

Ref:

http://tuttlem.github.io/2013/01/17/delegating-constructors-in-c-11.html

https://en.wikipedia.org/wiki/C%2B%2B11



Related Contents to follow