Can't add 2 numbers from different objects

I have two objects each has a number..I wanna add these numbers using operator+ overloading
Thnx in advance :)
You'll need to post something, otherwise we can't really help you much besides agreeing that yes, you'll need to use operator overloading.
#include <iostream>
using namespace std;
class Class {
int a,b;
public :
friend istream& operator>>(istream& in,Class& p);
friend ostream& operator<<(ostream& out,Class& p);

Class& operator+(const Class& p)
{
// don't know what i should write here
return *this;
}

};
istream& operator>>(istream& in,Class& p)
{
in>>p.a>>p.b;
return in;
}
ostream& operator<<(ostream& out,Class& p)
{
out<<p.a<< " " <<p.b;
cout << endl;
return out;
}
int main()
{
Class m1,m2,*p;
p = &m1;
cin >> m1;
p = &m2;
cin >> m2;
cout << m1 << m2;
// I wanna print m1.a+m2.a and m1.b+m2.b
return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

struct volume
{
    int lit ;
    int ml ;
};

volume operator+ ( volume a, volume b )
{
    const int ml = a.ml + b.ml ;
    return { a.lit + b.lit + ml/1000, ml%1000 } ;
}

std::ostream& operator<< ( std::ostream& stm, volume vol )
{ return stm << "( " << vol.lit << "L " << vol.ml << "ml )" ; }


int main()
{
      volume a { 12, 650 } ;
      volume b { 29, 723 } ;
      std::cout << a << " + " << b << " == " << a+b << '\n' ;
}

http://ideone.com/K3td36
Great..thnx :)
Topic archived. No new replies allowed.