473,378 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

C++ design question

Hi,

I have a design question. I am making a time series analysis tool.
Since I already use STL vector to represent time series, is there a
need to implement a class for the time series object? Right now I
just use

typedef vector<double>TS;

Is that enough? I feel that I am not taking advantage of the C++
encapsulation. But implementing the class seems redundant.. I'd
appreciate some insight! Thanks in advance!

Jessica
Jul 19 '05 #1
7 3663
"Jessica" <jl*****@earthlink.net> wrote in message
news:d1**************************@posting.google.c om...
Hi,

I have a design question. I am making a time series analysis tool.
Since I already use STL vector to represent time series, is there a
need to implement a class for the time series object? Right now I
just use

typedef vector<double>TS;

Is that enough? I feel that I am not taking advantage of the C++
encapsulation. But implementing the class seems redundant.. I'd
appreciate some insight! Thanks in advance!

Jessica


I wouldn't say that "taking advantage of the C++ encapsulation" is an issue.
But as you develop your project it seems likely you will find more to say
about a time series than just the time values themselves. If so you will
wish you had started with a class like this:

class TimeSeries
{
public:
typedef double t_time;
typedef std::vector<double> t_series;
private:
t_series m_series;
// probably other stuff to be added here later
public:
TimeSeries() {}
template <typename ITER> TimeSeries(ITER first, ITER last) :
m_series(first, last) {}
const t_series &series() const {return m_series;}
t_series &series() {return m_series;}
// accessors for new stuff to be added here as needed
};

[untested code] As you can see, there isn't that much to it and if you
decide to add some other properties to TimeSeries you can do so without
breaking your code. If you diligently declare times as TimeSeries::t_time
and container references as TimeSeries::t_series you can change your mind
about those types later. You will have to use syntax such as:

TimeSeries::t_time t = my_ts.series().front();

rather than

TimeSeries::t_time t = my_ts.front();

Not so terrible, IMHO.

--
Cy Edmunds
http://home.rochester.rr.com/cyhome/
Jul 19 '05 #2
Jessica wrote:
I have a design question. I am making a time series analysis tool.
Since I already use STL vector to represent time series, is there a
need to implement a class for the time series object? Right now I
just use

typedef vector<double>TS;

Is that enough? I feel that I am not taking advantage of the C++
encapsulation. But implementing the class seems redundant.. I'd
appreciate some insight! Thanks in advance!


Forget encapsulation.

(On modern projects, a given binary only grows so large before we use a pipe
or COM to plug it into a bigger app, so that's the encapsulation boundary.)

Focus on removing duplication.

If two clients do similar things to a vector of doubles, refactor them until
their code looks similar.

Then, replace the two similar procedures with a call to a new function,
containing those common lines.

The simplest place to put that common function is inside a class that also
contains TS.

Voila: Objects.

--
Phlip
http://www.c2.com/cgi/wiki?TestFirstUserInterfaces


Jul 19 '05 #3
Thank you all for your professional opinions. Now I look at this
language with a whole new perspective. As you can probably tell, I am
a student and haven't really been thinking about object-orientation
from a practical view. :) This is my first project in the real world,
and I am glad that I consulted with you pros first!

Jessica
"Phlip" <ph******@yahoo.com> wrote in message news:<bd********@dispatch.concentric.net>...
Jessica wrote:
I have a design question. I am making a time series analysis tool.
Since I already use STL vector to represent time series, is there a
need to implement a class for the time series object? Right now I
just use

typedef vector<double>TS;

Is that enough? I feel that I am not taking advantage of the C++
encapsulation. But implementing the class seems redundant.. I'd
appreciate some insight! Thanks in advance!


Forget encapsulation.

(On modern projects, a given binary only grows so large before we use a pipe
or COM to plug it into a bigger app, so that's the encapsulation boundary.)

Focus on removing duplication.

If two clients do similar things to a vector of doubles, refactor them until
their code looks similar.

Then, replace the two similar procedures with a call to a new function,
containing those common lines.

The simplest place to put that common function is inside a class that also
contains TS.

Voila: Objects.

Jul 19 '05 #4
Hi,

I started implementing the wrapper class for vector<double> but I
ran into some trouble. Hope some of you can help me.

First of all, I was trying to over load the [] operator:

// operator overloading. _tsVal is a private member declared as
vector<Type>
Type& operator[](int its) const
{
return _tsVal.at(its);
}

But the compiler complains that I am trying to convert
std::allocator<_Ty>::value_type to double&. So I changed the return
type to vector<Type>&

Now it compiles, but I get stuck in trying to overload the +=
operator. In the client code I want to be able to do something like
this:

myClassObject[i] += someDouble; or
someDouble += myClassObject[i]

But if operator[] returns the reference to the vector, how can I get
the value of myClassObject[i]? What do I pass in as the operand?

Perhaps I am doing something wrong. Please help. Thank you!

Jessica
"Phlip" <ph******@yahoo.com> wrote in message news:<bd********@dispatch.concentric.net>...
Jessica wrote:
I have a design question. I am making a time series analysis tool.
Since I already use STL vector to represent time series, is there a
need to implement a class for the time series object? Right now I
just use

typedef vector<double>TS;

Is that enough? I feel that I am not taking advantage of the C++
encapsulation. But implementing the class seems redundant.. I'd
appreciate some insight! Thanks in advance!


Forget encapsulation.

(On modern projects, a given binary only grows so large before we use a pipe
or COM to plug it into a bigger app, so that's the encapsulation boundary.)

Focus on removing duplication.

If two clients do similar things to a vector of doubles, refactor them until
their code looks similar.

Then, replace the two similar procedures with a call to a new function,
containing those common lines.

The simplest place to put that common function is inside a class that also
contains TS.

Voila: Objects.

Jul 19 '05 #5
Hi,

Never mind my silly question. After thinking about it more
carefully, I realized that I've got the concept of operator
overloading all wrong. But can someone please tell me why

someDouble += myClassObject[i];

doesn't work if [] has higher precedence? Thanks.

Jessica

jl*****@earthlink.net (Jessica) wrote in message news:<d1**************************@posting.google. com>...
Hi,

I started implementing the wrapper class for vector<double> but I
ran into some trouble. Hope some of you can help me.

First of all, I was trying to over load the [] operator:

// operator overloading. _tsVal is a private member declared as
vector<Type>
Type& operator[](int its) const
{
return _tsVal.at(its);
}

But the compiler complains that I am trying to convert
std::allocator<_Ty>::value_type to double&. So I changed the return
type to vector<Type>&

Now it compiles, but I get stuck in trying to overload the +=
operator. In the client code I want to be able to do something like
this:

myClassObject[i] += someDouble; or
someDouble += myClassObject[i]

But if operator[] returns the reference to the vector, how can I get
the value of myClassObject[i]? What do I pass in as the operand?

Perhaps I am doing something wrong. Please help. Thank you!

Jessica
"Phlip" <ph******@yahoo.com> wrote in message news:<bd********@dispatch.concentric.net>...
Jessica wrote:
I have a design question. I am making a time series analysis tool.
Since I already use STL vector to represent time series, is there a
need to implement a class for the time series object? Right now I
just use

typedef vector<double>TS;

Is that enough? I feel that I am not taking advantage of the C++
encapsulation. But implementing the class seems redundant.. I'd
appreciate some insight! Thanks in advance!


Forget encapsulation.

(On modern projects, a given binary only grows so large before we use a pipe
or COM to plug it into a bigger app, so that's the encapsulation boundary.)

Focus on removing duplication.

If two clients do similar things to a vector of doubles, refactor them until
their code looks similar.

Then, replace the two similar procedures with a call to a new function,
containing those common lines.

The simplest place to put that common function is inside a class that also
contains TS.

Voila: Objects.

Jul 19 '05 #6
"Jessica" <jl*****@earthlink.net> wrote...
Never mind my silly question. After thinking about it more
carefully, I realized that I've got the concept of operator
overloading all wrong. But can someone please tell me why

someDouble += myClassObject[i];

doesn't work if [] has higher precedence? Thanks.


Your question makes no sense, sorry. [] _always_ has higher
precedence, there can be no "if". And if whatever you wrote
doesn't work, how can anybody tell you why it doesn't work
without seeing what you actually wrote?

Victor
Jul 19 '05 #7
Hi Cy,

Thanks! I didn't have a const return type on my const function:

(const) Type &operator [] (int) const;

Adding the "const" on this one, as well as adding another non-const
function, solved all the problems! And thanks for all the reasonings
behind it.

Jessica
p.s. Victor, sorry I didn't phrase the question well enough -- I was
questioning why the compiler complains about += in this statement,
SINCE [] has higher precedence:

a += b[i]

I thought the compiler was complaining that I didn't overload the +=
operator, but apparently it was because I didn't implement []
overloading correctly, as Cy explained.

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message news:<pq*****************@twister.nyroc.rr.com>...
"Jessica" <jl*****@earthlink.net> wrote in message
news:d1**************************@posting.google.c om...
Hi,

I started implementing the wrapper class for vector<double> but I
ran into some trouble. Hope some of you can help me.

First of all, I was trying to over load the [] operator:

// operator overloading. _tsVal is a private member declared as
vector<Type>
Type& operator[](int its) const
{
return _tsVal.at(its);
}


When you declare the function to be "const" you are promising that the
function does not change the internal state of the object. So you can't
change _tsVal, for instance, and you can't call another function unless it
is also const. If you look at the code for <vector> I am sure you will see
that the "at" function has a const version which returns a const reference
and a non-const version which returns a non-const reference. When you wrote:

return _tsVal.at(its);

you were calling the const version of at() -- calling the non-const version
would violate const-correctness. Hence the return type was a const Type &
which you then tried to convert to a non-const Type &. I wasn't impressed
with the error message you got but that's life in template city.

It is usual for a const function to return only a const reference or const
pointer. You can write both in the same class:

Type &operator [] (int);
const Type &operator [] (int) const;

These are two different functions because they have different signatures
(the const declaration counts as part of the signature). I suggest you
implement both of these functions in your class and I think all will be
well.

BTW the practical implication of implementing both is that if your client
has a const object he can still use operator [] on the right hand side:

function yadda(const TimeSeries &ts)
{
double foo = ts[4]; // calls operator [] (int) const -- legal
ts[4] = 12.2; // calls operator [] (int) -- not legal
}

If he has a non-const object he can use operator [] on either side of the
assignment.

<snip>

Cy

Jul 19 '05 #8

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

Similar topics

5
by: Don Vaillancourt | last post by:
Hello all, Over the years as I design more database schemas the more I come up with patterns in database design. The more patterns I recognize the more I want to try to design some kind of...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
2
by: Test User | last post by:
Hi all, (please excuse the crosspost as I'm trying to reach as many people as possible) I am somewhat familiar with Access 2000, but my latest project has me stumped. So, I defer to you...
6
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
17
by: roN | last post by:
Hi, I'm creating a Website with divs and i do have some troubles, to make it looking the same way in Firefox and IE (tested with IE7). I checked it with the e3c validator and it says: " This...
6
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
19
by: neelsmail | last post by:
Hi, I have been working on C++ for some time now, and I think I have a flair for design (which just might be only my imagination over- stretched.. :) ). So, I tried to find a design...
8
by: indrawati.yahya | last post by:
In a recent job interview, the interviewer asked me how I'd design classes for the following problem: let's consider a hypothetical firewall, which filters network packets by either IP address,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.