Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask C/C++ Expert


Home >> C/C++

Assignment

QUESTION 1

Write a function template name maximum. The function takes two values of the same type as its arguments and returns the larger of the two arguments (or either value if they are equal). Give both the function declaration (prototype) and the function definition for the template. You will use the operator < in your definition. Therefore, this function template will apply only to types for which < is defined. Write a comment for the function declaration (prototype) that explains this restriction. Use Preconditions and Postconditions for the comment, similar to the example for the sort function declaration (prototype) on page 711, Display 16.2. Preconditions explains the input (parameters) for the function and the rules for what makes valid parameters. Postconditions explains the output of the function.

QUESTION 2

Write a function template name square. The function takes one values that value * itself. Give both the function declaration (prototype) and the function definition for the template. You will use the operator < in your definition. Therefore, this function template will apply only to types for which * is defined. Write a comment for the function declaration (prototype) that explains this restriction. Use Preconditions and Postconditions for the comment, similar to the example for the sort function declaration (prototype) on page 711, Display 16.2. Preconditions explains the input (parameters) for the function and the rules for what makes valid parameters. Postconditions explains the output of the function.

QUESTION 3

Would your function template for question 1 work if you gave it the arguments 3 and 4.5? Test it for yourself. If it works, just say yes it works. If it doesn't work, explain why not.

QUESTION 4

Did you know that you can actually write the solution for question 1 in such a way that it would work for more than one type? The book didn't get into this, but for example you could write a function like this:

template
void compare(T testval1, V testval2);

Just thought I would point this out since I didn't see it in the book. It is often a good practice to *not* allow for two different types unless you have specific reasons to allow for it. Writing this:

template
void compare(T testval1, T testval2);

Write a class template variation of the following class definition.
classbasicObject{
public:
basicObject();
basicObject(float newData);
floatgetData();
voidsetData(float newData);
private:
float data;
};

Replace all instances of float with the variable class T. You do not need to write implementations for the functions.

QUESTION 8

Is it legal for a derived template class to start as shown in the following code? The template class TwoDimPFArrayBak is designed to be a two-dimensional partially filled array with backup.

template< class T >
classTwoDimPFArrayBak : public PFArray< PFArray< T > >
{
public:
TwoDimPFArrayBak();
Note that the space in < PFArray< T > > is important, or at least the last space is. If the space between the next-to-last > is omitted, then the compiler may interpret >> to be the extraction operator used for input expressions such as cin >> n; rather than interpreting it as a nested < >.
What would need to be added

QUESTION 9

Consider the following toString function which converts an integer into a string using the ostringstream object type. (For more details on this technique see Chapter 12)
std::stringtoString(intval){
std::ostringstream outs; //create a string output stream object
outs< returnouts.str(); //return the string stored on the string output stream
}

Re-write this function as a function template which will convert any data type to a string. Should work for any data type which will work as the right-hand-1side of the << operator (as the integer val is doing here).

QUESTION 10

As a bit of outside reading, please browse some of the class templates available in the Standard Template Library (STL).

Read through a few of these, in particular the Algorithms and Containers.

All of these and more are made possible by class templates.

Describe the things you're most excited to see here and what you might want to use them for. Of course vector is great, but what are some other useful tools and how might you want to use them?

[Name at least 2 that were interesting to you, give some real-life application ideas. Feel free to gush.]

QUESTION 11

Considering the examples from the textbook and the examples from the STL, typical uses of class templates can be fairly abstract.

Put your mind to it and think of one other example of a good class template to make other than the ones listed in STL and the textbook. Perhaps it could be related to program 2 (I've got at least one idea there, I'll tell you about it next week). Provide a description of it and the types of member functions it would have / the purpose it would serve.

If you can't think of a class template to make, instead tell me an idea you had and what was wrong with it [Why you didn't feel like it would make a good class template afterall.]

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M92517188

Have any Question?


Related Questions in C/C++

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As