top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Can constructors be parameterized?

+2 votes
317 views
Can constructors be parameterized?
posted Jan 10, 2015 by Vrije Mani Upadhyay

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Yes, In Java and C++ Constructor can be Parameterized.

  1. Constructor can take value that is called argument.
  2. Argument can be any type e.g Integer, Char, Array or any object itself.
  3. Constructor can take any number of argument.

In C++,Constructor is automatically called when object(instance of class) create.It is special member function of the class.Which constructor has arguments thats called Parameterized Constructor.

Example:

#include<iostream>
#include<conio.h>

using namespace std;

class Example        {
    // Variable Declaration
    int a,b;
    public:

    //Constructor
    Example(int x,int y)            {
    // Assign Values In Constructor
    a=x;
    b=y;
    cout<<"Im Constructor\n";
    }

    void Display()    {
    cout<<"Values :"<<a<<"\t"<<b;
    }
};

int main()                {
        Example Object(10,20);
        // Constructor invoked.
        Object.Display();

        // Wait For Output Screen
        getch();
        return 0;
}
answer Jan 10, 2015 by Amit Kumar Pandey
...