[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 407
  • Last Modified:

help with Template conversion

I have a small program that i need to make a template and allow for multiple types of data. I am not familiar with templates and could use some help.

Here is what i have so far.


#include <iostream>
#include <windows.h>



using namespace std;

class subRange
{
      public:
            subRange( int, int );
            int getValue( );

      private:
            int lower, upper;
            exception newException;
};
      
subRange::subRange( int low, int high )
{
      lower = low;
      upper = high;
}
      
int subRange::getValue()
{

      int v;
      cout << "Enter value [ " << lower << ", " << upper << " ]: ";
      cin >> v;
      
      
      if (v  < lower || v >  upper )
            throw newException;
      
      return v;
}
      
int main()
{
      subRange x(1,10);

      try {
      cout << x.getValue( ) << endl;
      cout << "You have entered a Valid value" << endl;
      
      }
      catch (exception e)
      {
            cout << "Value out of Range" << endl;
      
      }
      Sleep (5000);
}
0
chetmunk700
Asked:
chetmunk700
  • 4
1 Solution
 
phoffricCommented:
What kinds of multiple types of data do you need to account for? I see that you are doing the following test:        v  < lower
This might not make any sense for some types of data, unless you know how to overload the "<" operator. But for POD types such as short, long, float, double, the "<" is built-in, so you should have no problem.

Here is a C++ Template Tutorials for you to review:
    http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm

To templatize your class, you start with:
template <class elemType>
class subRange

Open in new window

and then replace the specific type in your class with the generic elemType.

All the functions and construtors should remain in your header file when working with templates. You need to modify these constructors and functions as follows:
template <class elemType>
your_return_type  subRange<elemType>::your_function_or_constructor(...) {...}

template <class elemType>
subRange<elemType>::your_constructor(...) {...}

Open in new window


Then, in your main program where you use this class, you need to specify which type you are interested in. For example:
subRange<int> x(low, high);

Open in new window

0
 
phoffricCommented:
In addition to the PODs already mentioned, you could use string class as well, since "<" is defined for C++ string class.

BTW, if you want to throw newException, then make the following changes:
template <class elemType>
class subRange
{
public:
    class newException {};
   ...
};
  ...
   throw newException();

Open in new window

0
 
chetmunk700Author Commented:
I think it also wanted me to allow for char type and i'm not sure why.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
phoffricCommented:
char type is also a POD.
char is just a type in the integer family. It's an integer that almost always is just 8 bits.
signed char range is from -128 .. + 127
unsigned char range is from 0 .. + 255

When you say
   char val = 'A';
you are just putting a number into val; a number when sent to a screen console will show up as an A.
0
 
evilrixSenior Software Engineer (Avast)Commented:
Just to clear up some semantics: the terminology POD means "plain old data" but that doesn't mean it will have native support for a relational operator. A struct, for example, can be a POD. I suspect that you mean intrinsic type (ie, one of the standard build-in types).

http://en.wikipedia.org/wiki/Plain_old_data_structure
0
 
phoffricCommented:
Throughout the C++ standard are references to POD-struct. But I didn't notice this line:
Scalar types, POD-struct types, POD-union types (clause 9), arrays of such types and cv-qualified versions of these types (3.9.3) are collectively called POD types.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now