// Define + and = for the ThreeD class.
#include <iostream>
using namespace std;
class ThreeD {
int x, y, z; // 3-D coordinates
public:
ThreeD() { x = y = z = 0; }
ThreeD(int i, int j, int k) { x = i; y = j; z = k; }
ThreeD operator+(ThreeD op2); // op1 is implied
ThreeD operator=(ThreeD op2); // op1 is implied
void show() ;
};
// Overload +.
ThreeD ThreeD::operator+(ThreeD op2)
{
ThreeD temp;
temp.x = x + op2.x; // These are integer additions
temp.y = y + op2.x; // and the + retains is original
temp.z = z + op2.z; // meaning relative to them.
return temp;
}
// Overload assignment.
ThreeD ThreeD::operator=(ThreeD op2)
{
x = op2.x; // These are integer assignments
y = op2.y; // and the = retains its original
z = op2.z; // meaning relative to them.
return *this;
}
// Show X, Y, Z coordinates.
void ThreeD::show()
{
cout << x << \", \";
cout << y << \", \";
cout << z << \"\\n\";
}
int main()
{
ThreeD a(1, 2, 3), b(10, 10, 10), c;
cout << \"Original value of a: \";
a.show();
cout << \"Original value of b: \";
b.show();
cout << \"\\n\";
printf(\"======================================\\n\");
c = a + b; // add a and b together // 11, 12, 13
cout << \"Value of c after c = a + b: \";
c.show();
cout << \"\\n\";
c = a + b + c; // add a, b and c together
cout << \"Value of c after c = a + b + c: \";
c.show();
cout << \"\\n\";
c = b = a; // demonstrate multiple assignment
cout << \"Value of c after c = b = a: \";
c.show();
cout << \"Value of b after c = b = a: \";
b.show();
return 0;
}
----------------------
2번코드
// Demonstrate prefix and postfix ++.
#include <iostream>
using namespace std;
class ThreeD {
int x, y, z; // 3-D coordinates
public:
ThreeD() { x = y = z = 0; }
ThreeD(int i, int j, int k) {x = i; y = j; z = k; }
ThreeD operator++(); // prefix version of ++
ThreeD operator++(int notused); // postfix version of ++
void show() ;
};
// Overload the prefix version of ++.
ThreeD ThreeD::operator++()
{
x++; // increment x, y, and z
y++;
z++;
return *this; // return altered value
}
// Overload the postfix version of ++.
ThreeD ThreeD::operator++(int notused)
{
ThreeD temp = *this; // save original value
x++; // increment x, y, and z
y++;
z++;
return temp; // return original value
}
// Show X, Y, Z coordinates.
void ThreeD::show( )
{
cout << x << \", \";
cout << y << \", \";
cout << z << \"\\n\";
}
int main()
{
ThreeD a(1, 2, 3);
ThreeD b;
cout << \"Original value of a: \";
a.show();
cout << \"\\n\";
++a; // prefix increment
cout << \"Value after ++a: \";
a.show();
a++; // postfix increment
cout << \"Value after a++: \";
a.show();
cout << \"\\n\";
b = ++a; // b receives a\'s value after increment
cout << \"Value of a after b = ++a: \";
a.show();
cout << \"Value of b after b = ++a: \";
b.show();
cout << \"\\n\";
b = a++; // a receives c\'s value prior to increment
cout << \"Value of a after b = a++: \";
a.show();
cout << \"Value of b after b = a++: \";
b.show();
return 0;
}
knock knock knock
Get the hell off!
// this is good // this works // this code is terrible