Thursday, March 21, 2019

Digest for comp.lang.c++@googlegroups.com - 11 updates in 3 topics

Jorgen Grahn <grahn+nntp@snipabacken.se>: Mar 21 12:00AM

> Hi,
 
...
> 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" <alf.p.steinbach+usenet@gmail.com>: Mar 21 01:01AM +0100

> 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.
 
 
> }
> //------------in the main an instance is created-------------
> StaticList<ClassA> listClassA;
 
Cheers!,
 
- Alf
saulmartinezespinosa1@gmail.com: Mar 20 09:40PM -0700

On Wednesday, March 20, 2019 at 6:00:28 PM UTC-6, Jorgen Grahn wrote:
 
> --
> // Jorgen Grahn <grahn@ Oo o. . .
> \X/ snipabacken.se> O o .
 
Thank you Jorgen, it is clear now the concept
saulmartinezespinosa1@gmail.com: Mar 20 09:42PM -0700

On Wednesday, March 20, 2019 at 6:01:38 PM UTC-6, Alf P. Steinbach wrote:
> > StaticList<ClassA> listClassA;
 
> Cheers!,
 
> - Alf
 
Thank you Alf, it is very helpful your explanation!
Jorgen Grahn <grahn+nntp@snipabacken.se>: Mar 21 06:44AM

On Thu, 2019-03-21, Alf P. Steinbach wrote:
>> 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)
...
 
> 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.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Neil Cerutti <neilc@norwich.edu>: Mar 21 03:55PM

> »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
Chris Vine <chris@cvine--nospam--.freeserve.co.uk>: Mar 20 11:46PM

On 20 Mar 2019 22:38:57 GMT
> type system the most helpful of them, it's hard to swallow that it's
> classified as "weak". Even though I'm sure it /is/ according to the
> definition.
 
As I think you hint at, there seems to me to be a continuum and a trade
off. The guarantee of a strong statically typed language is that any
object passed to a function or other program interface which purports
to have (or must be inferred to have) a given type either (a) is of
that type, or (b) the program will fail to compile.
 
C and C++ do not, and in my view should not, offer that guarantee.
void* surpresses all static type information that the compiler
possesses, and sometimes you want that. More often, you want to be
able to cast to char* or unsigned char* to get at bytes and take the hit
of losing static type information, which in many usages doesn't matter.
 
On the other hand C++ does offer reasonable type guarantees if you
avoid casts. Using brace initialization also prevents implicit
narrowing conversions: 'int i{1.0}' won't compile. And as I have
mentioned elsewhere, the C++ class system itself is well typed.
 
If I were inventing a new language I would probably argue that unsafe
operations should be allowed for efficiency's sake, but that it should
only be possible to do them knowingly rather than accidentally, by
being explicitly elected for at the call site. I think that is what
rust does (but I do not have a firm handle on rust so I may be wrong
about that). For C++, compatibility with C is I think more important.
"Öö Tiib" <ootiib@hot.ee>: Mar 21 05:39AM -0700

On Wednesday, 20 March 2019 19:59:24 UTC+2, Chris Vine wrote:
> fine. "Statically typed with loopholes" would also do. For C++ you
> could say "statically and dynamically typed with loopholes", because
> C++ has RTTI.
 
Large part of C++ specification of most features of C++ is
explicitly telling us what all is undefined about that feature. So it is
only partially in control about most things (including type system).
That encourages us to set the rules of how strong, weak, dynamic
or static, safe or unsafe everything in it is ... ourselves.
ram@zedat.fu-berlin.de (Stefan Ram): Mar 21 12:56AM

>I just started learning c++, in my classroom the teacher wrote code of stat=
>ic list using class template, the list is based on an array,
 
In the first lessons, I teach simple expressions,
like »7/2 + 3.0«, explaining the values and types of the
operands. Then, there are many more lessons and many
exercises related to fundamental topics before
I get to arrays and classes.
 
>Please help, I want to understand and I can not ask the
>teacher because it was supposed he already explained.
 
I always encourage participants of my C++ courses to ask
questions about anything that already has been explained.
(What I would not like is them asking questions about stuff
that I only intend to explain in the /future/.)
 
You write long paragraphs with long sentences, which makes
your text hard to read. I assume that you write in this
style even in your native language. You need to improve
your writing.
 
Well, so if your teacher goes on so fast that you are
exposed to class definitions when you just have started
to learn the language and then you do not dare to ask
questions, none of this is good.
ram@zedat.fu-berlin.de (Stefan Ram): Mar 21 01:17AM

> 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?
..
> int iInteger;
> string sString;
> float fFloat;
 
You ask:
 
|what about classA datatype
 
but then you give no definition for »classA« with a
lower-case »c«.
 
In C++, we usually say "the class ClassA", not "ClassA
datatype" (although it is a type).
 
Names like »ClassA« and »iInteger« also are a little weird.
I prefer:
 
class a
{ int i;
::std::string s;
float f;
...
 
The /class/ does not create objects at all. Only when an
/instance/ of the class is created, will an object (with
subobjects and its memory requirements) be created.
 
For example, the execution of the declaration-statement
»a a0;« will create an object that will then require
object memory.
ram@zedat.fu-berlin.de (Stefan Ram): Mar 21 12:29PM

>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.)
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: