Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Test your skills (2005)
Index -> General Programming
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9, 10  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
[Gandalf]




PostPosted: 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:

code:
int c = 0;


and

code:
int c(0);


or

code:
c++;


and

code:
++c;



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* Smile
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: 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




PostPosted: 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:

code:
int c = 0;


and

code:
int c(0);


or

code:
c++;


and

code:
++c;



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]




PostPosted: 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? Confused
wtd




PostPosted: 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? Confused


Public by default, yes, but that can be modified, just as in a class. Smile
MysticVegeta




PostPosted: Wed Jul 20, 2005 8:37 am   Post subject: (No subject)

I think
code:
int c;
declares an integer c
code:
int c[0];
declares an array
code:
c++
increments the value of c by 1
code:
int b = ++c;
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 Laughing lol
[Gandalf]




PostPosted: Wed Jul 20, 2005 11:51 am   Post subject: (No subject)

MysticVegeta wrote:
I think
code:
int c;
declares an integer c
code:
int c[0];
declares an array
code:
c++
increments the value of c by 1
code:
int b = ++c;
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 Wink.

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




PostPosted: Thu Jul 21, 2005 8:25 pm   Post subject: (No subject)

Some awesome C++ code:

code:
cout << 5[2 + "abcdefghi"] << endl;


Very Happy

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
Sponsor
sponsor
[Gandalf]




PostPosted: 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.
code:
int c(0);

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:
code:
int c;
c(0);

while you can do something like this:
code:
int c;
c = 0;


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? Laughing
wtd




PostPosted: 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




PostPosted: Wed Sep 21, 2005 11:46 pm   Post subject: Alex's Opinion

I think it's about time for a Turing question. Twisted Evil
wtd




PostPosted: 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. Twisted Evil


Turing's not interesting enough to do anything tricky. Razz
beard0




PostPosted: 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




PostPosted: 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




PostPosted: 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]").
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 5 of 10  [ 147 Posts ]
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9, 10  Next
Jump to:   


Style:  
Search: