operator overloading

Hi, Everybody

Describe the class to work the following code:

A a1(2);
A a2 = 3;
a1 += a2;

Excuse my English language.

Thanks
I don't get it.. are you checking to see if WE know the answer?
Think it over, there are only so-many possabilities.
And sometimes even checking them all and seeing what works and what doesn't work is a great learning experience.
Anyhow, Good luck, i doubt anyone will answer this..
I do not know exactly my task to write a program that worked for this code....

class A {
public:
...
A & operator=(const A &rhs)
{
//Please write your codу here..
}
...
};

int main()
{

A a1(2);
A a2 = 3;
a1 += a2;
return 0;
}
Last edited on
You need a constructor that takes an int for the first line to compile.
You need a copy constructor for the second line (the default one provided by the compiler should be fine).
You need operator+= that takes another instance of the class for the third line to compile.

Now what operator+= is supposed to do, I don't know.
Last edited on
Please use CODE tags...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class A 
{
public:
   ...
   A & operator=(const A &rhs)
   {
       //Please write your codу here..
   }
   ...
};

int main()
{
   A a1(2);
   //THe operator overload above A& operator=(..) is used in this assignment below
   A a2 = 3;
  a1 += a2;
  return 0;
}


Now you need to create a constructor and overload the "+=" operator. As well as implement them all.

http://cplusplus.com/doc/tutorial/classes/
The operator += works like this:
1
2
variable = variable + 52;
// or you could use:    variable += 52; 
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
class A 
{
private:

int t;

public:

A::A (int a) { //Default constructor
  t = a;
 
}
   
A::A (const A& rv) { //Copy constructor
  t=rv.t;   
 }

   A & operator=(const A &rhs) 
   {
       //Please write your codу here..  I don't know
   }

  A & A::operator+=(const A &rhs) {
      
//Please write your codу here..  I don't know

    return *this;
  } 

};

int main()
{
   A a1(2);
   //THe operator overload above A& operator=(..) is used in this assignment below
   A a2 = 3;
  a1 += a2;
  return 0;
}
Last edited on
Use CODE Tags.

Now implement the parts that say "//Pease Write your cody here."

How do you assign a member variable? Hint....
 
t = rhs.t
Last edited on
iharrold I agree with you. thank you!


How do you assign a member variable? Hint....
t = rhs.t

Below written, but i didn't sure, that is true or false?:
http://www.cs.caltech.edu/courses/cs11/material/cpp/donnie/cpp-ops.html
Topic archived. No new replies allowed.