answersLogoWhite

0


Best Answer

The copy constructor is used to initialize a new instance from an old

instance, and is called when passing variables by value into functions

or as return values out of functions.

The assignment operator is used to change an existing instance to have

the same values as the rvalue, which means that the instance has to be

destroyed and re-initialized if it has internal dynamic memory.

User Avatar

Wiki User

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

Wiki User

6y ago

A constructor initialises a new object of the class while an assignment operator modifies (or mutates) an existing object of the class. These are fundamentally different operations which require completely different implementations. In addition, initialisation is often used to establish a class invariant in which case all mutators that directly modify the object's state must also maintain the class invariant. Again, establishing and maintaining a class invariant requires completely different implementations.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between a constructor and its corresponding overloaded assignment operator?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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 is the name of the special member function which is automatically called during creation of each class object?

The constructor. The constructor instantiates the object, and can optionally take parameters and has an optional initialization phase. It has no return type, and has the same name as the class itself. The constructor can be overloaded. It cannot be virtual or constant.


An instance method or constructor may be overloaded by providing the same name and arrgument list?

You can overload instance methods and constructors (ref. Prog. Logic)


Need of constructor in c plus plus?

There is no specific keyword for a constructor in C++. Simply define and declare a method of the class with the same name as the class and it will be a constructor. A constructor with no arguments is the default constructor, a constructor with one argument of class type is the copy constructor, and a constructor with one argument of some other type is the conversion constructor. You can provide other overloaded constructors if you want.


What is a java deconstruct?

Every class has at least one it's ownconstructort. Constructor creates a instance for the class. Constructor initiates (initialize) something related to the class's methods. Constructor is the method which name is same to the class. But there are many difference between the method (function) and the Constructor. In this example we will see that how to to implement the constructor feature in a class. This program is using two classes. First class is another and second is the main class which name is Construct. In theConstruct class two objects (a and b) are created by using the overloaded another Constructor by passing different arguments and calculated the are of the different rectangle by passing different values for the another constructor.

Related questions

What are different type of constructor in java?

Every class, including abstract classes, MUST have a constructor. The different types are: a. Regular constructors b. Overloaded constructors and c. Private constructors


How will you overload a constructor?

The same way you overload any function -- you provide a new signature for it. The signature is determined by the number and type of parameters it accepts, and whether they are const or not. Every class has a public default constructor if no other constructor is declared . Also every class has a public copy constructor whether you declare one or not. So every constructor you do declare is, in fact, an overloaded constructor. Example: class myClass { public: myClass():myInt(0){} // default ctor (no parameters) myClass(const myClass & copy){myint = copy.myInt;} // copy ctor myClass(int newInt):myInt(newInt){} // overloaded ctor private: int myint; };


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 is the name of the special member function which is automatically called during creation of each class object?

The constructor. The constructor instantiates the object, and can optionally take parameters and has an optional initialization phase. It has no return type, and has the same name as the class itself. The constructor can be overloaded. It cannot be virtual or constant.


An instance method or constructor may be overloaded by providing the same name and arrgument list?

You can overload instance methods and constructors (ref. Prog. Logic)


Need of constructor in c plus plus?

There is no specific keyword for a constructor in C++. Simply define and declare a method of the class with the same name as the class and it will be a constructor. A constructor with no arguments is the default constructor, a constructor with one argument of class type is the copy constructor, and a constructor with one argument of some other type is the conversion constructor. You can provide other overloaded constructors if you want.


What is a java deconstruct?

Every class has at least one it's ownconstructort. Constructor creates a instance for the class. Constructor initiates (initialize) something related to the class's methods. Constructor is the method which name is same to the class. But there are many difference between the method (function) and the Constructor. In this example we will see that how to to implement the constructor feature in a class. This program is using two classes. First class is another and second is the main class which name is Construct. In theConstruct class two objects (a and b) are created by using the overloaded another Constructor by passing different arguments and calculated the are of the different rectangle by passing different values for the another constructor.


Can you override the constructor?

A constructor should be overloaded when there is a parameter with default value that is commonly used, or if it is possible that not all the information for creating the instance will be gathered all at once. As an example, say there is a class that describes bank accounts. Assuming I can choose to deposit or not deposit money under my name upon creating an account, there is a chance for a meaningful overload. One constructor could take in the name of the account owner and the initial balance as parameters; the overloaded constructor could just take the name as a parameter while setting the initial balance to zero.


What is overloaded constructor in oop?

Overloading a function simply means providing the same function name with different argument types. Class constructors are no different. In fact, even if you declare no constructors in a class, there will be two compiler-generated constructor overloads provided for you: a default constructor; and a copy constructor. If you declare any other constructors, the compiler-generated default constructor will no longer be generated. You must declare your own default constructor if you require one. The copy constructor is always generated, however the default implementation only performs a member-wise copy of the class members. If your class contains a pointer to allocated memory you must provide your own copy constructor to perform a deep-copy of those pointers, so each instances "owns" its own copy of the memory.


Why a friend function cannot be used to overload the assignment operator?

Assignment(=) operator is a special operator that will be provided by the constructor to the class when programmer has not provided(overloaded) as member of the class.(like copy constructor). When programmer is overloading = operator using friend function, two = operations will exists: 1) compiler is providing = operator 2) programmer is providing(overloading) = operator by friend function. Then simply ambiguity will be created and compiler will gives error. Its compilation error.


Example of constructor in java?

Every class, including abstract classes, MUST have a constructor. Hard Code that into your brain. But just because a class must have one, doesn't mean the programmer has to type it. A constructor looks like this: class Car { Car() { } // The constructor for the Car class } You notice anything missing in the declaration above? There's no return type! Two key points to remember about constructors are that they have no return type and their names must exactly match the class name. Typically, constructors are used to initialize instance variable state, as follows: class Car { int size; String name; Car(String name, int size) { this.name = name; this.size = size; } } In the preceding code example, the Car class does not have a no-arg constructor. That means the following will fail to compile: Car f = new Car(); // Won't compile, no matching constructor but the following will compile: Car f = new Car("Ford", 43); // No problem. Arguments match // the Car constructor. So it's very common for a class to have a no-arg constructor, regardless of how many other overloaded constructors are in the class (constructors can be overloaded just like methods)


How do you overload constructors in java?

Overloading a constructor means typing in multiple versions of the constructor, each having a different argument list, like the following examples: class Car { Car() { } Car(String s) { } } The preceding Car class has two overloaded constructors, one that takes a string, and one with no arguments. Because there's no code in the no-arg version, it's actually identical to the default constructor the compiler supplies, but remember-since there's already a constructor in this class (the one that takes a string), the compiler won't supply a default constructor. If you want a no-arg constructor to overload the with-args version you already have, you're going to have to type it yourself, just as in the Car example. Overloading a constructor is typically used to provide alternate ways for clients to instantiate objects of your class. For example, if a client knows the Car name, they can pass that to a Car constructor that takes a string. But if they don't know the name, the client can call the no-arg constructor and that constructor can supply a default name