answersLogoWhite

0


Best Answer

A constructor differs from a normal member function in three ways:

  1. A constructor never returns a result. The constructor's declaration reflects this by not even declaring the function as "void."

    The common design hypothesis is that a well-designed constructor cannot fail, other than maybe in an irrecoverable way (such as a fatal running out of memory).

  2. A constructor is never called explicitly except with the new operator.
  3. Constructors impose further restrictions. For example, they cannot be declared abstract or virtual, and may have visibility requirements.

    The common design practise is that at least the default constructor is declared public.

User Avatar

Wiki User

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

Wiki User

8y ago

A constructor is not a function per se, but it is a member. A function differs from a constructor in that a function must have a return type while constructors have no return type (not even void).

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How constructor is different from normal member function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you differentiate between a member function and normal function?

A normal function is any function that is not a member of any class. Normal functions that operate upon a class are referred to as non-member functions, however a non-member function can also be a member of another class. Any class may declare any non-member function to be a friend of the class, in which case the function becomes a friend function.A member function is a member of a class and may be declared static or non-static. Non-static member functions have the following 3 properties:Private access to the class members.Scoped to the class.Must be invoked against an object of the class (has a 'this' pointer).Static member functions have the first two properties only while friend functions have the first property only. Non-member functions that are not friends of the class have none of these properties.


Why you don't use void in constructor function although it doesn't return any value?

Return a value in the constructor is senseless, because a constructor is used to initialize a object instance and not to perform a task or a operation. When we call a constructor we used a sentence like this: MyClass var = new MyClass(); Then when we execute the above line the constructor return ('create') a new object of the type 'MyClass'. If this call could return an other type, for example an Integer, the constructor is considered a normal method, and if there are not more constructors a empty default constructor for MyClass is defined by the java compiler. public class MyClass{ // The java compiler will insert a real constructor here public Integer MyClass(){ //This isn't a constructor, only a simple method return new Integer(1); } }


What is the prototype of a copy constructor for a class X?

class X { public: X(); // default constructor X(const X& x); // copy constructor // ... }; int main(void) { X objx1; //normal ctor X objx2 = x1; // copy ctor X x3(x2); // copy ctor }


What is mean constructor in java?

A parameterized constructor in java is just a constructor which take some kind of parameter (variable) when is invoked. For example. class MyClass { //this is a normal constructor public MyClass(){ //do something } //this is a parameterized constructor public MyClass(int var){ //do something } //this is another parameterized constructor public MyClass(String var, Integer var2){ //do something } }


Can abstract class have constructors?

A constructor of a class in invoked when a object of that class is created. As an abstract class can't have an object, so we can't create a constructor of the abstract class. But we can create a constructor of a concrete subclass of that abstract class and we have to pass the object of that concrete subclass to the abstract class.

Related questions

What is the difference between friend function and normal member function?

We can access a Friend function from any other class in which friend function is introduced or declared even if the other class is not a member of first class. But when we use normal member function, we can have its access only in the derived classes of the first class. This is the basic difference between a friend function and a normal member function.


How do you differentiate between a member function and normal function?

A normal function is any function that is not a member of any class. Normal functions that operate upon a class are referred to as non-member functions, however a non-member function can also be a member of another class. Any class may declare any non-member function to be a friend of the class, in which case the function becomes a friend function.A member function is a member of a class and may be declared static or non-static. Non-static member functions have the following 3 properties:Private access to the class members.Scoped to the class.Must be invoked against an object of the class (has a 'this' pointer).Static member functions have the first two properties only while friend functions have the first property only. Non-member functions that are not friends of the class have none of these properties.


Why you don't use void in constructor function although it doesn't return any value?

Return a value in the constructor is senseless, because a constructor is used to initialize a object instance and not to perform a task or a operation. When we call a constructor we used a sentence like this: MyClass var = new MyClass(); Then when we execute the above line the constructor return ('create') a new object of the type 'MyClass'. If this call could return an other type, for example an Integer, the constructor is considered a normal method, and if there are not more constructors a empty default constructor for MyClass is defined by the java compiler. public class MyClass{ // The java compiler will insert a real constructor here public Integer MyClass(){ //This isn't a constructor, only a simple method return new Integer(1); } }


What is the prototype of a copy constructor for a class X?

class X { public: X(); // default constructor X(const X& x); // copy constructor // ... }; int main(void) { X objx1; //normal ctor X objx2 = x1; // copy ctor X x3(x2); // copy ctor }


How do we invoke a constructor?

In Java, objects are constructed. Every time you make a new object, at least one constructor is invoked. Every class has a constructor, although if you don't create one explicitly, the compiler will build one for you. Ex: class Test { public Test() { } // this is Test's constructor public void Test() { } // this is a badly named, // but legal, method } If you see the example above, you would have realized that the constructor looks a lot like methods. Below are the main distinguishing factors between the constructor and normal methods: 1. The Constructor's name is exactly the same as the name of the class 2. They do not have a return type (Please remember this. A Constructor cannot have a return type as part of the code) 3. Constructors cannot be static, abstract or final


What is mean constructor in java?

A parameterized constructor in java is just a constructor which take some kind of parameter (variable) when is invoked. For example. class MyClass { //this is a normal constructor public MyClass(){ //do something } //this is a parameterized constructor public MyClass(int var){ //do something } //this is another parameterized constructor public MyClass(String var, Integer var2){ //do something } }


How is a ministroke different from a stroke?

Unlike in a true stroke, normal brain function will return with 24 hours


What is the normal blood system function?

Describe the normal function of the hematic system


Is COUNTIF an example of a logical function in Excel?

Yes. It combines the logical function IF with the normal COUNT function, so that it counts values that meet a condition.Yes. It combines the logical function IF with the normal COUNT function, so that it counts values that meet a condition.Yes. It combines the logical function IF with the normal COUNT function, so that it counts values that meet a condition.Yes. It combines the logical function IF with the normal COUNT function, so that it counts values that meet a condition.Yes. It combines the logical function IF with the normal COUNT function, so that it counts values that meet a condition.Yes. It combines the logical function IF with the normal COUNT function, so that it counts values that meet a condition.Yes. It combines the logical function IF with the normal COUNT function, so that it counts values that meet a condition.Yes. It combines the logical function IF with the normal COUNT function, so that it counts values that meet a condition.Yes. It combines the logical function IF with the normal COUNT function, so that it counts values that meet a condition.Yes. It combines the logical function IF with the normal COUNT function, so that it counts values that meet a condition.Yes. It combines the logical function IF with the normal COUNT function, so that it counts values that meet a condition.


Is a static member function similar to a friend function?

No. To understand how friend functions relate to static functions you first need to understand the three qualities of a normal member function (an instance member function): 1. The function has access to the private aspects of the class in which it is declared. 2. The function is scoped to that class. 3. The function must be invoked on or from within an object of that class. A static function only has the first two qualities (so no instance of the class is required) while a friend function only has the first quality (so is neither scoped to the class nor requires an instance of the class). All three share the first quality only but that alone does not make them similar. It is the qualities they lack that sets them apart.


Difference between normal function inline function?

gffg


Can abstract class have constructors?

A constructor of a class in invoked when a object of that class is created. As an abstract class can't have an object, so we can't create a constructor of the abstract class. But we can create a constructor of a concrete subclass of that abstract class and we have to pass the object of that concrete subclass to the abstract class.