Concepts of classes and objects in C/C++

F

Frederick Gotham

Robbie Hatley posted:

Yikes!

Oh, wow. Deja vu so strong my head is litterally spinning.

I was walking to the street yesterday to a Circle K to buy milk,
when it occurred to me, "Perhaps Frederick Gotham put those errors
in that program so that the student would get in trouble with his
teacher when he handed it in as his own work? That would be pretty
sadistic; but then, cheating is a pretty horrible thing to do. I
hope he doesn't get miffed because I mentioned the fact that there
were errors in the program? But perhaps the lazy student will grab
the work and turn it in before he reads my post."

Honestly, I thought all those things!


Hehe! : )

It's not so much that I'm vehemently against cheating, but becoming
proficient at a programming language such as C++ is not something that can be
cheated at, so the OP is just putting a masochistic delay to their untimely
demise.

I've cheated at things myself in the past (mostly in school at subjects I
disdained, e.g. History), but if what I'm cheating at is anything worthwhile,
then I'll be weeded out.

I'm in college myself at the moment, and I see many fellow college-goers copy
each other's work and so forth... but I just let them get on with it because
they're only doing themselves a disservice in the long run -- which is
reflected in the drop-out rate!

If I ever have the virtue of lecturing computer programming, I'll sit down
individually with each student and say, "OK, write a program to convert
stones and pounds to kilograms", and I'll watch them do it. Might check them
for ear-pieces beforehand too. Like to see them cheat.

I'll keep my mouth shut about hidden monkey wrenches from now on.
:)


Good to see I'm not the only one who thinks this is a laugh!

(P.S. I would have hoped you knew there was something up when I posted code
which was below my usual standards!)
 
W

wkaras

Daniel said:
All that is fine if you assume that the class will always and forever,
only be implemented using two variables held in RAM representing a
cartesion coordinate system on this particular machine and in this
particular thread and no code will *ever* have to patch into objects of
the class to detect changes to one or both variables.

If you google for "Bjarne Stroustrup", you can find and read
his arguments as to why it's not a good idea to design
classes to allow for every conceivable future use of the
class.

I think it's part of the class interface that the points are
in a cartesian coord system. But your argument for always
making data member private because it allows for future
access "side effects" does have some merit.
Now all that may very well be true especially for such a tiny little
class, however the OP's *requirement* was for the class to have "two
private data members x and y of type float". Personally, I was not in
the habit of leaving my homework requirements unfulfilled and I see no
reason to suggest others do so.

In these dark times, it's good to see that some people still
follow strict ethical standards when helping others to cheat on their
homework.
 
D

Daniel T.

In these dark times, it's good to see that some people still
follow strict ethical standards when helping others to cheat on their
homework.

Helping others cheat? You might want to look at the OP and my response
again... I in no way helped anyone to cheat.
 
N

Noah Roberts

Daniel T. wrote:

.... but so obtuse/obfuscated that the OP's teacher
will know at a glance that there is no way that this student of his
created it.

I don't expect most instructors to have it in them to look to hard and
many don't know the language enough (my experience anyway). Besides,
beginner code is regularly obtuse and unreadable having major logic
leaps that are difficult to make sense of...happens because many times
they just keep changing things until it does what the assigment said
without really knowing why. Many teachers might take points for
unreadability but if it works then it passes.

I don't think your plan would work.
 
D

Daniel T.

"Noah Roberts said:
Daniel T. wrote:

... but so obtuse/obfuscated that the OP's teacher

I don't expect most instructors to have it in them to look to hard and
many don't know the language enough (my experience anyway). Besides,
beginner code is regularly obtuse and unreadable having major logic
leaps that are difficult to make sense of...happens because many times
they just keep changing things until it does what the assigment said
without really knowing why. Many teachers might take points for
unreadability but if it works then it passes.

I don't think your plan would work.

You might be right, but I think if the teacher bothers to look at the
code and sees for example, extensive use of features not covered yet (or
even features he doesn't know) or extensive use of boolean operators, he
should be able to tell the difference. As an example, a post from
alt.comp.learn.c-c++ from February:

Dietmar said:
The others who think that you should do your homework assignment
alone are just spoilsports! The obvious solution to the above
problem is rather simple:
  #include <iostream>
  #include <iterator>
  #include <algorithm>
  struct __ {
    enum { _0 = 0x636363 };
    __(): _() {}
    void operator()(unsigned char _1) {
      _ = ((_ = (_ & 0xffff) << 8 | _1) == _0 || _ == _0 + 0x100)
        && std::cout << (_0 < _)? 0: _;
    }
    unsigned long _;
  } _;
  int main()
  {
    std::for_each(std::istreambuf_iterator<char>(std::cin),
                  std::istreambuf_iterator<char>(), _);
    std::cout << "\n";
  }
Of course, you should understand the program before handing it in
because it would be a really stupid idea not to do your homework!

Several of us also posted answers that made extensive use of #define,
but the above was by far the most ingenious, IMHO.
 
N

Noah Roberts

Daniel said:
You might be right, but I think if the teacher bothers to look at the
code and sees for example, extensive use of features not covered yet (or
even features he doesn't know) or extensive use of boolean operators, he
should be able to tell the difference. As an example, a post from
alt.comp.learn.c-c++ from February:

Several of us also posted answers that made extensive use of #define,
but the above was by far the most ingenious, IMHO.

Well, if the teacher knows the language, and there is no guarantee
there, he would mark this code as erroneous. Notice that the answer is
still incorrect as the program is ill-formed.
 
D

Daniel T.

"Noah Roberts said:
Well, if the teacher knows the language, and there is no guarantee
there, he would mark this code as erroneous. Notice that the answer is
still incorrect as the program is ill-formed.

Why do you think it is ill-formed? You might want to look at it again.
 
N

Noah Roberts

Daniel said:
Why do you think it is ill-formed? You might want to look at it again.

Struct definition starts with two underscores and is global; that name
is reserved. Struct contains an enum name that starts with an
underscore followed by a capitol letter; that name is also reserved.
The variable name "_" is also in the global scope and starts with an
underscore; that name is reserved. The behavior of the program is
undefined.
 
D

Daniel T.

"Noah Roberts said:
Daniel said:
Struct definition starts with two underscores and is global; that name
is reserved.

See [*] below.
Struct contains an enum name that starts with an
underscore followed by a capitol letter; that name is also reserved.

Actually it contains nothing of the sort. It contains an underscore
followed by a digit. _0
The variable name "_" is also in the global scope and starts with an
underscore; that name is reserved. The behavior of the program is
undefined.

[*] I am finding conflicting information about these two points. Some
sources say _ and __ are reserved, others say that the underscores must
be followed by something (upper or lower-case letter) to be reserved.
Can I get a quote from the standard?
 
N

Noah Roberts

Daniel said:
Noah Roberts said:
Daniel said:
Struct definition starts with two underscores and is global; that name
is reserved.

See [*] below.
Struct contains an enum name that starts with an
underscore followed by a capitol letter; that name is also reserved.

Actually it contains nothing of the sort. It contains an underscore
followed by a digit. _0

Then it's ok.
The variable name "_" is also in the global scope and starts with an
underscore; that name is reserved. The behavior of the program is
undefined.

[*] I am finding conflicting information about these two points. Some
sources say _ and __ are reserved, others say that the underscores must
be followed by something (upper or lower-case letter) to be reserved.
Can I get a quote from the standard?

It depends on the scope of the name. In global scope ANY name that
begins with an underscore is reserved. Other scopes anything that
begins with __ or with _ and an upper case letter. Your quote:

17.4.3.1.2 Global names

Certain sets of names and function signatures are always reserved to
the implementation:

-- Each name that contains a double underscore (__) or begins with an
underscore followed by an upper case letter (2.11) is reserved to the
implementation for any use.

-- Each name that begins with an underscore is reserved to the
implementation for use as a name in the global namespace. 165)

165) Such names are also reserved in namespace ::std (17.4.3.1)
 
N

Noah Roberts

Noah said:
-- Each name that contains a double underscore (__) or begins with an
underscore followed by an upper case letter (2.11) is reserved to the
implementation for any use.

Interestingly I believe this is an error in the standard (referring to
the reference to 2.11). 2.11 is not about characters at all but about
keywords and makes no mention of upper/lower case. 2.10 contains text
mentioning significance of case. 2.13.2 is about character literals
but also does not mention case. There is no 17.2.11. My bet is that
is supposed to be 2.10 referring to:

"An identifier is an arbitrarily long sequence of letters and digits.
Each universal-character-name in an identifier shall designate a
character whose encoding in ISO 10646 falls into one of the ranges
specified in Annex E. Upper- and lower-case letters are different.
All characters are significant..."
 
D

Daniel T.

"Noah Roberts said:
It depends on the scope of the name. In global scope ANY name that
begins with an underscore is reserved. Other scopes anything that
begins with __ or with _ and an upper case letter. Your quote:

17.4.3.1.2 Global names

Certain sets of names and function signatures are always reserved to
the implementation:

-- Each name that contains a double underscore (__) or begins with an
underscore followed by an upper case letter (2.11) is reserved to the
implementation for any use.

-- Each name that begins with an underscore is reserved to the
implementation for use as a name in the global namespace. 165)

165) Such names are also reserved in namespace ::std (17.4.3.1)

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top