-->

Friday, 14 July 2017

Dynamic constructor c++
Dynamic constructor

Image result for dynamic constructor logo

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 :


dc

keep calm and say bujuku bujuku.

0 comments:

Post a Comment

Start Work With Me

Contact Us
KUTTY SELVA
+91 7708139984
Madurai,Tamilnadu