Sei sulla pagina 1di 4

cpoint_friend.cpp #include <iostream.h> class CPoint { int m_x; // X coordinate int m_y; // Y coordinate public: // Consturctors...

CPoint() : m_x(0), m_y(0) {} CPoint(int ax, int ay) : m_x(ax), m_y(ay) {} friend bool Equal(CPoint& p1, CPoint& p2); }; bool Equal(CPoint& p1, CPoint& p2) { return (p1.m_x == p2.m_x && p1.m_y == p2.m_y); } void main() { CPoint p1, p2(3,4), p3(11,12), p4 = p2; cout<<"p1 and p2 are "<<(Equal(p1,p2)?"": "not") <<" equal"<<endl; cout<<"p2 and p4 are "<<(Equal(p2,p4)?"": "not") <<" equal"<<endl; } cline_friend.cpp #include <iostream.h> class CPoint { // as before friend class CLine; }; class CLine { CPoint m_from; CPoint m_to; public: // Consturctors... CLine(const CPoint& f, const CPoint& t) : m_from(f), m_to(t) {} void Shrink(); }; void CLine::Shrink() { m_to.m_x = m_from.m_x; m_to.m_y = m_from.m_y; } void main() { CPoint p1(3,4), p2(11,12); CLine l(p1,p2); l.Shrink(); }

namespace.cpp namespace graph_lib { void gf() { ... } class CPoint { ... }; } // End of namespace ======================== namespace graph_lib { class CRectangle { ... }; } // End of namespace ======================== namespace another_lib { void gf() { ... graph_lib::gf(); graph_lib::CPoint p1; } } // End of namespace ======================== using namespace graph_lib; void main() { gf(); CPoint p; } ======================== using namespace graph_lib; using namespace another_lib; void gf() {...} void main() { CPoint p; // gf(); --> ambiguity graph_lib::gf(); another_lib::gf(); ::gf(); }

carray.h class CArray { float* Items; int Size; public: // C'tors and D'tors CArray(int a_size):Size(a_size) { Items = new float[Size]; } CArray(const CArray& a) { Items = NULL; *this = a; } ~CArray() { delete []Items; } // Operators const CArray& operator=(const CArray& a); float& operator[](int index); CArray operator+(const CArray& a) const; friend CArray operator+(const CArray& a, float val); friend CArray operator+(float val, const CArray& a); const CArray& operator+=(float val); const CArray& operator*=(float val); const CArray& operator++(); const CArray operator++(int); //For postfix int operator==(const CArray& a) const; int operator!=(const CArray& a) const; const CArray operator-() const; const CArray& operator()(float* arr, int size); operator const float*() const {return Items;} friend ostream& operator<<(ostream& out, const CArray& a); friend istream& operator>>(istream& in, CArray& a); };

main.cpp #include <iostream.h> #include <fstream.h> #include <string.h> #include carray.h void func(const float* f) { //... } void main() { // CArray a; // Error - No default c'tor CArray grades_1(3); grades_1[0] = 97.2; grades_1[1] = 85.0; grades_1[2] = 60.2; CArray grades_2 = grades_1; if (grades_1 == grades_2) cout<<"grades_1 and grades_2 are equal"<<endl; CArray grades_all = grades_1 + grades_2; grades_all *= 1.2; grades_all += 1.0; grades_1 = grades_all + 3.2; cout<<grades_all; cout<<grades_all++<<endl; cout<<++grades_all<<endl; float more_grades[] = {85.5, 79.0, 100.0}; CArray grades_3(0); grades_3(more_grades, 3); ofstream OutFile("output.txt"); OutFile<<grades_3; OutFile.close(); }

carray.cpp #include carray.h const CArray& CArray::operator=(const CArray& a) { if (&a != this) { delete []Items; Size = a.Size; Items = new float[Size]; for (int i=0 ; i < Size ; i++) Items[i] = a.Items[i]; } return *this; } inline float& CArray::operator[](int index) { if (index < 0 || index >= Size) throw "Error - Out of range!!"; return Items[index]; } CArray CArray::operator+(const CArray& a) const { CArray TempArray(Size + a.Size); for (int i=0 ; i < Size ; i++) TempArray[i] = Items[i]; for (i = 0 ; i < a.Size ; i++) TempArray[Size + i] = a.Items[i]; return TempArray; } CArray operator+(const CArray& a, float val) { CArray TempArray(a.Size); for (int i=0 ; i < a.Size ; i++) TempArray[i] = a.Items[i] + val; return TempArray; } CArray operator+(float val, const CArray& a) { return a+val; } const CArray& CArray::operator+=(float val) { for (int i=0 ; i < Size ; i++) Items[i] += val; return *this; }

> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >

carray.cpp cont const CArray& CArray::operator*=(float val) { for (int i=0 ; i < Size ; i++) Items[i] *= val; return *this; } const CArray& CArray::operator++() { (*this) += 1.0; return *this; } const CArray CArray::operator++(int) { CArray TempArray = *this; (*this) += 1.0; return TempArray; } int CArray::operator==(const CArray& a) const { if (Size != a.Size) return false; for (int i=0; i < Size ; i++) if (Items[i] != a.Items[i]) return false; return true; } int CArray::operator!=(const CArray& a) const { return !(*this == a); } const CArray CArray::operator-() const { CArray TempArray(Size); for (int i=0; i < Size ; i++) TempArray[i] = - Items[i]; return TempArray; } .

VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV

carray.cpp cont const CArray& CArray::operator()(float* arr, int size) { if (Items != NULL) delete []Items; Size = size; Items = new float[Size]; for (int i=0 ; i<Size ; i++) Items[i] = arr[i]; return *this; } ostream& operator<<(ostream& out, const CArray& a) { for (int i=0; i<a.Size ; i++) out<<"item "<<i<<"="<<a.Items[i]<<endl; return out; } istream& operator>>(istream& in, CArray& a) { cout<<"Please enter "<<a.Size<<" numbers"<<endl; for (int i=0; i<a.Size ; i++) in>>a[i]; return in; }

Potrebbero piacerti anche