Programming in C++
Lab 4: Pointers, references, and how to use them
Task
Important! Read the conditions for completing the assignments on the module's website! Tasks are submitted via the form on the modules's website. Solutions will not be accepted via email. If you have any questions, please contact the subject list or the practical supervisor.
There are 14 days to resolve.
Deadline: 02.04.2022 23:59:59
General requirements for the trainee
Create a library libneljas.a
the contents of which can be used through header neljas.h
. Add all required solutions to this library. It is recommended to use different files for different assignments. Document the result and add a working Makefile. Again, please use given function and member names.
Task 1 - Swapping two variable values with pointers and references (1 point)
Write functions:
template<class T> void swap_ref (T &x, T &y); template<class T> void swap_ptr (T *x, T *y);
The purpose of these functions is to exchange the values given as input.
For example:
int x = 5, y = 7; swap_ref<int> (x, y); // nüüd x == 7 ja y == 5 swap_ptr<int> (&x, &y); // nüüd x == 5 ja y == 7
Problem 2 - Function for solving an equation (2 points)
Write a function:
bool solve (double a, double b, double c, double &x1, double &x2);
Parameters a
, b
and c
determine the equation ax2+bx+c=0. The function must return false if the given equation is not solvable, otherwise true. Solution must be written into parameters x1 and x2. When the equation is not solvable then x1 and x2 remain unchanged
NB! The function should also support equations where the quadratic multiplier is 0.
A little bit of sample code:
double a = 1.0, b = -2.0, c = 1.0; double solution1, solution2; // lahendused võiksid olla 1 ja 1 if (solve (a, b, c, solution1, solution2)) cout << "Solutions are: " << solution1 << ", " << solution2 << endl; else cout << "There are no solutions in real numbers!" << endl;
Task 3 – Line with pointed endpoints (2 points)
Write template class template<class T> DynamicLine
, which represents a line with given point
types, where points themselves are defined with pointers. Class description:
Class member | Description |
---|---|
T *p1; | Pointer to first point(public) |
T *p2; | Pointer to second point(public) |
DynamicLine (T *v1, T *v2) | Parameterized constructor |
DynamicLine (const DynamicLine& l) | Copy constructor |
DynamicLine& operator= (const DynamicLine& l) | Assginement operator |
~DynamicLine () | Destructor |
The idea behind the class is as follows. Let's imagine that we are writing a drawing program where points can be dragged with mouse. We wish that lines that connect those points were updated automatically. For that we will write a line class that does not include its own copies of endpoints but is pointing to existing points.
Define copy constructor and assignment operator which leave enpoint pointers unchanged (don't create new points with same coordinates). Constructor does not allocate memory for endpoints and destructor may not release them, because other lines might still use them.
The work of the class can be illustrated with:
Vector2 p1 (1.0, 2.0); Vector2 p2 (0,0); Vector2 p3 (5.0, 2.0); DynamicLine<Vector2> l1 (&p1, &p2); // line between p1 and p2 DynamicLine<Vector2> l2 = l1; // copy line l1 (same endpoints) l2.p2 = &p3; // define l2 other endpoint p1.x = 2.0; // both l1 and l2 endpoints have moved
Following material might be helpful:
http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor/
NB! Default constructor should not exist here because creating a line with null pointers is not reasonable.
Task 4 – Simple dynamic array (5 points)
Write template class <class T> MyArray
, that represents an array with dynamic size. Elements
can be added to the array. Adding new elements may be implemented unefficiently where upon
adding a new element new chunk of memory is allocated, data is copied and old copy is deleted.
In this class copy constructor and assignment operator must copy the data (so that the copy won't
be pointing to the same data). Also destructor must delete the data. Note that this class is
responsible for its pointers, whereas the straight line in the previous problem only saved the vertices given to it.
Class description:
Class member | Description |
---|---|
T *content; | Array content (data reference, publicly visible) |
unsigned int size; | Number of elements in the array (not publicly visible) |
MyArray (); | Default constructor, values pointer to null and a number of 0 |
MyArray (const MyArray& a) | Copy constructor |
MyArray& operator= (const MyArray& a) | Assignment operator |
~MyArray () | Destructor |
unsigned int getSize (); | Returns the number of elements in the array |
void addElement (T element); | Adds the end of an array to this element |
T& elementAt (unsigned int i); | Returns element at index iNB! Throws exception if no element with given index exists |
Sample program:
MyArray<int> numbers; // a sequence of numbers numbers.addElement (5); // we add one element numbers.addElement (11); // and one more MyArray<int> copy = numbers; // copy the sequence copy.addElement (13); // we add an item to the copy if (numbers.getSize () != 2 || copy.getSize () != 3) // check sizes cout << "Copy failed – wrong sizes!" << endl; if (numbers.elementAt(0) != copy.elementAt(0) || numbers.elementAt(1) != copy.elementAt(1)) // check content cout << "Copy failed – wrong elements!" << endl;
The following materials may be helpful:
http://www.cplusplus.com/doc/tutorial/dynamic.html
http://www.learncpp.com/cpp-tutorial/915-shallow-vs-deep-copying/
Tips – copying elements
To solve the fourth assignment it is useful to know the following:
How to copy several elements at once: http://www.cplusplus.com/reference/algorithm/copy/
Discussion why memcopy might not be a good idea: http://www.cplusplus.com/forum/beginner/17128/
You may find „full solutions” on the internet. Do not submit those without checking as they might have bugs.
Task 5 – When points are lost (1 lisapunkt)
We don't expect a program from the solution of the additional task this time (however, of course you can try to realize your ideas). This time write additional assignment into text file lisayl.txt. Put this into root folder of your solution (next to Makefile). Answer to these questions in the context of third assignment:
- What happens when a point that is pointed by lines is destroyed?
- How could point destructor know in which lines it is included?
- What could point destructor do to fix the situation?
Solution will be graded by how convincing it is and how well could the solution work. Creative approach will be welcomed.