联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2021-01-14 10:35

SOF103 C and C++ Programming

Lab 10

Class Template and STL

Due Date: 17 Jan 2021

Late Submission Policy:

? Up to 4 hours late:10% penalty

? Up to 2 days late: 25% penalty

? More than 2 days late: 50% penalty

? No submission will be accepted after 4 days of the specified deadline: 100% penalty

1. The following is the definition of a class template TwoValues:

Write a main function that instantiate three objects of type int, double and float of the

class. The objects are given initial values of type int, double and float, respectively.

Call the function biggest() to compare the largest of the two values you have initialized for

each object.

2. The following is the class declaration of a class template AddTwoValues

that is used to add two values.

Complete the implementation of the class template. Write a main function to

test the addition of two integers, two double values and two float values.

3. A class template can specify more than one type. For example, in class

template AverageTwoValues below, the class receives two values (type T)

template <class T>

class TwoValues {

public:

TwoValues(T x, T y) {values[0] = x; values[1] = y; }

// default constructor

T biggest(); // function to return the biggest value

private:

T values[2];

};

template <class T>

T TwoValues<T>::biggest()

{

T bigger = (values[0] > values[1]) ? values[0] : values[1];

// find the biggest of the two values

return bigger;

}

template <class T>

class AddTwoValues {

public:

AddTwoValues(T x, T y) {a = x; b = y; } // default constructor

T add(); // function to add two values

private:

T a, b;

};

2

and calculate the average of the two values and return the result as type U. The type T

and U can be int, double and float.

Implement the class template above and test the class using objects of different

data types. For example, return the average as type int when the two values received

are type double.

4. Complete the implementation of class template MyArray below:

The constructor initializes all N elements of array Arr to zero. The function setArr

sets the x-th element of array Arr with the value specified by parameter value. The

function getArr returns the x-th element of array Arr. Write the main function to

test the class template.

5. Use C++ Standard Template Library <vector> to do the followings:

(a) Declare a vector named v1 of type int.

(b) Use function push_back() to store value 1, 2, 3 and 4 into v1.

(c) Print out the size of v1.

(d) Use a for loop to print out each element value of v1.

(e) Assign a new value 5 to element 1.

(f) Remove the last element.

(g) Print out all elements of v1 using an iterator named it to access each element of

v1.

(h) Insert new element value 10 at position 0 (the first element).

(i) Use erase() to remove element at position 2.

(j) Use a for loop to print out each element value of v1 again.

(k) Remove all the elements in the vector v1.

(l) Check if the vector v1 is empty.

template <class T, class U>

class AverageTwoValues {

public:

AverageTwoValues(T x, T y) {a = x; b = y; } // constructor

U average(); // function to calculate average of two values

// return result as type U

private:

T a, b;

};

template <class T, int N>

class MyArray {

public:

MyArray(); // default constructor

void setArr (int x, T value);// set element x-th with value

T getArr (int x);// return element x-th of the array

private:

T Arr [N]; // Arr has N elements with type T

};


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp