Computer Science Notes

Notes From CS Undergrad Courses FSU

This project is maintained by awa03

string x= findMax(a);
string & y = x;
cout << y << endl;
// Refrence to an R Value
string && str2 = "Hello";

Parameter Passing

Call by Value

Pass by Reference

Pass by Reference

Call by rvalue refrence

vector <string> v("hello", "world");
cout << randomItem(v) << endl;  // L Value
cout << randomItem({"Hello", "World"}) << endl; // R Value

Given File Example

#include <iostream>
#include <vector>
#include <string.h>

using namespace std;


double ave( const vector<int> & arr, int n, bool & errorFlag)
{

    int sum = 0;
	for( int i = 0; i < arr.size( ); ++i )
	  sum += arr[i];

	n = 100;
	errorFlag = true;
	return ((double)sum)/arr.size();
}


int main( )
{
  int nn = 5;
  bool err = false;
  
  vector<int> myArray {1, 2, 3, 4, 5};
  double average = 0.0;
  
  cout << "Before: average = " << average << ", nn = " << nn << ", err = " << err << endl;  

  average = ave(myArray, nn, err);

  cout << " After: average = " << average << ", nn = " << nn << ", err = " << err << endl;  

  return 0;
}

Return Passing

Return by Value

Return by Reference

Return by Const Reference

Lifeline extended beyond function call for by const reference and reference.


Big Five in C++

A constructor is called whenever..


Problem with Defaults

Variable Scope


Templates

// Return the maximum item in the array a
template<typename Comparable>
const Comparable& findMax(const vector<Comparable>& a){
	int maxIndex = 0;
	for(int i= 1; i < a.size(); i++){
		if(a[maxIndex] < a[i]){
			maxIndex = i;
		}
	}
	return a[maxIndex];
}

For Example

Function Objects

Memory Cell

#code #functions #pass-by-ref #assignment #l-value #r-value #defaults #memberFunctions #memberVariables #declaration #cpp