473,670 Members | 2,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

defining new types

I'm sure there's a fairly easy answer for this... but how can I define a new
type with range checking?

Example: I want to define a new type that's like a double, except that you
can only give it values from 0.0 to 100.0. I'd also like it to act like a
double as much as possible, except that an exception is thrown when it's set
to an invalid number.

Ideas?

Thanks,
Joe
Jul 22 '05 #1
10 1865
* Joe Laughlin:

I'm sure there's a fairly easy answer for this... but how can I define a new
type with range checking?

Example: I want to define a new type that's like a double, except that you
can only give it values from 0.0 to 100.0. I'd also like it to act like a
double as much as possible, except that an exception is thrown when it's set
to an invalid number.

Ideas?


The difference between original C++ and C was that C++ had classes.

A class is a type.

To define a new type, define a class.

Supply the operations you want the type to have.

Get yourself a good C++ book, e.g. "Accelerate d C++".

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
Joe Laughlin wrote:
I'm sure there's a fairly easy answer for this... but how can I define a new
type with range checking?

Example: I want to define a new type that's like a double, except that you
can only give it values from 0.0 to 100.0. I'd also like it to act like a
double as much as possible, except that an exception is thrown when it's set
to an invalid number.

Ideas?


This google groups link points to a recent discussion on comp.std.c++.

http://tinyurl.com/6zyg9
Jul 22 '05 #3
Joe Laughlin posted:
I'm sure there's a fairly easy answer for this... but how can I define
a new type with range checking?

Example: I want to define a new type that's like a double, except that
you can only give it values from 0.0 to 100.0. I'd also like it to act
like a double as much as possible, except that an exception is thrown
when it's set to an invalid number.

Ideas?

Thanks,
Joe

Use your brain:

class RestrictiveDoub le
{
public: class bad_proposal {};

private:

double data;

Set(double const proposed)
{
if (propose > 100 || proposed < 0) throw bad_proposal;

data = proposed;
}

public:

RestrictiveDoub le& operator=(doubl e const proposed)
{
Set(proposed);
}

//Copy constructor is unnecessary

//Put a constructor here

//Put an operator double here
};

I would've finished it for you, but then half way through I thought it may
have been homework for you.

-JKop
Jul 22 '05 #4
JKop wrote:
Joe Laughlin posted:
I'm sure there's a fairly easy answer for this... but
how can I define a new type with range checking?

Example: I want to define a new type that's like a
double, except that you can only give it values from 0.0
to 100.0. I'd also like it to act like a double as much
as possible, except that an exception is thrown when
it's set to an invalid number.

Ideas?

Thanks,
Joe

Use your brain:

class RestrictiveDoub le
{
public: class bad_proposal {};

private:

double data;

Set(double const proposed)
{
if (propose > 100 || proposed < 0) throw
bad_proposal;

data = proposed;
}

public:

RestrictiveDoub le& operator=(doubl e const proposed)
{
Set(proposed);
}

//Copy constructor is unnecessary

//Put a constructor here

//Put an operator double here
};

I would've finished it for you, but then half way through
I thought it may have been homework for you.

-JKop


What's an "operator double"? And I'm confused why you are defining a class
bad_proposal inside of RestrictiveDoub le, and then throwing it.
Jul 22 '05 #5
What's an "operator double"? And I'm confused why you are defining a
class bad_proposal inside of RestrictiveDoub le, and then throwing it.


Sorry, I'd like to help you, but I still suspect that this is some sort of
homework question.

If you have a decent book on C++, then go to the chapter on "operator
overloading", the conversion operators will be in there with it.

As regards defining one class within another: All it means is that, instead
of the class's name being "bad_propos al", its name is
"RestrictiveDou ble::bad_propos al". Also, if I were to define the
"bad_propos al" class within the private section of the "RestrictiveDou ble"
class definition, then it would be inaccessible from outside of the class's
own code. (Also I wouldn't be able to throw it as the caller wouldn't be
able to play with it).
-JKop
Jul 22 '05 #6
JKop wrote:
What's an "operator double"? And I'm confused why you
are defining a class bad_proposal inside of
RestrictiveDoub le, and then throwing it.
Sorry, I'd like to help you, but I still suspect that
this is some sort of homework question.


Not homework.
If you have a decent book on C++, then go to the chapter
on "operator overloading", the conversion operators will
be in there with it.
I understand about operator overloading, just never heard of "operator
double". I've only heard of the usual operator==, operator>>, etc.

As regards defining one class within another: All it
means is that, instead of the class's name being
"bad_propos al", its name is
"RestrictiveDou ble::bad_propos al". Also, if I were to
define the "bad_propos al" class within the private
section of the "RestrictiveDou ble" class definition, then
it would be inaccessible from outside of the class's own
code. (Also I wouldn't be able to throw it as the caller
wouldn't be able to play with it).
-JKop


Jul 22 '05 #7
I understand about operator overloading, just never heard of "operator
double". I've only heard of the usual operator==, operator>>, etc.

Here's the jist of it:
class Blah
{
public:

operator bool() const
{
return true;
}
};

void SomeFunc(bool const monkey)
{

}
intm main()
{
Blah blah_object;

SomeFunc(blah_o bject);
}

Jul 22 '05 #8
JKop wrote:
I understand about operator overloading, just never
heard of "operator double". I've only heard of the
usual operator==, operator>>, etc.

Here's the jist of it:
class Blah
{
public:

operator bool() const
{
return true;
}
};

void SomeFunc(bool const monkey)
{

}
intm main()
{
Blah blah_object;

SomeFunc(blah_o bject);
}


So, you can use Blah anywhere you can use a bool?
Jul 22 '05 #9
Joe Laughlin posted:
JKop wrote:
I understand about operator overloading, just never
heard of "operator double". I've only heard of the usual operator==,
operator>>, etc.

Here's the jist of it:
class Blah
{
public:

operator bool() const {
return true; } };

void SomeFunc(bool const monkey)
{

}
intm main()
{
Blah blah_object;

SomeFunc(blah_o bject); }


So, you can use Blah anywhere you can use a bool?

Yep, for instance:

while (blah_object)
{
;
}

if (blah_object) ;

-JKop
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2270
by: Erik Max Francis | last post by:
I've come across a limitation in unpickling certain types of complex data structures which involve instances that override __hash__, and was wondering if it was known (basic searches didn't seem to come up with anything similar) and if there is a workaround for it short of restructuring the data structures in question. The fundamental issue rests with defining classes which override __cmp__ and __hash__ in order to be used as keys in...
7
2689
by: Senthilraja | last post by:
I have the following program using templates. Someone please let me know the syntax to be used for defining the member functions push, pop etc. as non-inline functions. #include <iostream> using namespace std; template<class T, int size = 50> class Stack {
12
3197
by: Matt Garman | last post by:
I'd like to create a "custom output facility". In other words, I want an object whose use is similar to std::cout/std::cerr, but offers more flexibility. Instead of simply writing the parameter to stdout/stderr, I'd like it to write to stdout, to a file, and/or call a logging function. So my output function might look something like this: OutputFacility& OutputFacility::put(const std::string& s) { cout << s; // print to stdout
8
1419
by: johny smith | last post by:
If I have a simple class with say a couple of integers only is there any need for me to provide a destructor? thanks!
10
2119
by: nambissan.nisha | last post by:
I am facing this problem.... I have to define a structure at runtime as the user specifies... The user will tell the number of fields,the actual fields...(maybe basic or array types or multiple arrays,etc) I do not understand how to define the structure at run time.i.e.what fields it will contain.
11
3426
by: mesut demir | last post by:
Hi All, When I create fields (in files) I need assign a data type like char, varchar, money etc. I have some questions about the data types when you create fields in a file. What is the difference between data type 'CHAR' and 'TEXT'? When do you use 'VAR' in your datatype word? e.g. VARCHAR ?
2
1492
by: lcaamano | last post by:
We have a tracing decorator that automatically logs enter/exits to/from functions and methods and it also figures out by itself the function call arguments values and the class or module the function/method is defined on. Finding the name of the class where the method we just entered was defined in is a bit tricky. Here's a snippet of the test code: class Base: @tracelevel(1)
2
1277
by: Martin Z | last post by:
Hi, I'm trying to support a 3rd-party XML format for which there is no schema. As such, I've been making my objects that map to their format using the XMLSerializer... but there's one object that stops me. We have a class called Abstract-A, and a collection of Abstract-As we'll call A-List. One particular subtype of AbstractA is bothering me, because it works like this:
8
1849
by: lubomir dobsik | last post by:
hi, i have seen an interesting thing: #if sizeof((char*)0 - (char*)0) == sizeof(unsigned int) typedef unsigned int size_t; #elif sizeof((char*)0 - (char*)0) == sizeof(unsigned long) typedef unsigned long size_t; #elif sizeof((char*)0 - (char*)0) == sizeof(unsigned long long) typedef unsigned long long size_t; #endif
0
8471
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8903
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8815
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8592
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8661
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4213
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4393
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2802
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.