1 / 13

Review of Function Overloading

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;

hestia
Download Presentation

Review of Function Overloading

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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 }

  4. 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; }

  5. 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; }

  6. 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); }

  7. 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;

  8. << 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; }

  9. “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;

  10. 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

  11. 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

  12. 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++

  13. 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

More Related