Test your skills (2005)
Author |
Message |
[Gandalf]
|
Posted: Fri Jul 01, 2005 6:57 pm Post subject: (No subject) |
|
|
I think that since that post seems to have scared people off, and nobody seems to be able to answer them all, we should start picking it apart piece by piece. So, I will start with my minor knowledge of c++.
wtd wrote: Do you understand the difference between:
and
or
and
Do you know the difference between a struct and a class?
Well, for the first two, the first one uses assignment, and the second one uses initialization. I'm not sure of the differences of what they do, but they are two different operations, and they are used in different contexts usually.
For the second two, the first is a postfix increment, and the second is a prefix increment. They both increment the value by one, but the postfix is not as good since it still outputs the previous value, not the newly incremented one.
For the last question, a struct makes all member functions/variables public, while classes allow you to declare what is private/public/or protected. This prevents bugs because you can control when and where the member of the class is manipulated.
*ahh, well, I tried* |
|
|
|
|
|
Sponsor Sponsor
|
|
|
rizzix
|
Posted: Fri Jul 01, 2005 7:01 pm Post subject: (No subject) |
|
|
ehm... you are wrong the first and second are the same.. they are both initialisations... they are just two different notations for the same thing.. |
|
|
|
|
|
wtd
|
Posted: Fri Jul 01, 2005 7:05 pm Post subject: (No subject) |
|
|
[Gandalf] wrote: I think that since that post seems to have scared people off, and nobody seems to be able to answer them all, we should start picking it apart piece by piece. So, I will start with my minor knowledge of c++.
wtd wrote: Do you understand the difference between:
and
or
and
Do you know the difference between a struct and a class?
Well, for the first two, the first one uses assignment, and the second one uses initialization. I'm not sure of the differences of what they do, but they are two different operations, and they are used in different contexts usually.
For the second two, the first is a postfix increment, and the second is a prefix increment. They both increment the value by one, but the postfix is not as good since it still outputs the previous value, not the newly incremented one.
For the last question, a struct makes all member functions/variables public, while classes allow you to declare what is private/public/or protected. This prevents bugs because you can control when and where the member of the class is manipulated.
Close, but not quite. Postfix and prefix incrementing are both equally "good", which is to say they both have value when used correctly.
The postfix version will both increment the variable and return its previous value. This is a trickier operation, and generally requires a bit (but that may be a very small bit) more overhead. If you're incrementing in a void context, using the prefix version may be slightly faster.
As for structs, they still allow for access specifiers.
code: | struct foo
{
private:
int a;
public:
foo(int init_a) : a(init_a) { }
}; |
Is perfectly legal. |
|
|
|
|
|
[Gandalf]
|
Posted: Sat Jul 02, 2005 8:02 pm Post subject: (No subject) |
|
|
Ok, I don't get it, you said it yourself:
wtd wrote: a struct (class with all public members)
Is that not what I said? |
|
|
|
|
|
wtd
|
Posted: Sat Jul 02, 2005 9:25 pm Post subject: (No subject) |
|
|
[Gandalf] wrote: Ok, I don't get it, you said it yourself:
wtd wrote: a struct (class with all public members)
Is that not what I said?
Public by default, yes, but that can be modified, just as in a class. |
|
|
|
|
|
MysticVegeta
|
Posted: Wed Jul 20, 2005 8:37 am Post subject: (No subject) |
|
|
I think
declares an integer c
declares an array
increments the value of c by 1
makes the value b = (c+1) but the value of c stays same
I dont know, i might be wrong, i read the C++ for dummies a long time ago lol |
|
|
|
|
|
[Gandalf]
|
Posted: Wed Jul 20, 2005 11:51 am Post subject: (No subject) |
|
|
MysticVegeta wrote: I think
declares an integer c
declares an array
increments the value of c by 1
makes the value b = (c+1) but the value of c stays same
Well, it was int c(0); not int c[0];. Using the () brackets to initialize is the "C++ syle" while using the = would be another method of initialization used in C, Java, and most others (it's still used in C++ too). Also, I think he explained post/pre increments already .
Ok, so how about this:
In a struct all members are public by default, while in a class they are private by default. By default meaning, if you do not put any access limited (or whatever). ? |
|
|
|
|
|
Martin
|
Posted: Thu Jul 21, 2005 8:25 pm Post subject: (No subject) |
|
|
Some awesome C++ code:
code: | cout << 5[2 + "abcdefghi"] << endl; |
And of course, some of the coolest C code that I've seen in a while. Ported from SPARC by a Mark Sherry.
code: | #include <stdio.h>
#include <sys/types.h>
int globaldummy;
void
entry (void (*blabber) ())
{
blabber ();
}
void
quux (void)
{
globaldummy++;
}
void
bar (void)
{
printf ("first instruction\n");
globaldummy++;
printf ("after the entry point\n");
}
main ()
{
void (*crigglesnash) (void);
size_t fnsize;
fnsize = bar - quux - 5;
for (crigglesnash = bar; memcmp (crigglesnash, quux + fnsize, fnsize);
crigglesnash += fnsize);
entry (crigglesnash);
}
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
[Gandalf]
|
Posted: Sat Jul 23, 2005 7:28 pm Post subject: (No subject) |
|
|
Interesting code, I'll try it out.
Alright, an improvement over my previous explanations.
This style of of assignment can only be used when the variable is being declared. So the () method is only usable when initializing. You cannot, for example, do something like this:
while you can do something like this:
Quote: Do you know when and why to define a destructor?
A constructor can 'allocate' memory, or for example open a file, and a destructor is needed to automatically close that file, and deallocate the resources without creating a new function.
How about some bits? |
|
|
|
|
|
wtd
|
Posted: Sat Jul 23, 2005 8:07 pm Post subject: (No subject) |
|
|
[Gandalf] wrote: Quote: Do you know when and why to define a destructor?
A constructor can 'allocate' memory, or for example open a file, and a destructor is needed to automatically close that file, and deallocate the resources without creating a new function.
Sure. +50 bits.
If we continue to develop the following class, would it need a destructor? If so, write that destructor.
c++: | class Student
{
private:
std::vector<double> *grades;
public:
const std::string first_name, last_name;
}; |
|
|
|
|
|
|
Mr. T
|
Posted: Wed Sep 21, 2005 11:46 pm Post subject: Alex's Opinion |
|
|
I think it's about time for a Turing question. |
|
|
|
|
|
wtd
|
Posted: Wed Sep 21, 2005 11:50 pm Post subject: Re: Alex's Opinion |
|
|
Pwned wrote: I think it's about time for a Turing question.
Turing's not interesting enough to do anything tricky. |
|
|
|
|
|
beard0
|
Posted: Thu Sep 22, 2005 7:03 am Post subject: (No subject) |
|
|
I think it's about time for another question, period..... please?
(And Turing is interesting enough... grr) |
|
|
|
|
|
wtd
|
Posted: Thu Sep 22, 2005 11:41 am Post subject: (No subject) |
|
|
Well, it may be "interesting" by some definition of that word, but these tests are meant to test details, and things that are often overlooked.
I either don't know Turing well enough to myself test others on such things, or haven't seen an opportunity to ask such questions.
With that said, the next question:
Ok, Pythonistas, this one's for you again.
Given the class Foo:
Python: | class Foo(object):
def __init__(self):
self.bar = 42 |
We can easily add attributes to an instance of Foo at run-time.
Python: | baz = Foo()
baz.ninja = 27.3
print baz.ninja |
That's not a problem.
Create a subclass Wooble of Foo which makes all added attributes strings. Make sure it doesn't change the type of "bar".
Do this in one function in the Wooble class. |
|
|
|
|
|
wtd
|
Posted: Thu Sep 22, 2005 12:06 pm Post subject: (No subject) |
|
|
A Haskell question:
Let's have a simple factorial program.
code: | import IO
main =
do
putStr "Enter a number: "
hFlush stdout
inputLine <- hGetLine stdin
let inputNumber = read input
let factorial = product [1..number]
let strFactorial = show factorial
putStrLn strFactorial |
Now... rewrite main as a single line of code with no explicit passing of state (such as: "let inputNumber = read input" and "let factorial = product [1..number]"). |
|
|
|
|
|
|
|