C++ Exercises¶
Today we have two problems for you to tackle.
Problem 1: Adding a ColoredShape¶
Navigate to /code/c++/ExerciseDay3/ex1/ to find the code for the C++ example presented in todays video. You are asked to create another shape of your choosing and add to the code base. A similar example was perfomed during the video.
Hint
When starting a new C++ class it is usual for developers to start by copying the .h and .cpp file of an existing working class that is similar to have the name of the new class they want. Then they open the new class and make isssue a global replacement to replace old class name with new name. This way they start with a working class. Then they would go into new class and start making modifications.
$ cp workingClass.h newClass.h $ cp workingClass.cpp newClass.cpp $ emacs newClass.h emacs> <esc>% emacs> (asks for string to replace:>) oldClass <enter> emacs> (asks for replacement string:>) newClass <enter> emacs> ! <-- to replace all quickly emacs> // make other changes as needed emacs> <CTRL> XS emacs> <CTRL> XF emacs> newClass.cpp emacs> <esc>% $ (asks for string to replace:>) oldClass <enter> $ (asks for replacement string:>) newClass <enter> $ ! <-- to replace all quickly $ // make other changes as needed emacs> <CTRL> XF emacs> <CTRL> XC
Note
A CMakeLists.txt file has been provided. Add your file to it when created. The cmake command will check your system for compilers and other development tool and create a Makefile in each source folder. The make will build the executable(s). For subsequent builds, you only have to call cmake again if you change anything in the CMakeFilesList.txt file. Placing the compile files into a build folder makes cleanup easier: simply delete the entire build folder when done. It can be regenerated easily using the procedure below.
The build process again is:
$ mkdir build
$ cd build
$ cmake ..
$ make
Problem 2: An engineering Vector Class requires you to finish off¶
Navigate to /code/c++/ExerciseDay3/ex2/ to find the partially completed code for a C++ Vector class. You are to finish it and test it. The directory contains a header file Vector.h, an implementation file Vector.cpp, a main.cpp to test with and a CMakeFilesList.txt file for building and compiling.
The header file Vector.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #ifndef _VECTOR
#define _VECTOR
class Vector
{
public:
Vector(int size);
~Vector();
// some methods
int Size(void) const;
void zero(void);
double norm(void) const;
double dot(const Vector &other) const;
void print(void);
// overload some operators to look Matlabish
Vector operator+(const Vector &other) const;
void operator=(const Vector &other);
void operator+=(double val);
void operator+=(const Vector &other);
double operator()(int x) const;
double &operator()(int x);
private:
double *data;
int size;
};
#endif
|
The implementation file Vector.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | #include <iostream>
#include <cmath>
#include "Vector.h"
Vector::Vector(int sz)
{
size = 0;
data = 0; // PROVIDE CODE
}
Vector::~Vector()
{
if (data != 0)
; // PROVIDE CODE
}
// some methods
int
Vector::Size(void) const
{
return size;
}
void
Vector::zero(void)
{
for (int i=0; i<size; i++)
data[i] = 0;
}
double
Vector::norm(void) const
{
return 0; // PROVIDE CODE
}
double
Vector::dot(const Vector &other) const
{
double result = 0;
if (other.size != size) {
std::cerr << "Vector::dot ERROR vectors not of same size, returning 0\n";
return result;
}
for (int i=0; i<size; i++)
; // PROVIDE CODE
return result;
}
void
Vector::print(void)
{
for (int i=0; i<size; i++)
std::cerr << " " << data[i];
std::cerr << "\n";
}
// overload some operators to look Matlabish
Vector
Vector::operator+(const Vector &other) const
{
Vector result(size);
if (other.size != size) {
std::cerr << "Vector::operator ERROR vectors not of same size, returning empty vector\n";
return result;
}
for (int i=0; i<size; i++)
result.data[i] = data[i] + other.data[i];
return result;
}
void
Vector::operator=(const Vector &other)
{
for (int i=0; i<size; i++)
data[i] = other.data[i];
}
void
Vector::operator+=(double val)
{
for (int i=0; i<size; i++)
data[i] += val;
}
void
Vector::operator+=(const Vector &other)
{
for (int i=0; i<size; i++)
data[i] += other.data[i];
}
double
Vector::operator()(int x) const
{
return data[x];
}
double &
Vector::operator()(int x)
{
if (x < 0 || x >= size) {
static double errorResult = 0;
std::cerr << "Vector::operator() " << x << " outside range 0 through " << size-1 << "\n";
return errorResult;
}
return data[x];
}
|
Hint
Wherever you see a comment //PROVDE CODE is a place you need to edit, e.g. line 8 requires some assignment using a new double[some value].