answersLogoWhite

0

What is a argument constructor?

Updated: 8/10/2023
User Avatar

Wiki User

12y ago

Best Answer

The value what we pass in the constructor is know as argument.you can look into the example given below to make it more clear.

class Test

{

Test(int a,int b)

{

int c=a+b;

System,out.println("the value of "+c);

}

}

public class testMain{

Public static void main(String args[])

{

Test t=new Test(10,20);

//here 10 and 20 are the arguments and a,b are parameters....

}

}

i hope this will help u

Kumar Ashish

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

An Argument Constructor is one that takes a value as argument and uses it while initializing a class.

Ex:

class Car {

int size;

String name;

Car(String name, int size) {

this.name = name;

this.size = size;

}

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

A constructor that has arguments in it.

Ex:

class Car {

Car() { }

Car(String s) { }

}

The line in bold is the argument constructor and the other is the default (no-arg) constructor

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a argument constructor?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

When a object is passed to a function as argument why it is not generating an error if the parameterized constructor is defined and not the default constructor?

default constructor is used only when the programmer does not use a constructor to initialize objects. Once the programmer defines a constructor then the default constructor is no longer used


Default constructor in java?

If you don't type a constructor into your class code, a default constructor will be automatically generated by the compiler. The default constructor is ALWAYS a no-arg constructor. (Obviously the compiler has no clue what all arguments you might want for your class. So it takes the safe way out with a no argument constructor) A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you're free to put in your own no-arg constructor.


When invoking a constructor from subclass its super class's no-arg constructor is always invoked?

Implicitly: (i.e., you do not code for it, but works as if you did)calling the no-argument constructor of the subclass, and there is no explicitly "redirect" codes.Explicitly:a constructor with base() / super() in the implementation, even that invoked constructor required some arguments.C# example: public SubClass(string whatever) : base() {//...}


What is the method of constructor overloading in c plus plus?

Constructor overloading, just like any function's overloading, is where more than one configuration of parameters exists for the function. Based on the number and type of the parameters, different versions of the function can be resolved by the linker. This is typically used in the constructor as the default constructor (no parameters), the copy constructor (one reference parameter of the same type as the class), and the conversion constructor (any other combination of parameters).


What are the properties of class in c plus plus?

A constructor is not a function. A function is a type, as specified by its return type, and must return a value of that type unless the type is void. A constructor does not return anything, not even void. The purpose of a constructor is to both allocate and initialise memory for an object of the type being constructed. If a valid object cannot be constructed for any reason, the constructor must throw an exception. If the object's class has no data members (attributes), the class does not require a constructor. This is typically the case for most abstract data types and base classes which are used purely as interfaces. Constructors differ from functions in that all constructors have an initialisation section that is used specifically to initialise non-static data members. The body of the constructor is rarely used except to perform initialisations that cannot be more easily performed by the initialisation section. A class may have more than one constructor to provide alternative methods of construction based upon the number and type of arguments supplied (if any). When no arguments are required or all arguments have default values then the constructor is known as the default constructor. If the constructor has only one argument the constructor is known as a conversion constructor (because the argument is converted to an object of the class). However, if the constructor argument is a constant reference to an object of the same class, then it is known as a copy constructor, and when the constructor argument is an rvalue reference, it is known as a move constructor. If copy and/or move constructors are provided for a class, the equivalent assignment operators should also be provided for that class. All other constructors are known as user-defined constructors.

Related questions

What is the method of constructor overloading?

The first thing to note about constructor overloading is that Java creates a no argument constructor for you if and only if you have not typed a constructor yourself. Every class has a constructor even abstract ones (default no argument constructor). Abstract constructors are always executed. To overload a constructor you can do the following: class Test { String name; Test(String n) { name = n; System.out.println("Constructing Test Object named: " + name); } } In the case above we are overloading the default no argument constructor with a constructor that takes a String parameter. You can write you own no argument constructor as follows: class Test { Test() { System.out.println("Constructing Test Object"); } } To override our own no argument constructor we do this: class Test { Test() { // our no argument constructor System.out.println("Constructing Test Object"); } String name; Test(String n) { // overloading our no argument constructor with this // constructor that take a String parameter name = n; System.out.println("Constructing Test Object named: " + name); } }


What happens when no constructor function is declared in a class - in Java?

When any constructor is deffined in your class, the java compiler create a default no argument constructor for you. This constructor only have an invocation to the super class constructor (" super( ) ").


When a object is passed to a function as argument why it is not generating an error if the parameterized constructor is defined and not the default constructor?

default constructor is used only when the programmer does not use a constructor to initialize objects. Once the programmer defines a constructor then the default constructor is no longer used


Default constructor in java?

If you don't type a constructor into your class code, a default constructor will be automatically generated by the compiler. The default constructor is ALWAYS a no-arg constructor. (Obviously the compiler has no clue what all arguments you might want for your class. So it takes the safe way out with a no argument constructor) A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you're free to put in your own no-arg constructor.


When invoking a constructor from subclass its super class's no-arg constructor is always invoked?

Implicitly: (i.e., you do not code for it, but works as if you did)calling the no-argument constructor of the subclass, and there is no explicitly "redirect" codes.Explicitly:a constructor with base() / super() in the implementation, even that invoked constructor required some arguments.C# example: public SubClass(string whatever) : base() {//...}


What is constructor and discuss different types of constructor with syntax and examples?

A constructor is a class method which initialises an object of the class at the point of instantiation. Specifically, it initialises the base classes (if any) and the non-static data members (if any). Constructors also play a central role in the resource acquisition is initialisation (RAII) paradigm. Objects which have a natural default value have a default constructor. The default constructor is a constructor that has no arguments or where all arguments have default values. Objects which can be copied have a copy constructor. The copy constructor has just one non-default argument, a const l-value reference of the same type as the class. Objects which can be moved have a move constructor. The move constructor has just one non-default argument, a modifiable r-value reference of the same type as the class. All other constructors that have only one argument of a type other than the class itself are known as conversion constructors. Constructors can also have more than one argument. No specific name is given to these constructors. Other than physical memory constraints, there is no limit to the number of constructors that may be defined for a class.


Is constructor created automatically in java?

There is a default constriuctor that takes no argument for every class that extends Object.


What is the method of constructor overloading in c plus plus?

Constructor overloading, just like any function's overloading, is where more than one configuration of parameters exists for the function. Based on the number and type of the parameters, different versions of the function can be resolved by the linker. This is typically used in the constructor as the default constructor (no parameters), the copy constructor (one reference parameter of the same type as the class), and the conversion constructor (any other combination of parameters).


What are the properties of class in c plus plus?

A constructor is not a function. A function is a type, as specified by its return type, and must return a value of that type unless the type is void. A constructor does not return anything, not even void. The purpose of a constructor is to both allocate and initialise memory for an object of the type being constructed. If a valid object cannot be constructed for any reason, the constructor must throw an exception. If the object's class has no data members (attributes), the class does not require a constructor. This is typically the case for most abstract data types and base classes which are used purely as interfaces. Constructors differ from functions in that all constructors have an initialisation section that is used specifically to initialise non-static data members. The body of the constructor is rarely used except to perform initialisations that cannot be more easily performed by the initialisation section. A class may have more than one constructor to provide alternative methods of construction based upon the number and type of arguments supplied (if any). When no arguments are required or all arguments have default values then the constructor is known as the default constructor. If the constructor has only one argument the constructor is known as a conversion constructor (because the argument is converted to an object of the class). However, if the constructor argument is a constant reference to an object of the same class, then it is known as a copy constructor, and when the constructor argument is an rvalue reference, it is known as a move constructor. If copy and/or move constructors are provided for a class, the equivalent assignment operators should also be provided for that class. All other constructors are known as user-defined constructors.


What is parameter constructor?

C++ permits us to achieve this objects bt passing argument to the constructor function when the object are created . The constructor that can take arguments are called parametrized constructors Example:- class abc { int m,n; public: abc(int x,int y); //paramererise constructor ................ ................. }; abc::abc(int x,int y) { m=x;n=y; }


How do you call multiple constructors with single object?

You can have any number of constructors for a class. All we need to do is implement constructor overloading. Ex: let us say we want to create multiple constructor for a class Test Public class Test { Public Test() { //code } Public Test(int vals) { //code } Public Test(String val) { //code } }


Using default constructor in inheritance and in what reason it throws error?

The compiler places a default no-arg constructor in any java class that does not have an explicit constructor coded into it. for ex: public class Car { ... ... //lots of code but no constructor } In the above case, the compiler will place the below constructor into the code: public Car() { super(); } But, if you have a constructor in your class that takes arguments then the compiler will not put the default constructor. Ex: public class Car { public Car(String name){ ... } ... //lots of code } Above, we have a Car constructor that takes a string name as argument. so, the compiler wont put the default constructor in the code. now, if you try to do: Car obj = new Car(); you will get an error because this constructor is not defined.