Parameterized Constructor
Types Of Constructors:
- Default Constructor
- Parameterized Constructor
1.Default Constructor:
class codezword
{
.....................
.....................
.....................
codezword(); //default constructor
.....................
......................
};
coedzword :: codezword()
{
.....................
.....................
}
2. Parameterized Constructor:
Example:
{
.....................
.....................
int c,d;
.....................
public:
codezword(int a , int b); //perameterized constructor
.....................
......................
};
coedzword :: codezword(int a, int b)
{
.....................
c=a; d=b;
.....................
}
Code:
- Default Constructor invoke automatically when objects created.
- Default Constructor have no argument.
- Default Constructor follows all following ruels.
- Constructor should be declare in public section.
- Constructor are invoked automatically when object is created.
- Constructor do not have any datatype.
- Constructor cannot be inherited.
- Constructor have default argument like functions.
- Constructor cannot be virtual.
class codezword
{
.....................
.....................
.....................
codezword(); //default constructor
.....................
......................
};
coedzword :: codezword()
{
.....................
.....................
}
2. Parameterized Constructor:
- The constructors that can take arguments are called parameterized constructor.
- We must pass the initial values as arguments to the constructor function when an object is declared.
- We can pass the value by two ways
- By calling the constructor explicitly.
- By calling the constructor implicitly.
Example:
- explicit call
- implicit call
Syntax:
class codezword{
.....................
.....................
int c,d;
.....................
public:
codezword(int a , int b); //perameterized constructor
.....................
......................
};
coedzword :: codezword(int a, int b)
{
.....................
c=a; d=b;
.....................
}
Code:
0 Comments