public: _Self& operator,(const _Tp& __obj) // comma operator overloading to create a defined function { // with an arbitrary number of parameters of the same type(ease of entry) _Base::push_back(__obj); return *this; } };
void F(const Vector<int>& __obj) { //some code }
//overloaded extraction operator >> and insertion operator << #include <iostream> using namespace std; class Distance { private: int feet; // 0 to infinite int inches; // 0 to 12 public: // required constructors Distance(){ feet = 0; inches = 0; } Distance(int f, int i){ feet = f; inches = i; } friend ostream &operator<<( ostream &output, const Distance &D ) { output << "F : " << D.feet << " I : " << D.inches; return output; }
int main() { //overloaded comma Vector<int> a; F((a,1,2,3,4,5,6,7,8,9,10));
//overloaded extraction operator >> and insertion operator << Distance D1(11, 10), D2(5, 11), D3; //Here it is important to make operator overloading function a friend of the class because it would be called without //creating an object. cout << "Enter the value of object : " << endl; cin >> D3; cout << "First Distance : " << D1 << endl; cout << "Second Distance :" << D2 << endl; cout << "Third Distance :" << D3 << endl; }
Comments