Avoid reduplicating identical pieces of contructors’ code

There are cases where you have to define several constructors (or constructors and

  operator = 

) within one class, all of which perform some identical tasks (e.g., initialize members, allocate memory, and so on), plus additional individual functionality, as in the following example:

 class Personnel {	int children;	float salary;	string name;	...	public:	personnel() {	children =0; 		       	salary = 0.0;		       	...//individual actions}	personnel (const string& n) {	children =0; 		       			salary = 0.0;						name = n;		      		 	...}};

Instead of repeating identical pieces of code in each of the constructors, it

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: