C++/Overload/Parenthesis
Содержание
Demonstrate the function call operator.
#include <iostream>
using namespace std;
class three_d {
int x, y, z;
public:
three_d() { x = y = z = 0; }
three_d(int i, int j, int k) { x = i; y = j; z = k; }
three_d operator()(three_d obj);
three_d operator()(int a, int b, int c);
friend ostream &operator<<(ostream &strm, three_d op);
};
three_d three_d::operator()(three_d obj)
{
three_d temp;
temp.x = (x + obj.x) / 2;
temp.y = (y + obj.y) / 2;
temp.z = (z + obj.z) / 2;
return temp;
}
three_d three_d::operator()(int a, int b, int c)
{
three_d temp;
temp.x = x + a;
temp.y = y + b;
temp.z = z + c;
return temp;
}
ostream &operator<<(ostream &strm, three_d op) {
strm << op.x << ", " << op.y << ", " << op.z << endl;
return strm;
}
int main()
{
three_d objA(1, 2, 3), objB(10, 10, 10), objC;
cout << "This is objA: " << objA;
cout << "This is objB: " << objB;
objC = objA(objB);
cout << "objA(objB): " << objC;
objC = objA(10, 20, 30);
cout << "objA(10, 20, 30): " << objC;
objC = objA(objB(100, 200, 300));
cout << "objA(objB(100, 200, 300)): " << objC;
return 0;
}
Demo: Overload ().
#include <iostream>
using namespace std;
class MyClass {
int x, y, z;
public:
MyClass() {
x = y = z = 0;
}
MyClass(int i, int j, int k) {
x = i;
y = j;
z = k;
}
MyClass operator()(int a, int b, int c);
void show() ;
};
// Overload ().
MyClass MyClass::operator()(int a, int b, int c)
{
MyClass temp;
temp.x = x + a;
temp.y = y + b;
temp.z = z + c;
return temp;
}
void MyClass::show()
{
cout << x << ", ";
cout << y << ", ";
cout << z << endl;
}
int main()
{
MyClass object1(1, 2, 3), object2;
object2 = object1(10, 11, 12); // invoke operator()
cout << "object1: ";
object1.show();
cout << "object2: ";
object2.show();
return 0;
}
Overload [] to create a generic safe array type.
#include <iostream>
#include <cstdlib>
using namespace std;
template <class T, int len> class safe_array {
T ar[len];
int length;
public:
safe_array();
T &operator[](int i);
int getlen() { return length; }
};
template <class T, int len> safe_array<T, len>::safe_array() {
for(int i=0; i < len; ++i) ar[i] = T();
length = len;
}
template <class T, int len> T &safe_array<T, len>::operator[](int i)
{
if(i < 0 || i > len-1) {
cout << "\nIndex value of " << i << " is out-of-bounds.\n";
exit(1);
}
return ar[i];
}
class myclass {
public:
int x;
myclass(int i) { x = i; };
myclass() { x = -1; }
};
int main()
{
safe_array<int, 10> intArray;
safe_array<double, 5> doubleArray;
int i;
for(i=0; i < intArray.getlen(); ++i)
cout << intArray[i] << endl;
for(i=0; i < intArray.getlen(); ++i)
intArray[i] = i;
for(i=0; i < intArray.getlen(); ++i)
cout << intArray[i] << endl;
for(i=0; i < doubleArray.getlen(); ++i)
cout << doubleArray[i] << endl;
for(i=0; i < doubleArray.getlen(); ++i)
doubleArray[i] = (double) i/3;
for(i=0; i < doubleArray.getlen(); ++i)
cout << doubleArray[i] << endl;
safe_array<myclass, 3> classArray; // myclass array of size 3
cout << "Initial values in classArray: ";
for(i = 0; i < classArray.getlen(); ++i) cout << classArray[i].x << " ";
cout << endl;
classArray[0].x = 9;
classArray[1].x = 9;
classArray[2].x = -9;
cout << "New values for classArray: ";
for(i = 0; i < classArray.getlen(); ++i) cout << classArray[i].x << " ";
cout << endl;
intArray[12] = 100;
return 0;
}
Using the Subscript Operator in Programming a Dynamic Array
#include <iostream>
using namespace std;
class CMyArray
{
private:
int* m_pnInternalArray;
int m_nNumElements;
public:
CMyArray (int nNumElements);
~CMyArray ();
// a subscript operator
int& operator [] (int nIndex);
};
int& CMyArray::operator [] (int nIndex)
{
return m_pnInternalArray [nIndex];
}
CMyArray::CMyArray (int nNumElements)
{
m_pnInternalArray = new int [nNumElements];
m_nNumElements = nNumElements;
}
CMyArray::~CMyArray ()
{
delete [] m_pnInternalArray;
}
int main ()
{
CMyArray mArray (5);
mArray [0] = 25;
mArray [1] = 20;
mArray [2] = 15;
mArray [3] = 10;
mArray [4] = 5;
for (int nIndex = 0; nIndex < 5; ++ nIndex)
std::cout << mArray [nIndex] << " ";
return 0;
}