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 :
Subscribe to:
Post Comments (Atom)
7 comments:
thanks....
Thank u for the help
Thank u for the help
T q for help,
T q for help,
How to implement with an array
Post a Comment