473,886 Members | 2,377 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 1880
* 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
2290
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
2702
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
3229
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
1436
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
2137
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
3450
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
1508
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
1289
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
1861
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
11172
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
10770
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
10872
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
10429
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
9592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7139
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5808
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...
2
4235
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.