Mid-term Exam 2 (90 minutes) Date: Dec.14.2017
Name:Number: / Signature:
1(50 points) Given Rect.h andtest_Rec.cpp, write the correspondingimplementation and header files(Rect.h, Point.h, Point.cpp) using C++ codes.
// Rect Class Header file : Rect.h
#ifndef REC_H
#define REC_H
#include "Point.h"
class Rect
{
public:
Rect (); //initialize Rect. sides with point (0,0) and (1,1) as the opposite corners
Rect (const Point& src_p1, const Point& src_p3); // initialize Rect. with the given points
voidsetCorners(const Point& src_p1, const Point& src_p3); //set p1 and p3 corners of Rect.
float diagonal() const; //output the diagonal of the Rect.
float area()const; //output the area of the Rect.
void print()const; //displays all of the corners :p1, p2, p3, p4
private:
Point p1;
Point p3;
void getOtherCorners(Point* p2ptr,Point* p4ptr) const; //print calls it: returns pointers as other corners
};
#endif
#include <iostream
#include "Point.h"
#include "Rect.h"
using namespace std;
int main()
{
Rect r1;
cout<r1.diagonal()<endl; // 1.41421
Point p1(2, 4);
Point p3(4, 6);
r1.setCorners(p1, p3);
cout<r1.diagonal()<endl; //2.82843
r1.print();
//p1:(2, 4)
//p2:(4, 4)
//p3:(4, 6)
//p4:(2, 6)
coutr1.area(); //4
return 0;
} / Note:(coordinates of opposite corners and ) is considered to prevent a huge ambiguity.
2(50 points) Given Vec.hand test_Vec.cppwrite the corresponding implementation file Vec.cppusing C++ codes.
#ifndef VEC_H#define VEC_H
#include <iostream
using namespace std;
class Vec
{
friend ostream& operator<( ostreamout, const Vecrhs );
public:
//------constructors
Vec(); //empty constructor: with vector values 0 and vector size 1
Vec(const int Size); //constructor with vector values 0 and vector size Size
Vec(const int Size, const float* arr); //constructor with a float array and vector size Size
//------basic functions
void print() const; //prints vector
void fill(float val); //fills vector with a value
int getSize() const; //returns size of the vector
float* getVector(); //returns a pointer of the array
//------operator overlading
//indexing
float& operator[](const int ind); //subscript operator for non-const objects, returns modifiable lvalue
float operator[](const int ind) const; //subscript operator for non-const objects, returns rvalue
//assignments (all element-wise operations)
Vec& operator=(const Vecrhs);
Vec& operator+=(const Vecrhs);
Vec& operator*=(const Vecrhs);
//comparison
int operator==(const Vecrhs) const; //returns -1 or 1
//preinc
Vec& operator++(); //increments each element
private:
float* data;
int SIZE;
};
#endif
//test_Vec.cpp
#include <iostream
#include <iomanip
#include <cmath
#include "Vec.h"
using namespace std;
int main()
{
float arr[5]={1.25, 2.23, 3.45, 4.01, 5.12};
Vec v1; v1.print(); //[ 0.00]
Vec v2(5); v2.print(); //[ 0.00 0.00 0.00 0.00 0.00]
Vec v3(5, arr); v3.print();//[ 1.25 2.23 3.45 4.01 5.12]
v2.fill(1.0);
v2+=v3; cout<v2; //[ 2.25 3.23 4.45 5.01 6.12]
v2*=v3; cout<v2; //[ 2.81 7.20 15.35 20.09 31.33]
(++v2); cout<v2; //[ 3.81 8.20 16.35 21.09 32.33]
(--v2); cout<v2; //[ 2.81 7.20 15.35 20.09 31.33]
cout< v2[2] <endl; //15.35
cout< (v2==v3) <endl; //0
return 0;
}
//hint: data=new float[SIZE];
Dr. Muharrem Mercimek