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

Assignment Help:

Described the "Named Constructor Idiom"?


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

Implement a binomial tree class, a) Implement a binomial tree class with a ...

a) Implement a binomial tree class with a method that calculates the value of an option passed in to the class. The binomial tree should not rely on specific features of the differ

Custom scans and alerts think or swim, Custom Scans and Alerts Think or Swi...

Custom Scans and Alerts Think or Swim Project Description: I am seeking somebody to make me several Custom Scans and Alerts on Thinkorswim TOS Skills required are C++ Prog

Difference between mutex and binary semaphore, Semaphore is used to synchro...

Semaphore is used to synchronize processes. whereas mutex is used to give synchronization among threads running in the similar process.

M - algorithm corrections, Of course it is C[i] = A[i] + B[i].It was a typi...

Of course it is C[i] = A[i] + B[i].It was a typing mistake,never mind. You just understand the concept. 27-1 b. for grain-size=1 n=A.length grain-size=1 r=n for

It/218.., Write a program that does the following: Calculates the Velocity ...

Write a program that does the following: Calculates the Velocity and Momentum of an object. The formula for the velocity is V=d/t and the formula Momentum is m=mass*velocity. Your

Displays the lower case and upper case alphabetical letters, Write a progra...

Write a program that displays both the lower case and upper case alphabetical letters using the following Format Lowercase        Uppercase a                        A

How do i develop a subscript operator for a matrix class?, Employ operator ...

Employ operator () instead of operator[]. While you have multiple subscripts, the cleanest way to do it is along with operator () instead of with operator[]. The reason is that

Program to compare the two dates given by the user, THIS PROGRAM IS TO COM...

THIS PROGRAM IS TO COMPARE THE TWO DATES GIVEN BY THE USER #include #include #include struct date  {   int dd;   int mm;   int yy;  }; date compare(date d1,date d2)  {

Develop a biomedical imaging project, Develop a biomedical imaging project ...

Develop a biomedical imaging project Project Description: This is a biomedical imaging project. Skills required are C++ Programming, Cocoa, Mac OS, Objective C

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