Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

doubt with abstract data type list

37 views
Skip to first unread message

saulmartin...@gmail.com

unread,
Mar 20, 2019, 6:43:07 PM3/20/19
to
Hi,
I just started learning c++, in my classroom the teacher wrote code of static list using class template, the list is based on an array, it works fine for what was tried, integers, floats, strings and as well for abstract data (classA), because he wrote also a class that has one integer, one float and one string so the class template list is also able to work with that abstract data type, everything was tried in the same console application and worked fine, my questions for abstract data type, how or where this list store those data, I moreless understand how it works with the template for integers, floats and strings but I can not understand how it does for the 3 data types at the same time, I mean in a classA datatype together.
For example if data is integer with the template is like cresting an integer array, for floats it is like creating a float array, same for string, but what about classA datatype, does it create 3 arrays? one for integer, one for float and one for string? or it is a single array containing addresses to each of classA instances?
Please help, I want to understand and I can not ask the teacher because it was supposed he already explained.
Thanks
//-----------you can see here the array that was used l[100] in staticlist hearder file--------------
template <class T1>
class StaticList
{
private:
int cont;
T1 l[100];
template <class T1>
StaticList<T1>::StaticList()
{
cont = 0;
}
//-----------here you can see how the adt classA was defined in classA header file --------------
class ClassA
{
private:
int iInteger;
string sString;
float fFloat;
public:
ClassA();
ClassA(int, float, string);

//------------
ClassA::ClassA(int a, float b, string c)
{
iInteger= a;
fFloat= b;
sString= c;
}
//------------in the main an instance is created-------------
StaticList<ClassA> listClassA;

Jorgen Grahn

unread,
Mar 20, 2019, 8:00:28 PM3/20/19
to
On Wed, 2019-03-20, saulmartin...@gmail.com wrote:
> Hi,

...
> For example if data is integer with the template is like cresting an
> integer array, for floats it is like creating a float array, same
> for string, but what about classA datatype, does it create 3 arrays?
> one for integer, one for float and one for string? or it is a single
> array containing addresses to each of classA instances?

> template <class T1>
> class StaticList
> {
> private:
> int cont;
> T1 l[100];
...

Your problem seems (to me) to be about understanding objects and
arrays, not so much about templates and classes. The cases you
mentioned:

int list[100]; // T1 = int
float list[100]; // T1 = float
string list[100]; // T1 = string
ClassA list[100]; // T1 = ClassA

(I renamed the list from 'l'. Never call anything 'l' because it
looks a lot like '1'.)

All four cases are the same: there are 100 objects of some type, in a
single array. Arrays can store pretty much any kind of object. And
they don't just store the address of an object: if you say

list[42] = foo;

an object in the array will be overwritten with a copy of foo,
using the type's assignment operator. If it doesn't have an assignment
operator, it won't compile.

> Please help, I want to understand and I can not ask the teacher
> because it was supposed he already explained.

Thank you for explaining why you're asking.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Alf P. Steinbach

unread,
Mar 20, 2019, 8:01:38 PM3/20/19
to
On 20.03.2019 23:42, saulmartin...@gmail.com wrote:
> Hi,
> I just started learning c++, in my classroom the teacher wrote code of static list using class template, the list is based on an array, it works fine for what was tried, integers, floats, strings and as well for abstract data (classA), because he wrote also a class that has one integer, one float and one string so the class template list is also able to work with that abstract data type, everything was tried in the same console application and worked fine, my questions for abstract data type, how or where this list store those data, I moreless understand how it works with the template for integers, floats and strings but I can not understand how it does for the 3 data types at the same time, I mean in a classA datatype together.
> For example if data is integer with the template is like cresting an integer array, for floats it is like creating a float array, same for string, but what about classA datatype, does it create 3 arrays? one for integer, one for float and one for string? or it is a single array containing addresses to each of classA instances?

No, it's a single array of ClassA instances directly as array items.
Each ClassA item contains an int, a string and a float.

By the way, ClassA, as presented in the code below, is /not/ abstract.
Also, preferably use the default floating point datatype in C++, double.
E.g., the literal 3.14 is of type double.

If ClassA were abstract then one couldn't create an array of classA
instances.


> Please help, I want to understand and I can not ask the teacher because it was supposed he already explained.

You should be able to ask your teacher to clarify things.

I think in a case where the teacher refuses to explain or is generally
inaccessible, I would first consult with other students in the class.

If that happens often enough, also with other students, then the
students, as a body, may try to complain about the teacher.
Cheers!,

- Alf

saulmartin...@gmail.com

unread,
Mar 21, 2019, 12:40:23 AM3/21/19
to
On Wednesday, March 20, 2019 at 6:00:28 PM UTC-6, Jorgen Grahn wrote:
Thank you Jorgen, it is clear now the concept

saulmartin...@gmail.com

unread,
Mar 21, 2019, 12:42:58 AM3/21/19
to
On Wednesday, March 20, 2019 at 6:01:38 PM UTC-6, Alf P. Steinbach wrote:
Thank you Alf, it is very helpful your explanation!

Jorgen Grahn

unread,
Mar 21, 2019, 2:44:47 AM3/21/19
to
On Thu, 2019-03-21, Alf P. Steinbach wrote:
> On 20.03.2019 23:42, saulmartin...@gmail.com wrote:

>> I just started learning c++, in my classroom the teacher wrote code
>> of static list using class template, the list is based on an array,
>> it works fine for what was tried, integers, floats, strings and as
>> well for abstract data (classA)
...

> By the way, ClassA, as presented in the code below, is /not/ abstract.
> Also, preferably use the default floating point datatype in C++, double.
> E.g., the literal 3.14 is of type double.
>
> If ClassA were abstract then one couldn't create an array of classA
> instances.

I think the teacher may have meant abstract data type (ADT) which is
more or less the same thing as a well-designed C++ class.

Neil Cerutti

unread,
Mar 21, 2019, 11:56:07 AM3/21/19
to
On 2019-03-21, Stefan Ram <r...@zedat.fu-berlin.de> wrote:
> r...@zedat.fu-berlin.de (Stefan Ram) writes:
>>float f;
>
> I mean, using »float« in a beginners class is one of those
> symptoms, like »void main()« (or maybe »int main( void )«).
> It consolidates my original assessment of the lecturer.
>
> When one is using »float« as the default type for floating
> point numbers without a good reason, one just might be
> following the sound of the word (»float« reminds of »floating
> point«), but might not even be aware of »double« being the
> standard floating-point type in C++. (The type of »1.2« is
> »double«, float printf-arguments are promoted to »double«,
> and the smaller precision of float values might cause
> troubles.)

In the C++ introductory course I just completed examples and
sample code used float without comment. I always used double
instead, based on Stroustrup's explicit recommendation.

This semester we're doing x86-32, integrating with C++. The use
of float, which fits in a 32-bit register, is certainly easier
than double in this scenario, so maybe there was method to it.

--
Neil Cerutti
0 new messages