11 Replies - 17984 Views - Last Post: 19 June 2015 - 11:40 AM Rate Topic: -----

#1 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1026
  • View blog
  • Posts: 4,280
  • Joined: 14-February 08

[Week 6] Game Programming Sample Interview Question

Post icon  Posted 20 March 2011 - 03:39 AM

Hey :) Time for week 6 of the interview questions.

Question 1

a.) Explain the difference between a copy constructor and an overloaded assignment operator?

b.) What happens if you declare the only constructor in a class as private? Give an example of why you might want to do this.

Question 2

What was the last game you played and what did you think of it?

Please use spoiler tags for answers :)

This post has been edited by stayscrisp: 21 March 2011 - 06:39 AM


Is This A Good Question/Topic? 1
  • +

Replies To: [Week 6] Game Programming Sample Interview Question

#2 sh1n3  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 164
  • Joined: 22-April 10

Re: [Week 6] Game Programming Sample Interview Question

Posted 20 March 2011 - 07:30 PM

Question 1:

Spoiler


Is this right? I think the first one is wrong.

This post has been edited by stayscrisp: 21 March 2011 - 06:39 AM
Reason for edit:: Added spoiler tags :)

Was This Post Helpful? 0
  • +
  • -

#3 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: [Week 6] Game Programming Sample Interview Question

Posted 21 March 2011 - 06:41 AM

You can also use private constructors to let only specific classes or functions to instantiate the class (by declaring they as friend).
Was This Post Helpful? 0
  • +
  • -

#4 hype261  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 29
  • Joined: 02-March 11

Re: [Week 6] Game Programming Sample Interview Question

Posted 21 March 2011 - 07:05 AM

Here are my answers.

Spoiler

This post has been edited by hype261: 21 March 2011 - 07:08 AM

Was This Post Helpful? 0
  • +
  • -

#5 shintetsu_80  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 108
  • Joined: 01-July 08

Re: [Week 6] Game Programming Sample Interview Question

Posted 21 March 2011 - 07:21 AM

Question 1:
Spoiler


Question 2:
Spoiler

This post has been edited by shintetsu_80: 21 March 2011 - 07:23 AM

Was This Post Helpful? 0
  • +
  • -

#6 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 3062
  • View blog
  • Posts: 16,459
  • Joined: 24-September 08

Re: [Week 6] Game Programming Sample Interview Question

Posted 22 March 2011 - 01:31 PM

How fun.

Spoiler

This post has been edited by NeoTifa: 22 March 2011 - 01:34 PM

Was This Post Helpful? 1
  • +
  • -

#7 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1026
  • View blog
  • Posts: 4,280
  • Joined: 14-February 08

Re: [Week 6] Game Programming Sample Interview Question

Posted 29 March 2011 - 05:28 PM

Wkikipedia has a great example of why you sometimes need a user defined copy constructor, what happens when you run this code?

#include <iostream>
 
class Array {
public:
    int size;
    int* data;
 
    explicit Array (int size)
        : size (size), data (new int[size]) {}
 
    ~Array() 
    {
        delete[] data;
    }
};
 
int main()
{
    Array first (20);
    first.data[0] = 25;
 
    {
        Array copy = first;
 
        std::cout << first.data[0] << " " << copy.data[0] << std::endl;
 
    }    // (1)
 
    first.data[0] = 10;    // (2)
 
    return 0;
}



Was This Post Helpful? 0
  • +
  • -

#8 hype261  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 29
  • Joined: 02-March 11

Re: [Week 6] Game Programming Sample Interview Question

Posted 30 March 2011 - 06:04 AM

View Poststayscrisp, on 29 March 2011 - 05:28 PM, said:

Wkikipedia has a great example of why you sometimes need a user defined copy constructor, what happens when you run this code?

#include <iostream>
 
class Array {
public:
    int size;
    int* data;
 
    explicit Array (int size)
        : size (size), data (new int[size]) {}
 
    ~Array() 
    {
        delete[] data;
    }
};
 
int main()
{
    Array first (20);
    first.data[0] = 25;
 
    {
        Array copy = first;
 
        std::cout << first.data[0] << " " << copy.data[0] << std::endl;
 
    }    // (1)
 
    first.data[0] = 10;    // (2)
 
    return 0;
}





(1) Array copy is going to go out of scope and call its destructor. Since copy used the copy constructor to build itself and the compiler provided the copy constructor a shallow copy of data was perform. The destructor of copy will delete data, but sadly Array first will still be pointing to the same data.

(2) This is a dangling pointer issue. The program is assigning a value to memory which it doesn't own anymore. The program could work correctly, could crash, could cause OS to crash, or could cause Computer to grow legs and light itself on fire. Anything is possible.
Was This Post Helpful? 0
  • +
  • -

#9 Peabsesora  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-January 11

Re: [Week 6] Game Programming Sample Interview Question

Posted 03 April 2011 - 04:43 PM

You have missed the most important.
Was This Post Helpful? 0
  • +
  • -

#10 hype261  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 29
  • Joined: 02-March 11

Re: [Week 6] Game Programming Sample Interview Question

Posted 04 April 2011 - 06:15 AM

View PostPeabsesora, on 03 April 2011 - 04:43 PM, said:

You have missed the most important.


Are you talking about the fact that the program is going to crash when the second Array goes out of scope?? I didn't miss that. I thought we were only supposed to comment on the numbered items. If that wasn't what you meant then please inform me what I missed.
Was This Post Helpful? 0
  • +
  • -

#11 Ericool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-June 15

Re: [Week 6] Game Programming Sample Interview Question

Posted 19 June 2015 - 11:35 AM

View Poststayscrisp, on 20 March 2011 - 03:39 AM, said:

Hey :)/> Time for week 6 of the interview questions.

Question 1

a.) Explain the difference between a copy constructor and an overloaded assignment operator?

b.) What happens if you declare the only constructor in a class as private? Give an example of why you might want to do this.

Question 2

What was the last game you played and what did you think of it?

Please use spoiler tags for answers :)/>


question 1 : copy constructor is simply copying data , but operator=() might take the address as a reference , so it is a clone .
question 2 : you decide no good answer , but should be a recent game ..
Was This Post Helpful? 0
  • +
  • -

#12 Ericool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-June 15

Re: [Week 6] Game Programming Sample Interview Question

Posted 19 June 2015 - 11:40 AM

View PostEricool, on 19 June 2015 - 11:35 AM, said:

View Poststayscrisp, on 20 March 2011 - 03:39 AM, said:

Hey :)/>/> Time for week 6 of the interview questions.

Question 1

a.) Explain the difference between a copy constructor and an overloaded assignment operator?

b.) What happens if you declare the only constructor in a class as private? Give an example of why you might want to do this.

Question 2

What was the last game you played and what did you think of it?

Please use spoiler tags for answers :)/>/>


question 1 : copy constructor is simply copying data , but operator=() might take the address as a reference , so it is a clone .
question 2 : you decide no good answer , but should be a recent game ..


and for question 1 b : something we do in optimization is memory management . For instance , we want every instance to be on the heap (pointer) so the user can just create an instance on the stack by putting the constructor as private :

   class EntityNode{
     private : 
         EntityNode(void){}
     public : 
         static EntityNode* create(void){return new EntityNode();}
   };
 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1