Parameterized constructor
Parameterized constructor
The constructor which take parameters is called parameterized constructor.
This constructor allows us to initialize the various data members of different objects with different value when they are constructed. This is done by passing different values as
arguments to the constructor function, when the objects are created.
Syntax :
class class-name
{
private :
data member;
public :
class-name(list of parameters) //parameterized constructor
{
}
};
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<< "Account number is : "<< account_no;
cout<< "\nBalance is : " << balance;
}
};
void main()
{
clrscr();
Account acc(121,5000);
acc.display();
getch();
}
Output :
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment