Assignment operators overloading in C++

I saw an example about how can you switch the information between two objects. I understood the first example, but I wanted to make another example, and don't work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  #include <iostream>
using namespace std;
 
class Distance
{
   private:
      int feet;             // 0 to infinite
      int inches;           // 0 to 12
   public:
      // required constructors
      Distance(){
         feet = 0;
         inches = 0;
      }
      Distance(int f, int i){
         feet = f;
         inches = i;
      }
      void operator=(const Distance &D )
      { 
         feet = D.feet;
         inches = D.inches;
      }
      // method to display distance
      void displayDistance()
      {
         cout << "F: " << feet <<  " I:" <<  inches << endl;
      }
      
};
int main()
{
   Distance D1(11, 10), D2(5, 11);

   cout << "First Distance : "; 
   D1.displayDistance();
   cout << "Second Distance :"; 
   D2.displayDistance();

   // use assignment operator
   D1 = D2;
   cout << "First Distance :"; 
   D1.displayDistance();

   return 0;
}

#include<iostream>
#include<string.h>
using namespace std;
class Student{
private:
    char firstname[56];
    char secondname[56];
public:
    Student(char*name,char*sname);
     Student operator=(const Student& a,const Student& b)
    {
        a.firstname=b.firstname;
        a.secondname=b.secondname;
    }
    friend void show(Student ex);
};
void show(Student ex)
{
    cout<<"First name: "<<ex.firstname<<endl;
    cout<<"Second name: "<<ex.secondname<<endl;
}
Student::Student(char*name,char*sname)
{
    strcpy(Student::firstname,name);
    strcpy(Student::secondname,sname);
}
int main()
{
    Student d1("Luca","Cas"),d2("Oliver","Paul");
    show(d1);
    cout<<endl;
    show(d2);
    cout<<endl;
    d1=d2;
    cout<<endl;
    show(d1);
    return 0;
}




Last edited on
closed account (48bpfSEw)
Did you listen to the compiler's output?

11:57: error: 'Student Student::operator=(const Student&, const Student&)' must take exactly one argument In function 'int main()':

30:28: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

30:28: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

30:48: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

30:48: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
I saw the warnings, but my main problem it's this error. I tried something, but don't work either.
Hi,
I am confused there are two functions main(). Which one do you choose?
Hi. :D
Are two examples, the first examples work with numbers, from the second I wanted to see If he can work with strings.
1
2
3
4
5
Student operator=(const Student& a,const Student& b)
    {
        a.firstname=b.firstname;
        a.secondname=b.secondname;
    }

Only one parameter is allowed. That is why your first example works.

Change it to :
1
2
3
4
5
void operator=(const Student& a)
    {
        firstname=a.firstname;
        secondname=a.secondname;
    }
Does it help you? :)
I tried once, didn't work. Tell me "invalid array assignment"
Don't use char c-array. Use std::string instead.

You just forgot that they are not std::string. That is why you need to write :
1
2
3
4
5
void operator=(const Student& a)
    {
        strcpy(firstname, a.firstname);
        strcpy(secondname, a.secondname);
    }

Does it help? :)
What fool I am. Yeee, works. Thank you a lot m8. I forgot to use strcpy....
Have a good day :)
Topic archived. No new replies allowed.