C++ dynamic constructor

C++ Dynamic constructor

The constructor can also be used to allocate(creating) memory while creating object. This will enable the system to allocate the correct amount of memory for each object when the objects are not of the same size, so resulting in the saving of memory. allocation of the memory to objects at the time of their construction is know as dynamic construction of objects.

The memory is allocated with new operator. in the program show the use of new , in constructors that are used to construct string in objects

CONSTRUCTOR WITH new
#include<iostream>
#include<cstring>
using namespace std;
class String
{
char *name;
int length;
public :
String ()
{
length=0;
name=new char[length+1];
}
String (char *s) // constructor 2
{
length=strlen(s);
name=new char[length+1]; //one additional
strcpy(name,s); // character for \0
}
void display()
{
std::cout<<name <<"\n";
}
void join(String &a,String &b);
}; void String:: join(String &a,String &b)
{
length=a.length+b.length;
delete name;
name=new char[length+1]; // dynamic allocation
strcpy(name,a.name);
strcat(name,b.name);
};
int main()
{
char *first="joseph";
String name1(first),name2("louis"),name3("lagrange"),s1,s2;
s1.join(name1,name2);
s2.join(s1,name3);
name1.display();
name2.display();
name3.display();
s1.display();
s2.display();
}

The output is
joseph
louis
lagrange
joseph Louis
Joseph Louis Langrange

in the above program uses two constructs the first is an empty construct that allows us to declare an array of string. The second construct initializes the length of the string, allocates necessary space ofr the string to be stored and creates the string itself, allocate necessary space for the string to be stored and created the string itself. Note That one additional character space is allocated to hold the end of string character ‘\0’

The member function join() concatenates two string. It estimates the combined length of the string to be joined, allocates memory for the combined string and then create the same using the string function strcpy() and strcat().
Note that in the function join() length members of the argument object a. The main() function program concatenates three strings into one string

The output is as shown below

joseph Louis Lagrange

CONSTRUCTING TWO DIMENSIONAL ARRAYS

We can construct matrix variable using the class type objects. The example in this program show how to construct a matrix of size m * n

CONSTRUCTING MATRIX OBJECTS
#include <iostream>
using namespace std;
class matrix
{ int **p; // pointer to matrix
int d1,d2; // dimensions
public:
matrix(int x,int y);
void get_element(int i,int j,int value)
{
p[i][j]=value;
}
int & put_element(int i,int j)
{
return p[i][j];
}
};
matrix::matrix(int x,int y)
{
d1=x;
d2=y;
p=new int *[d1]; // creates an array pointer
for ( int i=0; i < d1;i++)
{
p[i]=new int[d2]; // creates space for each row
}
}
int main()
{
int m,n;
std::cout<< "Enter size of matrix";
std::cin>> m >> n;
matrix A(m,n);
cout << "Enter matrix elements row by row \n";
int i,j,value;
for (i=0;i < m;i++)
{
for (j=0;j < n;j++)
{
std::cin>> value;
A.get_element(i,j,value);
}
}
std::cout<< "\n";
std::cout<< A.put_element(1,2);
};

The output:
Enter size of matrix:3 4
Enter matrix elements row by row
11 12 13 14
15 16 17 18
19 20 21 22

17

17 is the value of the element(1,2)

The construct first creates a vector pointer to an int of size d1. Then it allocates iteratively an int type vector of size d2 pointed at by each element p[i]. Thus space for the elements of a d1 X d2 matrix is allocated from free store as shown

two dimensional array in c++

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise