Types Of Constructors:
  1. Default Constructor
  2. Parameterized Constructor
1.Default Constructor:

  • 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.
Syntax:
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
  1. By calling the constructor explicitly.
  2. By calling the constructor implicitly.

Example:
  • explicit call
codezword int a = codezword(0,15);  //explicit call
  • implicit call
codezword  int a(0,15);  // 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:    

#include <iostream>
Using namespace std;
Class construct
{
Public:
    Int a, b;
    Construct()
    {
        A = 10;
        B = 20;
    }
};
Int main()
{
    Cout << “W.A.P. that illustrates the concept of constructor.\n\n\n”;
    Construct c;
    Cout << “a: “ << c.a << endl
         << “b: “ << c.b;
    Return 0;
}

Output:


0 Comments