130 likes | 255 Vues
Review of Function Overloading. Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int x) { return x*x; } double sqr(double x) { return x*x; } double sqr(double x, double y) { return x*y; } int i = 2; double x = 1.2, y = 2.0;
E N D
Review of Function Overloading • Allows different functions to have the same name if they have different types or numbers of arguments, e.g. • int sqr(int x) { return x*x; } • double sqr(double x) { return x*x; } • double sqr(double x, double y) { return x*y; } • int i = 2; double x = 1.2, y = 2.0; • cout << sqr(i) << “ ” << sqr(x) << “ “ << sqr(x,y); • 4 1.44 2.4
Operator Overloading This is a powerful C++ feature that allows the use of operators for user defined classes, e.g. dVector A, B, C, D; // a user defined class C = A + B; // = and + is overloaded D = A*B; // * is overloaded (dot product) cout << D; // << is overloaded Similar to function overloading except it uses operators
Member Operator Functions General form: return-type class-name::operator#(argument-list) { // operation to be performed } e.g. void dVector::operator=(dVector &v2) { // v1 = v2 int i; if ( n1 != v2.n1 || n2 != v2.n2 ) return; // error ! for ( i = n1; i <= n2; i++ ) e(i) = v2.e(i); // copy elements }
Improved = Operator dVector dVector::operator=(dVector &v2) { // v1 = v2 int i; dVector *ans; // declare a return variable ans = new dVector(n1,n2); // allocate a dVector if ( n1 != v2.n1 || n2 != v2.n2 ) return *ans; // error ! for ( i = n1; i <= n2; i++ ) ans->e(i) = e(i) = v2.e(i); // copy elements return *ans; }
Overloading the + Operator dVector dVector::operator+(dVector &v2) { // v1 + v2 int i; dVector *ans; // declare a return variable ans = new dVector(n1,n2); // dynamically allocate if ( n1 != v2.n1 || n2 != v2.n2 ) return *ans; // error ! for ( i = n1; i <= n2; i++ ) ans->e(i) = e(i) + v2.e(i); // copy elements return *ans; }
Overloading Unary Operators Example: Norm operator double dVector::operator!() { int i; double ans=0.0; // declare a return variable for ( i = n1; i <= n2; i++ ) ans += e(i)*e(i); return sqrt(ans); }
Friend Functions A Friend function is not a member function of a class, but it has access to all its private members, e.g. class number { double x; // private member public: number() { x = 7.11; } // constructor friend void show_x(number n); }; void show_x(number n) { cout << n.x; } number n1; show_x(n1); 7.11;
<< Operator Overloading class dVector { friend ostream &operator<<(ostream &stream, dVector &v); … }; ostream &operator<<(ostream &stream, dVector &v) { // example usage: cout << v1 << v2 << v3; int i; for ( i = v.n1; i <= v.n2; i++ ) stream << "\n" << v.e(i); return stream; }
“this” Pointers A this pointer is a special pointer that is automatically passed to a member function when it is called It is a pointer to the object that calls the member function It is useful for indirect (backdoor) access of member variables and functions, e.g. dVector::dVector(int n1, int n2) { this->n1 = n1; this->n2 = n2; It is also useful for operator overloading and returning objects from member functions, e.g. return *this;
Static Variables Local static variables are not destroyed when the function returns Similar to global variables but has the advantage of not being accessible to other functions This is useful for initializing parameters in a function or giving a function memory
Static Variables Global static variables are not accessible to functions in other files Useful for restricting the access of global variables to only one file Static member variables result in only one copy of the variable that is shared by all the objects of that type Useful for communication between objects or counting the number of objects currently being used
Extern Statement The extern statement is needed to access global variables in other files, e.g. File #1: double global1 = 7.7; // original definition has no extern File #2: extern double global1; // use extern in other files cout << global1; 7.7; Try to avoid the use of global variables when using C++
Const Variables const variables cannot be changed in the program, e.g. const double pi=3.14; pi = 1.0; // not allowed void f1(const double x) { x = 1.0; } // not allowed void f2(const double *x) { *x = 2.0; } // not allowed void f3(const double &x) { x = 2.0; } // not allowed Not that useful for protecting objects with dynamic memory passed by reference such as dVector