devxlogo

Avoid reduplicating identical pieces of contructors’ code

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

See also  Redefining Energy Management: Advanced Solutions for Modern Buildings
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist