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
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;}
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;
call-by-reference functions Use the ampersand (&) in front of the parameters in the function header Syntax for function header: returnType functionName(datatype ¶meters)
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!
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
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
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;
When passing-by-value to a function //function definition: void functionX(Fraction fraction8){ //code... } //function call: fraction1.functionX(fraction2);
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; }
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
to the system Useful for dynamically allocated data members, in which nodes point to other nodes Linked lists, stacks, queues Binary search trees
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
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
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
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