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
IS IT POSSIBLE FOR YOU TO WRITE A PROGRAM FOR ME TO MEASURE EXISTING ANGLES AND DO IF AND THEN FROM THIS , AND TO PLACE FIXED ANGLES AT CERTAIN POSITION AS A ADD WITH DLL FOR CERTA

Can copy constructor admit an object of the same class as parameter, rather than reference of the object?

Create a program to print a battleship grid to the console and mark squares as destroyed

Abstract Classes Abstract classes are the classes, which are written just to act as base classes. Consider the following classes.                 class base

Default Arguments A default argument is a value that is automatically assigned to a formal variable, if the actual argument from the function call is omitted. e.g. void

Make the following 3 functions work by filling in appropriate C code. This file 1. // Write a function to count the number of particular characters in a string. // Do not use a

Develop a Student class that has the following header file: #ifndef STUDENT_H #define STUDENT_H #include #include #include using namespace std; class Stu

Assume variables x, f, and d are of type int, float, and double, respectively.  Their values are arbitrary, except that neither f nor d equals +∞, -∞, or NaN .  For each of the fo


Problem: (a) Using a class hierarchy of your own choosing, with at least TWO subclass levels, show (i) the use of abstract and concrete forms of Java class and method in you