2.1 String functions
2.2 Another example/exercise with C++ references
On the accompanying slide we have shown a number of examples that illustrate different uses of reference types in C++.
Your task in this exercise is to come up with yet another good and convincing example of reference types in C++. (Please use your own fantasy - not some internet fantasy).
Your program can be seen as the solution to a programming exercise about references. Please provide a short formulation of this exercise.
2.3 Pointers in combination with references
In this exercise we will explore pointers and references relative to each other. Play and learn! We do this in context of a function that returns the maximum of two doubles. Here is the function with call-by-value parameters:
double max(double a, double b){ return a < b ? b : a; }
In all cases below, program the max function, call it, make make sure you get the expected result from the call.
Can you imagine other interesting variations?
Solution
Here are my variations of the max function:
#include <iostream> #include <string> // By value double max0(double a, double b){ return a < b ? b : a; } // By const reference const double& max1(const double& a, const double& b){ return a < b ? b : a; } // Pointers to references - does not compile. const double& max2(double& *a, double & *b){ return *a < *b ? *b : *a; } // References to pointers double max3(double* &a, double* &b){ return *a < *b ? *b : *a; } // Const references to pointers const double& max4(double* const &a, double* const &b){ return *a < *b ? *b : *a; } // const reference to const double pointer (it is the doule which is constant). const double& max5(const double* const &a,const double* const &b){ return *a < *b ? *b : *a; } int main(){ using namespace std; double a = 1, b = 2; double *c = &a, *d = &b; const double e = 3, f = 4; cout << max1(a,b) << endl; cout << max3(c,d) << endl; cout << max4(&a,&b) << endl; cout << max4(c,d) << endl; //cout << max4(&e,&f) << endl; // ERROR cout << max5(&e,&f) << endl; }
2.4 Ways of returning results from the div function
This exercise works in the context of the divide program from the slide about constant references.
Program the struct ldiv_t and the function div yourself, instead of using the definitions from the std namespace.
Run the program with your own versions of ldiv_t and div.
Experiment with returning a reference ldiv_t& from div. (See page 282-283 in The C++ Programming Language).
And similarly, experiment with returning a const reference const ldiv_t& from the div.
Solution
Here are one of my experiments with the div function:
// C++ In A Nutshell example, page 36. Three version of divide. // div returns an object that aggregates the quotient and the remainder. #include <iostream> // #include <cstdlib> struct ldiv_t{ ldiv_t(int a, int b):quot(a), rem(b){}; int quot; int rem; }; const ldiv_t& div(int x, int y){ return ldiv_t(x / y, x % y); // Warning. Problematic. } void divide_1 (long num, long den, long& quo, long& rem){ ldiv_t result = div(num, den); // The struct is COPIED out of div quo = result.quot; rem = result.rem; } void divide_2 (long num, long den, long& quo, long& rem){ const ldiv_t& result = div(num, den); // The struct is NOT COPIED out of div. quo = result.quot; // Instead a const reference is established rem = result.rem; // to the resulting struct. } int main(){ using namespace std; long q, r; divide_1(107, 20, q, r); cout << q << " remainder " << r << endl; // 5 remainder 7 divide_2(107, 20, q, r); cout << q << " remainder " << r << endl; // 5 remainder 7 }
2.5 Understand Vectors
Take a look at the documentation of the (template) class vector at www.cplusplus.com
Familiarize yourself with the vector member functions, and read some of the (numerous) small examples which are available
2.6 Use of class Point in C++
In this exercises we will explore some basic similarities and differences between use of classes in Java/C# and C++. (In the scope of this lecture we will NOT be concerned with class definition details. This belongs to the next lecture). In particular we will study creation/allocation of objects, and parameter passing of objects to a function/method.
The starting point is the simple Point class in C# and a sample C# client program that creates and operates on points. Make sure that you familiarize yourself with the correct output of this program. Predict it before you run it.
We provide a similar Point class in C++. The C++ definitions of the Point functions are also provided (but they are not important for this exercise). You are supposed to explore various uses of class Point in C++.
Feel free to do other experiments based on the Point classes that help you understand the similarities and differences between use of C# and C++ classes, pointers, references, and stack allocation of C++ objects.
Solution
I have made a number of programs that illustrates the various issues in this exercise. They may appear on the slide that refers to this exercise at a later point in time
Generated: Tuesday March 26, 2013, 13:03:17