operator+
๋ ๋ง์
์ฐ์ฐ์๋ฅผ +๋ฅผ ์๋ฏธํจclass Point
{
private:
int x, y;
public:
Point(int x_, int y_)
{
x = x_;
y = y_;
}
void print()
{
cout << "x : " << x << ", y : " << y << "\n";
}
Point operator+(Point &p)
{
x = x + p.x;
y = y + p.y;
return Point(x, y);
}
};
int main(void)
{
Point p1 = {1, 1};
Point p2(2, 2);
Point p3 = p1 + p2;
p3.print();
return 0;
}
Overloaded operators should mimic the functionality of their built-in counterpartsโfor example, the + operator should be overloaded to perform addition, not subtraction. Avoid excessive or inconsistent use of operator overloading, as this can make a program cryptic and difficult to read.
=
)
&
)
object2 = object2.add(object1);
object2 = object2 + object1;
๋ด์ฅ ํ์
์ ๊ฐ์ฒด์ ์ ์ฉ๋๋ ์ฐ์ฐ ๋ฐฉ๋ฒ
(i.e., ์ ์ ๋ง์
์ ์ฌ์ฉ๋๋ +
์ฐ์ฐ์์ ์๋ฏธ๋ฅผ ๋ฐ๊ฟ ์ ์๋ค.)
=
์ +
์ฐ์ฐ์๊ฐ ์ค๋ฒ๋ก๋ฉ ๋์๋ค๊ณ ํด์ +=
์ฐ์ฐ์๊ฐ ์ค๋ฒ๋ก๋ฉ ๋ ๊ฒ์ ์๋๋ค.?:
๋ ์ค๋ฒ๋ก๋ฉ ๋ ์ ์๋ค.()
, []
, ->
๋๋ ๋์
์ฐ์ฐ์(=
)๋ ํด๋์ค ๋ฉค๋ฒ ํจ์๋ก ์ค๋ฒ๋ก๋ ๋์ด์ผ ํ๋ค.cout<<classObject
์์์ ๊ฐ์ฒด cout
HugeIntClass + long int
long int + HugeIntClass
Class PhoneNumber
#pragma once
#include <iostream>
using namespace std;
class PhoneNumber
{
friend ostream &operator<<(ostream &, const PhoneNumber &);
friend istream &operator>>(istream &, PhoneNumber &);
private:
string areaCode;
string exchange;
string line;
};
// #include <iostream>
#include <iomanip>
#include "PhoneNumber.h"
using namespace std;
ostream &operator<<(ostream &output, const PhoneNumber &number)
{
output << "(" << number.areaCode << ") "
<< number.exchange << "-" << number.line;
return output;
}
istream &operator>>(istream &input, PhoneNumber &number)
{
input.ignore();
input >> setw(3) >> number.areaCode;
input.ignore(2);
input >> setw(3) >> number.exchange;
input.ignore();
input >> setw(4) >> number.line;
return input;
}
istream &operator>>(istream &input, PhoneNumber &number)
{
input.ignore();
input >> setw(3) >> number.areaCode;
input.ignore(2);
input >> setw(3) >> number.exchange;
input.ignore();
input >> setw(4) >> number.line;
return input;
}
#include <iostream>
using namespace std;
#include "PhoneNumber.h"
int main()
{
PhoneNumber phone;
cout << "Enter phone number in the forms (123) 456-7890:" << endl;
cin >> phone;
cout << "The phone number entered was: ";
cout << phone << endl;
return 0;
}
class String
{
public:
const String & operator+=( const String & );
โฆ
};
const String &operator+=( String &, const String & );
==
๋ก ๋ ๊ฐ์ ๋ฐฐ์ด์ ๋น๊ตํ ์ ์๋ค.๊ฐ์ฒด์ ๋ณต์ฌ๊ฐ ํ์ํ ๋๋ง๋ค ์ฌ์ฉ:
Array newArray(oldArray) ๋๋ Array newArray = oldArray;
๋ณต์ฌ ์์ฑ์ ์ ์ธ
Array(const Array&);
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
using std::istream;
using std::ostream;
class Array
{
friend ostream &operator<<(ostream &, const Array &);
friend istream &operator>>(istream &, Array &);
public:
Array(int = 10);
Array(const Array &);
~Array();
int getSize() const;
const Array &operator=(const Array &);
bool operator==(const Array &) const;
bool operator!=(const Array &right) const
{
return !(*this == right);
}
int &operator[](int);
int operator[](int) const;
private:
int size;
int *ptr;
};
#endif
#include <iostream>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
#include <cstdlib>;
using std::exit;
#include "Array.h"
Array::Array(int arraySize)
{
size = (arraySize > 0 ? arraySize : 10);
ptr = new int[size];
for (int i = 0; i < size; i++)
{
ptr[i] = 0;
}
}
Array::Array(const Array &arrayToCopy)
: size(arrayToCopy.size)
{
ptr = new int[size];
for (int i = 0; i < size; i++)
{
ptr[i] = arrayToCopy.ptr[i];
}
}
Array::~Array()
{
delete[] ptr;
}
int Array::getSize() const
{
return size;
}
const Array &Array::operator=(const Array &right)
{
if (&right != this)
{
if (size != right.size)
{
delete[] ptr;
size = right.size;
ptr = new int[size];
}
for (int i = 0; i < size; i++)
{
ptr[i] = right.ptr[i];
}
}
return *this;
}
bool Array::operator==(const Array &right) const
{
if (size != right.size)
{
return false;
}
for (int i = 0; i < size; i++)
{
if (ptr[i] != right.ptr[i])
{
return false;
}
}
return true;
}
int &Array::operator[](int subscript)
{
if (subscript < 0 || subscript >= size)
{
cerr << "\nError: Subscript " << subscript
<< "out of range" << endl;
exit(1);
}
return ptr[subscript];
}
int Array::operator[](int subscript) const
{
if (subscript < 0 || subscript >= size)
{
cerr << "\nError: Subscript " << subscript
<< " out of range" << endl;
exit(1);
}
return ptr[subscript];
}
istream &operator>>(istream &input, Array &a)
{
for (int i = 0; i < a.size; i++)
{
input >> a.ptr[i];
}
return input;
}
ostream &operator<<(ostream &output, const Array &a)
{
int i;
for (i = 0; i < a.size; i++)
{
output << setw(12) << a.ptr[i];
if ((i + 1) % 4 == 0)
{
output << endl;
}
}
if (i % 4 != 0)
{
output << endl;
}
return output;
}
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include "Array.h"
int main()
{
Array integers1(7);
Array integers2;
cout << "Size of Array integers1 is "
<< integers1.getSize()
<< "\nArray after initialization:\n"
<< integers1;
cout << "Size of Array integers2 is "
<< integers2.getSize()
<< "\nArray after initialization:\n"
<< integers2;
cout << "\nEnter 17 integers:" << endl;
cin >> integers1 >> integers2;
cout << "\nAfter input, the Arrays contatin:\n"
<< "integers1:\n"
<< integers1
<< "integers2:\n"
<< integers2;
cout << "\nEvaluating: integers1 != integers2" << endl;
if (integers1 != integers2)
{
cout << "integers1 and integers2 are not equal" << endl;
}
Array integers3(integers1);
cout << "\nSize of Array integers3 is "
<< integers3.getSize()
<< "\nArray after initialization:\n"
<< integers3;
cout << "\nAssigning integers2 to integers1:" << endl;
integers1 = integers2;
cout << "integers1:\n"
<< integers1
<< "integers2:\n"
<< integers2;
cout << "\nEvaluating: integers1 == integers2" << endl;
return 0;
}
Date &operator++();
++d1
์ d1.operator++()
๊ณผ ๋์ผDate &operator++(Date& );
++d1
์ d1.operator++(d1)
๊ณผ ๋์ผDate operator++();
d1++
์ d1.operator++(0) // C++์ ์ฝ์
Date operator++(Date&, int);
d1++
์ d1.operator++(d1, 0)
๊ณผ ๋์ผ๋ฐํ๊ฐ
Date &
)๊ฐ์ ์ฐ์ฐ์(โ) ์ ๊ฒฝ์ฐ๋ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์ ์ฉ
#pragma once
#include <iostream>
using std::ostream;
class Date
{
friend ostream &operator<<(ostream &, const Date &);
public:
Date(int m = 1, int d = 1, int y = 1900);
void setDate(int, int, int);
Date &operator++();
Date operator++(int);
const Date &operator+=(int);
bool leapYear(int) const;
bool endOfMonth(int) const;
private:
int month;
int day;
int year;
static const int days[];
void helpIncrement();
};
#include <iostream>
#include "Date.h"
const int Date::days[] =
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Date::Date(int m, int d, int y)
{
setDate(m, d, y);
}
void Date::setDate(int mm, int dd, int yy)
{
month = (mm >= 1 && mm <= 12) ? mm : 1;
year = (yy >= 1900 && yy <= 2100) ? yy : 1900;
if (month == 2 && leapYear(year))
day = (dd >= 1 && dd <= 29) ? dd : 1;
else
day = (dd >= 1 && dd <= days[month]) ? dd : 1;
}
Date &Date::operator++()
{
helpIncrement();
return *this;
}
Date Date::operator++(int)
{
Date temp = *this;
helpIncrement();
return temp;
}
const Date &Date::operator+=(int additionalDays)
{
for (int i = 0; i < additionalDays; i++)
{
helpIncrement();
}
return *this;
}
bool Date::leapYear(int testYear) const
{
if (testYear % 400 == 0 ||
(testYear % 100 != 0 && testYear % 4 == 0))
return true;
else
return false;
}
bool Date::endOfMonth(int testDay) const
{
if (month == 2 && leapYear(year))
return testDay == 29;
else
return testDay == days[month];
}
void Date::helpIncrement()
{
if (!endOfMonth(day))
day++;
else
{
if (month < 12)
{
month++;
day = 1;
}
else
{
year++;
month = 1;
day = 1;
}
}
}
ostream &operator<<(ostream &output, const Date &d)
{
static char *monthName[13] = {"", "January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
output << monthName[d.month] << ' ' << d.day << ", " << d.year;
return output;
}
#include <iostream>
using std::cout;
using std::endl;
#include "Date.h"
int main()
{
Date d1;
Date d2(12, 27, 1992);
Date d3(0, 99, 8045);
cout << "d1 is " << d1 << "\nd2 is " << d2 << "\nd3 is " << d3;
cout << "\n\nd2 += 7 is " << (d2 += 7);
d3.setDate(2, 28, 1992);
cout << "\n\n d3 is " << d3;
cout << "\n++d3 is " << ++d3 << " (leap year allows 29th)";
Date d4(7, 13, 2002);
cout << "\n\nTesting the prefix increment operator:\n"
<< " d4 is " << d4 << endl;
cout << "++d4 is " << ++d4 << endl;
cout << " d4 is " << d4;
cout << "\n\nTesting the postfix increment operator:\n"
<< " d4 is " << d4 << endl;
cout << "d4++ is " << d4++ << endl;
cout << " d4 is " << d4 << endl;
return 0;
}