Described the "named constructor idiom"?, C/C++ Programming

Assignment Help:

Described the "Named Constructor Idiom"?


Related Discussions:- Described the "named constructor idiom"?

Data structure, Project Description: Project is related indexing data re...

Project Description: Project is related indexing data related some data structure. I need to share all information related project later if i contact any body Skills required

What are source files and bytecode files, Problem : (a) What do you u...

Problem : (a) What do you understand by the term ‘constructor' in Java? Explain with an appropriate example. (b) Describe the differences between an object and a class usi

AREAS, Write a program to find the area under the curve y = f(x) between x ...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Write a program that computes the cost of a long distance ca, Write a progr...

Write a program that computes the cost of a long distance call. The cost of the call is determined according to the following rate schedules. a. A call made between 8:00 AM and 6:

Define enumeration in computer programming, Define Enumeration in Computer ...

Define Enumeration in Computer Programming? Enumerated types enclose a list of constants that are able to be addressed in integer values. We can declare variables and types

Differentiate between the expression ++a and a++, Differentiate between the...

Differentiate between the expression "++a" and "a++"? - With ++a, increment happens first on variable a, and resulting value is used. This is known as prefix increment. - Wi

Employee payroll, You are to write a C++ program which will compute the gro...

You are to write a C++ program which will compute the gross pay, Social Security Tax, Income Tax and net pay for an employee. The program needs to prompt for and read the employ

Functions, Define  F u n c t i o n?  T h e r e a r e t...

Define  F u n c t i o n?  T h e r e a r e t w o t y p e s o f f u n ct i o n b u i l t - i n f un ct i o n s a n d u

3/15/2013 6:19:37 AM

 A: A method which provides more intuitive and/or safer construction operations for users of your class.

The difficulty is that constructors have the same name always as the class. Thus the only way to differentiate among the various constructors of a class is via the parameter list. But if there are many constructors, the differences among them become somewhat and error prone and subtle.

Along the Named Constructor Idiom, you say publicly all the class''s constructors in protected or private sections, and you provide public static methods which return an object. These static techniques are "Named Constructors." usually, there is one such static method for each distinct way to construct an object.

For instance, suppose we are creating a Point class which represents a position on the X-Y plane. Turns out there are two common ways to mention a 2-space coordinate: polar coordinates (Radius+Angle), rectangular coordinates (X+Y). Unluckily the parameters for these two coordinate systems are the alike: two floats. It would create an ambiguity error in the overloaded constructors:

class Point {

public:

Point(float x, float y); // Rectangular coordinates                              

Point(float r, float a); // Polar coordinates (radius and angle)

// ERROR: Overload is Ambiguous: Point::Point(float,float)

};

int main()

{

Point p = Point(5.7, 1.2); // Ambiguous: Which coordinate system?

...

}

One way to solve out this ambiguity is to employ the Named Constructor Idiom:

#include // To get sin() & cos()

class Point {

public:

static Point rectangular(float x, float y); // Rectangular coord''s static Point polar(float radius, float angle); // Polar coordinates

// These static methods are so-called "named constructors"

... private:

Point(float x, float y); // Rectangular coordinates float x_, y_;

};

inline Point::Point(float x, float y)

: x_(x), y_(y) { }

inline Point Point::rectangular(float x, float y)

{ return Point(x, y); }

inline Point Point::polar(float radius, float angle)

{ return Point(radius*cos(angle), radius*sin(angle)); }

The users of Point now have a clear & unambiguous syntax for developing Points in either coordinate system:

int main()

{

Point p1 = Point::rectangular(5.7, 1.2); // clearly rectangular

Point p2 = Point::polar(5.7, 1.2); // Obviously polar

...

}

Ensure your constructors are in protected section if you expect Point to contain derived classes.

The Named Constructor Idiom can also be utilized to make sure your objects are always created using new.

Note down that the Named Constructor Idiom, at least as implemented above, is only as fast as calling directly constructor modern compilers will not make any additional copies of your object.

 

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd