What is difference between initialization and assignment?
Answers were Sorted based on User's Feedback
Answer / janet
Assignment can be done as many times as desired where as
initialization can be done only once.
| Is This Answer Correct ? | 166 Yes | 24 No |
Answer / jaroosh
Those answers are actually highly insufficient.
Here are some differences to point that come to my mind :
a) you CANT assign to a const variable, whereas you CAN
initialize it. For example :
const AXC d = 3; //OK! initialization
d = 3; //WRONG! cannot assign to const
b) initialization IS about creating object
assignment IS about setting some value to object
This is why in the following code :
AXC d = 3;
AXC x;
x = 2;
first line will require an appropriate constructor:
AXC(int i) { ... }
whereas the third line will use overloaded assignment "="
operator if its specified (if not, it will use constructor
like above):
AXC operator =(int x) { ... }
c) Initialization also calls copy constructors, while
assignment does not :
A c;
A d = c; //Calls copy constructor of A
A e;
e = c; //Calls assignment operator of A
| Is This Answer Correct ? | 71 Yes | 5 No |
Answer / hariharan,k
initialization mean's to initilise a variable.
assignment mean's assign value to a variable.
| Is This Answer Correct ? | 73 Yes | 41 No |
Answer / suraj
Initialization is a type of assignment done at declaration
level, but assinment can be done anywhere.
| Is This Answer Correct ? | 23 Yes | 4 No |
Answer / lucky
Some defference between Initialization and assignment
Initialization means whenever we initialize any var. at
declaration time.So as we initialize var. in init() in java
programming.For i.e.
prblic init()
{
int a=10;
}
Assignment means whenever we assigne any value in any var.
except const. variable.For i.e.
int a,b;
a=15; //right
b=a; //right
const c;
c=10 or c=a; //Worng
const d=20; //that is initialization
| Is This Answer Correct ? | 22 Yes | 7 No |
Answer / sandeep mannarakkal
All object creation involves two steps,
1) Memory allocation
2) Memory initialization , so initialization is a part of object creation itself.
Assignment is the process of assigning values in to already available object.
Hope the concept is clear
| Is This Answer Correct ? | 4 Yes | 0 No |
Answer / sweety
Initialization does the memory allocation, i.e. it assigns
the emory to the variable whereas assignment assigns a
value to the variable.
| Is This Answer Correct ? | 15 Yes | 16 No |
Answer / adeolu
initialization is the first assignment value given to a
variable while assignment is the subsequent value giving to
the program variable
| Is This Answer Correct ? | 2 Yes | 7 No |
What is iomanip c++?
an operation between an integer and real always yeilds a) integer result b) real result c) float result
What is the difference between ++ count and count ++?
What is the cout in c++?
Write a program to swap 2 chars without using a third varable? char *s = "A"; char *p = "B";
write a programming using for loop in c++ to generate diamond trangel?
What are templates? where we should use it?
Explain class invariant.
Why should we use null or zero in a program?
Explain what is polymorphism in c++?
5. Can inline functions have a recursion?
class X { private: int a; protected: X(){cout<<"X constructor was called"<<endl;} ~X(){cout<<"X destructor was called"<<endl} }; Referring to the code above, which one of the following statements regarding "X" is TRUE? a) X is an abstract class. b) Only subclasses of X may create X objects. c) Instances of X cannot be created. d) X objects can only be created using the default copy constructor. e) Only friends can create instances of X objects.