51 - More on Operator Overloading

#include 
#include "Sally.h"
using namespace std;

int main()
{
    Sally a(34);
    Sally b(21);
    Sally c;

    c = a+b;
    cout << c.num << endl;
    system("pause");
}

#include "Sally.h"
#include 
using namespace std;

Sally::Sally()
{
}

Sally::Sally(int a)
{
    num = a;
}

Sally Sally::operator+(Sally aso)
{
    Sally brandNew;
    brandNew.num = num + aso.num;
    return(brandNew);
}

#ifndef Sally_H
#define Sally_H

class Sally
{
public:
    int num;
    Sally();
    Sally(int);
    Sally operator+(Sally);
};

#endif // Sally_H

你可能感兴趣的:(51 - More on Operator Overloading)