Sei sulla pagina 1di 10

Name:________________________

Score:

/44

Instructions: This exam consists of two parts. The first part is a closed book exam. You are not
allowed to use any aids whatsoever. Once you are finished and have handed in the first part, you can
use a computer for the second part but you must write your answers on this exam paper. You are
allowed to use Code::Blocks only. If the instructor notices you using any other software, you will
immediately get 0 for this test.
Before starting this test, log in to Moodle and download the folder with the resources for the second
part of the test. Start Code::Blocks and unplug the network cable from your computer.
After you hand in your test, please plug the network cable back in.
Marks for each question are shown in [ ] unless they are worth 1 mark. If you attempt more than
one solution, clearly indicate which you wish to be marked.
Please write your name at the top of each page that you detach! Read the questions carefully!
.

Good Luck!

1) [16] Carefully examine the declaration of the class TT1 given below and answer the following
questions. You can detach this page, but please submit it with the rest of your test.
#ifndef TT1_H
#define TT1_H
#include <string>
class TT1
{
public:
/*initialize my_string to "Horse", and create array words of size 10 */
TT1();
/* initialize my_string to c, and create an array words of size num */
TT1(std::string c, int num);
/* initialize my_string to "", create an array words of size 10 */
/* and initialize all the array words to a */
TT1(std::string a);
/* create an object that is a copy of t1 */
TT1(const TT1& t1);
/* overloaded assignment operator */
void operator=(const TT1& t1);
/* the size of the words array */
int words_size;
std::string get_my_str();
private:
std::string my_string;
std::string* words;
/* accessor for the my_str data member */
std::string get_words();
/* returns the k-th element of my_array */
/* no error checking is performed */
std::string get_words_at_loc(int k);
};
#endif // TT1_H

In main the following is declared:


TT1 b("Cat",5), c[5], d("Dog",10), e(b);

What is displayed by each of the following? If the given code produces an error, write the error and
explain it.
2

a) cout << d.words_size;


outputs 10
b) cout << b.my_string;
error my_string is private

c) cout << e.get_my_str();


output: Cat
d) b.set_my_string("Ant");
class TT1 has no member named set_my_string

e) cout << c.get_my_str();


c is not an object of TT1

f) cout << c[1].get_my_str().at(0);


output:H

Answer the following questions:


g) How many constructors does the class TT1 have?
4
h) [2] What are the two biggest design flaws of the class?
1. Some data members are declared public.
2. The class uses dynamic data, but doesnt specify a destructor so it can cause memory
leaks.

i) [2] What do we usually call the function: TT1(const TT1& t1); included above? Why do we need to
add that function to the class? Give an example of when this function will be used.
The function is called a copy constructor. We need a copy constructor because the class uses
dynamic data. For example, this constructor will be called when an object of the class is passed to a
function by value, so the function works with different memory locations.

i) [2] Write the implementation of the function std::string get_my_str();


std::string TT1::get_my_str()
{
return my_string;
}

j) [3] Write an implementation of a member function glue_words() that returns all the words from the
array words glued together in the string in which the words are separated by a blank.
std::string TT1::glue_words()
{
std::string glue = "";
for (int i =0; i < words_size; i++ )
glue = words[i] + " " + glue;
glue = glue.substr(0,glue.size()-1);
return glue;
}

3. [6] Write a declaration of a class called Circle that can be used as follows. You can refer to
Question 1 for syntax. You dont need to write the implementation of the class.
Circle c(10);
cout <<
<<
<<
<<

// 10 is the radius of the circle

"
Radius = " << c.get_radius() << endl
"
Area = " << c.get_area() << endl
" Circumference = " << c.get_circumference()
endl;

Circle d(c);
cout <<
<<
<<
<<

"
Radius = " << d.get_radius() << endl
"
Area = " << d.get_area() << endl
" Circumference = " << d.get_circumference()
endl;

d.set_radius(100);
cout <<
<<
<<
<<

"
Radius = " << d.get_radius() << endl
"
Area = " << d.get_area() << endl
" Circumference = " << d.get_circumference()
endl;

Write your solution here:


#ifndef CIRCLE_H
#define CIRCLE_H
class Circle
{
public:
Circle();
Circle(double r);
Circle(const Circle& c);
double get_area();
double get_circumference();
double get_radius();
void set_radius(double r);

};

private:
double radius;

#endif // CIRCLE_H
5

Part 2
1) [15] A definition for Point2D (representing a point on a 2D plane), and a Circle class
(representing a 2D circle in a standard coordinate system) is in the folder you downloaded
from Moodle. Study the specification of these classes carefully before proceeding. Write the
implementation of all member functions of class Circle.
#include "circle.h"
#include "Point2D.h"
Circle::Circle()
{
centre.set_coordinates(0,0);
radius = 0;
}
Circle::Circle(Point2D p, double r)
{
centre = p;
radius = r;
}
Circle::Circle(double x, double y, double r)
{
centre.set_coordinates(x,y);
radius = r;
}
double Circle::get_area() const
{
return 3.14*radius*radius;
}
bool operator<(Circle& c1, Circle& c2)
{
return (c1.get_area() < c2.get_area());
}
bool Circle::intersects(const Circle c1) const
{
return (centre.distance(c1.centre) <= radius + c1.radius);
}
Point2D Circle::get_centre() const
{
return centre;
}
std::ostream& operator<<(std::ostream& out_stream, const Circle c)
{
out_stream << "Circle with centre: (" << c.centre.get_x() << "," <<
c.centre.get_y()<< ") and radius " << c.radius;
return out_stream;
}
6

P2

2) [7] Write a function only that takes a vector of strings and returns a vector that contains all
unique elements of the input vector.

For example (your function should work for any input vector of strings) if the input vector is
v={"one","two","three","one","two","one","two","three","three","one"};

your function would return {"one","two","three"} as these are all unique elements of v.
Your function should also display on the screen a log of how many duplicates of each element
it removed. For the above example, the log would be
Removed 3 occurrences of one.
Removed 2 occurrences of two.
Removed 2 occurrences of three.

You can define helper functions if this makes your solutions simpler.
vector<string> remove_repeats(vector<string> words)
{
vector<string> unique_words;
int num_repeats;
for (int i = 0; i < words.size(); i++)
{
num_repeats = 0;
for (int j = i+1; j < words.size();j++)
if (words.at(i) == words.at(j))
{
num_repeats++;
words.erase(words.begin()+j);
j--;
}
cout << "Removed " << num_repeats << " occurrences of " <<
words.at(i) << endl;
unique_words.push_back(words.at(i));
}
}

#ifndef POINT2D_H
#define POINT2D_H
class Point2D
{
public:
// POST: a 2D point at the origin (0,0) is created
Point2D();
// PRE: x and y are defined
// POST: a 2D point with given coordinate is created
Point2D(double x, double y);
// PRE: x and y are defined
// POST: the coordinate of this point is changed to x and y
void set_coordinates(double x, double y);
//POST: the x coordinate of this point is returned
double get_x() const;
//POST: the y coordinate of this point is returned
double get_y() const;
//PRE: second_point is defined.
//POST: the distance between this point and second_point returned.
double distance(Point2D second_point) const;
// PRE: second_point is defined.
// POST: a point halfway between this and second_point is returned.
Point2D mid_point(Point2D second_point) const;

private:
double x, y;
};
#endif // POINT2D_H

#include "Point2D.h"
#include <cmath>
// POST: a 2D point at the origin (0,0) is created
Point2D::Point2D()
{
x=y=0;
}
// PRE: x and y are defined
// POST: a 2D point with given coordinate is created
Point2D::Point2D(double x, double y)
{
this->x = x;
this->y = y;
}
// PRE: x and y are defined
// POST: the coordinate of this point is changed to x and y
void Point2D::set_coordinates(double x, double y)
{
this->x = x;
this->y = y;
}
//POST: the x coordinate of this point is returned
double Point2D::get_x() const
{
return x;
}
//POST: the y coordinate of this point is returned
double Point2D::get_y() const
{
return y;
}
//PRE: second_point is defined.
//POST: the distance between this point and second_point returned.
double Point2D::distance(Point2D second_point) const
{
return sqrt((x-second_point.x)*(x-second_point.x)+(ysecond_point.y)*(y-second_point.y));
}
// PRE: second_point is defined.
// POST: a point halfway between this and second_point is returned.
Point2D Point2D::mid_point(Point2D second_point) const
{
Point2D p;
p.x = (x+second_point.x)/2;
p.y = (y+second_point.y)/2;
}
9

#ifndef CIRCLE_H
#define CIRCLE_H
#include "Point2D.h"
#include <ostream>
class Circle
{
public:
// POST:Circle with center at the origin and radius 0 is created
Circle();
//PRE: p and r are defined an r >= 0
//POST: Circle with centre at p and radius r is created
Circle(Point2D p, double r);
//PRE: x, y and r are defined an r >= 0
//POST: Circle with centre at point (x,y) and radius r is created
Circle(double x, double y, double r);
//POST: the area of the circle is returned
double get_area() const;
//POST: the perimeter of the circle is returned
double get_perimeter() const;
//POST: The center of the circle is returned
Point2D get_centre() const;
//PRE: c1 and c2 are defined
//POST: if c1 has greater area than c2 return true otherwise return
false

friend bool operator<(const Circle& c1, const Circle& c2);


//PRE: c1 and c2 are defined
//POST: return true if c1 and c2 intersect, false otherwise
bool intersects(const Circle c1) const;

//POST:: the data of the circle is put on ostream


friend std::ostream& operator<<(std::ostream& out_stream, const
Circle c);

};

private:
Point2D centre;
double radius;

#endif // CIRCLE_H

10

Potrebbero piacerti anche