






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Electrical Engr Computations; Subject: Electrical And Computer Engr; University: University of Tennessee - Knoxville; Term: Fall 2007;
Typology: Study notes
1 / 11
This page cannot be seen from the preview
Don't miss anything!







2
3
C++ storage classes - revisit
Each C++ variable has several attributes: name, type, size, value storage class, scope, linkage
C++ has four storage classes auto : the default. Variables are automatically created and initialized when they are defined and are destroyed at the end of the block containing their definition. They are not visible outside that block. register : a type of auto variable. a suggestion to the compiler to use a CPU register for performance. static : a variable that is known only in the function that contains its definition but is never destroyed and retains its value between calls to that function. It exists from the time the program begins execution. extern : a static variable whose definition and placement is determined when all object and library modules are combined (linked) to form the executable code file. It can be visible outside the file where it is defined.
4
Example - register
#include
register int i, j; int starttime, endtime;
starttime = clock() for (i=0; i<LARGENUM; i++) ; endtime = clock();
cout << “the duration is” << endtime – starttime;
7
Another
example
// class definition class Complex { public: Complex(); Complex(float, float); ~Complex(); void setComplex(float, float); void printComplex(); float getReal() const; float getImag() const; void setReal(float); void setImag(float);
static int getCount(); private: float real; float imag;
static int count; };
int Complex::getCount() { return count; } // method implementation Complex::Complex() { real = 0; imag = 0; count++; } Complex::Complex(float a, float b) { real = a; imag = b; count++; } Complex::~Complex() { count--; }
8
Inline functions
Short frequently used functions are good candidates for inline functions.
An inline function’s body is inserted directly in the compiled code at the point where the function would normally be called.
The advantage is efficiency.
Like the register keyword, “inline” is only a suggestion to the compiler.
9
Example
// using inline function to calculate the volume of a cube
#include
inline float cube(float s) { return s * s * s; }
int main() { float side;
cin >> side; cout << “The volume of cube with side “ << side << “ is “ << cube(side);
return 0; }
10
Default arguments
Default arguments have a defined value if they are not present in the calling parameter list.
Default argument must be the rightmost parameters (so they can be omitted).
Default arguments must be specified only once (first appearance of function name, usually in function prototype).
13
Example
// overloading abs() #include
// abs() is overloaded in two ways int abs(int i); double abs(double d); int main() { cout << abs(-10) << “\n”; cout << abs(-11.0) << “\n”; return 0; }
int abs(int i) { if (i<0) return -i; else return i; } double abs(double d) { if (d<0.0) return -d; else return d; }
14
Function templates allow the definition of
functions independent of the type of the arguments and return value.
This implements a philosophy of “write-
once / use-anywhere” by allowing parameterized types.
Templates form the basis for the C++
Standard Template Library (STL) of algorithms, which we will discuss later.
15
template
int maximum ( int value1, int value2, int value3 ) { int max = value1;
if ( value2 > max ) max = value2;
if ( value3 > max ) max = value3;
return max; }
int main() { int a=1,b=3,c=3; int m;
m = maximu
return 0; }
16
Recursive functions calls itself (directly or through another function).
Any C++ function call call itself, but functions that use static and extern variables must do so carefully.
Recursion can be used to solve problems by induction:
19
Recursive process
Factorial(3)
3Factorial(2)*
2Factorial(1)*
1Factorial(0)*
2*
1*
3*
6
20
If an algorithm can be expressed in a recursive form with a simple algorithm for each step, the recursive implementation can be very easy to verify.
Recursion incurs overhead in creation and destruction of automatic variables and in function calls.
Recursion may hide gross inefficiencies (e.g., the Fibonacci example); watch for explosive numbers of recursive calls!
21
long fib (long n) { if ( n == 0 || n == 1 ) { return n; //base case else if ( n > 0 ) { return fib(n-1) + fib(n-2); //recursive case } else return -1; //error case } Each call to fib (except fib(0) and fib(1)) generates 2 more calls to fib, which generates an exponential explosion in the total number of calls to fib as n increases.
Let O(fib,n) be the number of calls required to calculate fib(n) Then O(fib,n) = 1 + O(fib,n-1) + O(fib,n-2).