Dynamic initialization of objects and dynamic constructor, C/C++ Programming

Dynamic Initialization of objects:

It is initializing the objects by passing the valued to the constructor from the user input or

other means.   Through cin operator a value can be stored in a variable and passed through a constructor this form of initializing an object is known dynamic initialization.

Dynamic Constructor:

The memory allocation for a constructor can be managed using new operator called new,

similarly the memory can be free with another operator called deleter.

 

class dynmem

{ char *name;

int length;

public:

dynmem(void)

{length =0;

name = new char[length+1];

}

dynmem(char *s)

{length = strlen(s);

name = new char[length+1];//for null terminator

strcpy(name,s);

}

void display(void)

{cout<

}

void join(dynmem &a, dynmem &b);

};

void dynmem::join(dynmem &a, dynmem &b)

{length = a.length+b.length;

delete name;

name = new char[length+1]; strcpy(name,a.name); strcat(name,b.name);

};

void main()

{

clrscr();

char *first="Electronic ";

dynmem name1(first);

dynmem name2("and Communication ");

dynmem name3("Engineering"); dynmem s1,s2; s1.join(name1,name2); s2.join(s1,name3); name1.display();

name2.display(); name3.display(); s1.display(); s2.display();

}

Posted Date: 9/20/2012 8:12:31 AM | Location : United States







Related Discussions:- Dynamic initialization of objects and dynamic constructor, Assignment Help, Ask Question on Dynamic initialization of objects and dynamic constructor, Get Answer, Expert's Help, Dynamic initialization of objects and dynamic constructor Discussions

Write discussion on Dynamic initialization of objects and dynamic constructor
Your posts are moderated
Related Questions
though the ascii is called 7-bit code. why do we use 8-bits to represent a character?

create a program to populate the first array then the odd number is the second array and get the sum of the second array.

Develop libcurl XML project I have a Project using XML with C++. You will use things like LIbcurl etc....and a XML parser to finish the project. Developer must know how to use X

C program to demonstrate call by reference: void disp(int *,int*,int*,int*); void main() {                 int a=2,b=3, sum,mul;                 printf("Enter 1st n

what is virtual datatype

Explain about the Floating point Constants in c language? A floating point constant is the number that contains either a fraction or decimal part. If an exponent is present its

A more advanced data type is the structure; here we can define a template as a collection of different variables e.g.     struct birthdate   {     int month;     int day;

#question.A Padovan string P(n) for a natural number n is defined as: P(0) = ‘X’ P(1) = ‘Y’ P(2) = ‘Z’ P(n) = P(n-2) + P(n-3), n>2 where + denotes string concatenation. For a s

how to use a pointer variable?

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec