Dynamic constructor


Dynamic constructor


This constructor is used to allocate the memory to the objects at the run time.
The memory allocation to objects is allocated with the help of 'new' operator.

By using this constructor, we can dynamically initialize the objects.

Example :

#include <iostream.h>
#include <conio.h>

class Account
{
private:

int account_no;
int balance;

public :

Account(int a,int b)
{
account_no=a;
balance=b;
}

void display()
{
cout<< "\nAccount number is : "<< account_no;
cout<< "\nBalance is : " << balance;
}
};

void main()
{
clrscr();
int an,bal;

cout<< "Enter account no : ";
cin >> an;

cout<< "\nEnter balance : ";
cin >> bal;

Account *acc=new Account(an,bal); //dynamic constructor

acc->display(); //'->' operator is used to access the method

getch();
}


Output :


7 comments:

Unknown said...

thanks....

Unknown said...

Thank u for the help

Unknown said...

Thank u for the help

Unknown said...

T q for help,

Unknown said...

T q for help,

Unknown said...
This comment has been removed by the author.
Unknown said...

How to implement with an array

Post a Comment