We know that class is a data type which means whatever you can do with two integers, you can do with two objects of class. e.g.
If we can write
then we should be able to do same with class objects. e.g.
but what does line number 11 and 12 mean? In line number 11,we are saying student1's value should be assigned to student3. but what does it really mean? Should all of the three data member's value of student1 be assigned to student3's. but this will be wrong since two students can have same name and/or marks but their roll numbers should/can not be same. So where would you write this logic where you can define the definition of assignment(by definition,what I mean is that do you want to just assign marks of one student to another or name or both) i.e. what should happen in line number 11 and 12.
you will have to write this logic in copy constructor and assignment operator. so Let us first understand these two :
If we can write
1 2 3 4 | int a = 2; int b = 3; int c = a; b = c; |
then we should be able to do same with class objects. e.g.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class student { char name[100]; float marks; int roll_num; }; int main() { student student1; student student2; student student3 = student1; student2 = student3; student student4 = student1; } |
but what does line number 11 and 12 mean? In line number 11,we are saying student1's value should be assigned to student3. but what does it really mean? Should all of the three data member's value of student1 be assigned to student3's. but this will be wrong since two students can have same name and/or marks but their roll numbers should/can not be same. So where would you write this logic where you can define the definition of assignment(by definition,what I mean is that do you want to just assign marks of one student to another or name or both) i.e. what should happen in line number 11 and 12.
you will have to write this logic in copy constructor and assignment operator. so Let us first understand these two :
- Copy Constructor : As the name suggests, it is a constructor i.e. it is a function which is called when an object is created. But it is a special constructor that takes object of its own type as an argument (to be more precise, constant reference of object of its own type)
- Assignment operator : It is a function that defines the behavior of assignment of two class objects. It is basically operator overloading of assignment operator.
Now from this definition, it may seem to you that at line number 11 and 12 assignment operator is being called but actually at line number 11 copy constructor is called and at line number 12 assignment operator is being called.
When you assign a class object to another object while construction/declaration (as shown in line number 11 which is equivalent to line number 3 in first figure),copy constructor is called but if you choose to assign later(i.e. after construction, at line number 12 which is equivalent to line number 4 in first figure), assignment operator is called.
Now it should be clear from the definitions that at line number 13, copy constructor will be called. It looks like a normal constructor call that takes student object as its argument Hence it is copy constructor.
How to define copy constructor and assignment operator in a class
How to define copy constructor and assignment operator in a class
1 2 3 4 5 6 7 8 9 10 11 12 13 | class student { char name[100]; int roll_no; int marks; public : student(const student &rhs) //copy constructor { marks = rhs.marks; } void operator=(const student &rhs) // assignment operator { strcpy(name,rhs.name); roll_no = rhs.roll_no; } }; |
If you carefully notice, in the copy constructor I have just copied the marks but not the name and in the assignment operator, I have copied both field. so at line number 11 (student student3 = student1) only marks will be copied but at line number 12 student2 = student3, name as well as marks will be copied.
Did you know?
when you pass an object by value in a function argument, its copy constructor is called.
e.g.
void func(student s1);
Did you know?
when you pass an object by value in a function argument, its copy constructor is called.
e.g.
void func(student s1);