64 538
Assignments Done
99,3%
Successfully Done
In September 2018

Answer to Question #16984 in C# for martin

Question #16984
Is there any difference in allocation of memory to the members of the class and object. Justify the Statement that “the Dynamic Constructor helps in utilizing the memory efficiently. Illustrate with the help of an example.
Expert's answer
Applying Dynamic Initialization to Constructors
Like simple variables, objects can be initialized dynamically when they are created.
This feature allows you to create exactly the type of object you need, using information
that is known only at run time. To illustrate how dynamic initialization works, let’s
rework the timer program from the previous section.
Recall that in the first example of the timer program, there is little to be gained by
overloading the timer( ) constructor, because all objects of its type are initialized
using constants provided at compile time. However, in cases where an object will be
initialized at run time, there may be significant advantages to providing a variety of
initialization formats. This allows you, the programmer, the flexibility of using the
constructor that most closely matches the format of the data available at the moment.
For example, in the following version of the timer program, dynamic initialization
is used to construct two objects, b and c, at run time:
// Demonstrate dynamic initialization.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class timer{
int seconds;
public:
// seconds specified as a string
timer(char *t) { seconds = atoi(t); }
// seconds specified as integer
timer(int t) { seconds = t; }
// time specified in minutes and seconds
timer(int min, int sec) { seconds = min*60 + sec; }
void run();
} ;
void timer::run()
{
clock_t t1;
t1 = clock();
while((clock()/CLOCKS_PER_SEC - t1/CLOCKS_PER_SEC) < seconds);
cout << "\a"; // ring the bell
}
int main()
{
timer a(10);
a.run();
cout << "Enter number of seconds: ";
char str[80];
cin >> str;
timer b(str); // initialize at run time
b.run();
cout << "Enter minutes and seconds: ";
int min, sec;
cin >> min >> sec;
timer c(min, sec); // initialize at run time
c.run();
return 0;
}
As you can see, object a is constructed using an integer constant. However, objects
b and c are constructed using information entered by the user. For b, since the user
enters a string, it makes sense that timer( ) is overloaded to accept it. In similar
fashion, object c is also constructed at run time from user input. In this case, since the
time is entered as minutes and seconds, it is logical to use this format for constructing
object c. As the example shows, having a variety of initialization formats keeps you
from having to perform conversions when initializing an object.
The point of overloading constructors is to help programmers handle greater
complexity by allowing objects to be constructed in the most natural manner relative
to their specific use. Since there are three common methods of passing timing values to
an object, it makes sense that timer( ) be overloaded to accept each method. However,
overloading timer( ) to accept days or nanoseconds is probably not a good idea. Littering
your code with constructors to handle seldom-used contingencies has a destabilizing
influence on your program.

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions