Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ics212-23-classesII

 ics212-23-classesII

Avatar for William Albritton

William Albritton

November 03, 2015
Tweet

More Decks by William Albritton

Other Decks in Programming

Transcript

  1. Function Overloading  Function overloading is when you have two

    or more functions that have the same name, but the parameter type and/or parameter count is different  This is useful for functions that perform similar operations, but with different data types  This way, we do not need different names for functions that do similar tasks
  2. Function Overloading  See this example program, which multiplies two

    integers or two doubles using two different functions that have the same name, but different parameter types (and return types): overload.cpp int square(int a){return a*a;} double square(double a){return a*a;}
  3. Reference Variables  Used as an alias for other variables

     You can think of it as a second name for the original variable  Sort of like a nickname for a person  See example program: reference.cpp
  4. Declaring Reference Variables  Use an ampersand (&) in front

    of the name of the reference variable to declare a reference variable:  datatype &referenceVariable = regularVariable; //regular variable: int number1 = 100; //reference variable: int &reference1 = number1;
  5. Using Reference Variables  Can use a reference variable same

    as any variable number1++; cout<<number1<<“,”<<reference1; //101, 101 reference1++; cout<<number1<<“,”<<reference1; //102, 102
  6. Call-by-Reference  A reference variable can be used to implement

    call-by-reference functions  Use the ampersand (&) in front of the parameters in the function header  Syntax for function header: returnType functionName(datatype &parameters)
  7. Call-by-Reference vs Call-by-Value  Call-by-value copies the all data from

    the original variable into the parameter  If this is a large data structure, this could take a while!  Using reference variables to implement call-by- reference saves execution time, because the parameter is simply an alias for the original variable  No data has to be copied over!
  8. Call-by-Reference  Example method call:  From example program: reference.cpp

    int number2 = 50; cout<<number2<<endl; //50 square(number2); cout<<number2<<endl; //2500
  9. Constructors  A constructor is a class member function with

    the same name as its class  Used to initialize the class data members  Can have several overloaded constructors to initialize data members in different ways  When instantiating, data to be initialized is put in parenthesis to the right of the object’s name  See example program at: constructor.cpp
  10. Constructors  Syntax for constructor function definition:  ClassName(dataTypes parameterNames){

    //initialize data memebers }  Syntax for instantiating an object:  ClassName variableName(argumentNames);
  11. Example Code  Example constructor function definition:  Example of

    instantiating an object: Fraction(int num, int den){ set(num, den); } Fraction fraction3(3,4);
  12. Copy Constructor  The copy constructor is a special constructor

    that is used for making a copy of an object  Usually, a copy constructor is created automatically by the compiler, and makes copies of the data members from one object to another  However, this is problematic when the data members are pointers that point to a data structure, which we will look at in a later lecture
  13. Copy Constructor  We will create a copy constructor that

    copies the values for the numerator and denominator of one object, and creates a new object with the same values for the numerator and denominator
  14. Copy Constructor is Called in 3 Cases  Case #1:

    When initializing an object with an object of the same class using parenthesis Fraction fraction1; Fraction fraction6(fraction1);
  15. Copy Constructor is Called in 3 Cases  Case #2:

    When initializing an object in a program with the equals sign  Note: this is not the same as the assignment operator, although it looks the same Fraction fraction1; Fraction fraction7 = fraction1;
  16. Copy Constructor is Called in 3 Cases  Case #3:

    When passing-by-value to a function //function definition: void functionX(Fraction fraction8){ //code... } //function call: fraction1.functionX(fraction2);
  17. Copy Constructor Function Definition  You can create your own

    copy constructor Fraction(const Fraction &fraction){ set(fraction.numerator, fraction.denominator); }
  18. Copy Constructor Function Definition  Otherwise the computer will automatically

    create one in which each data member of one object is copied to another object’s data members Fraction(const Fraction &fraction){ numerator=fraction.numerator; denominator=fraction.denominator; }
  19. Destructors  A destructor is a class member function with

    the same name as its class with a tilde (~) character in front of it  Called when an object is “destroyed”  When program execution leaves the scope in which the object of that class was instantiated  For global objects & static objects, when the program ends execution
  20. Destructors  Performs termination housekeeping so memory can be returned

    to the system  Useful for dynamically allocated data members, in which nodes point to other nodes  Linked lists, stacks, queues  Binary search trees
  21. Destructor Definition  Syntax for a destructor definition:  ~NameOfClass(void){

    //code to delete dynamically allocated data }  Example code for class Fraction Destructor ~Fraction() { //code }
  22. Destructors  Since the data members of class Fraction do

    not point to anything, the Fraction destructor doesn’t really “do” anything  However, this program will show you when destructors are called  See example program: destructor.cpp
  23. Efficiency of Passing Parameters  When passing a parameter to

    a method call-by- reference more efficient than call-by-value  Call-by-value creates a “local copy” of the original variable with the “copy constructor”, so all data has to be copied from the original variable to the parameter  Call-by-reference using reference variables simply uses the parameter as an alias for the original variable
  24. Efficiency of Passing Parameters  Pass-by-value does not take a

    long time with simple data types (char, int, double, etc.)  However, pass-by-value can take a long time with be with user-defined data structures that can contain a lot of data (linked list, stack, queue, binary tree, etc.)  When possible, make parameters call-by-reference  Can speed up execution for some data types
  25. Using Constant Parameters  If the data members should not

    be changed, use “const” reference parameters  Can help to prevent programming bugs  Principle of least privilege: a function should not be able to change a parameter if it is not necessary
  26. Call-by-Reference Using const  Here is the syntax for making

    a function definition with call-by-reference using const  returnType functionName(const DataType &parameter){ //code }
  27. Other Member Functions  We can create any function that

    we wish for a class  For example, add(), subtract, multiply(), and divide() function for class Fraction  See example program: math.cpp
  28. Fraction multiply(const Fraction &f){ int num3=numerator*f.numerator; int den3=denominator*f.denominator; Fraction f3(num3,den3);

    f3.reduce(); return f3; } Other Member Functions  Example multiply function for class Fraction: