Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   C++ Questions (http://www.go4expert.com/forums/cpp-questions-t25359/)

zackary13 28Mar2011 21:20

C++ Questions
 
Please answer it will be greatly appreciated and number the answer with the number of the question, thanks.
Questions:
1) Let's look at the following code:


int (**val1)[2];


int *(*val2)[2];


int (**const val3)() = 0;


int *(*val4)();




Explain what does each value mean.


2) What's wrong with the following code?
int scrollXOffSet;
if( isScroll ) {
scrollXOffSet = 2;
} else {
scrollXOffSet = -2;
}

Assuming isScroll returning a true value when there is a request to offset for an x amount in scrolling.

So, what's wrong with the code snippet above? If you can't find anything wrong, then why? If you can find something wrong, what is it?

3) For the following code:


int *val1 = new int;


int *val2 = new int[6];


delete [] val1;


delete val2;


val1 = new int[2];


delete val1;


Can you spot the delete error in the above code? If so, how would you correct them?


4) Let's say we have the following code:

Code:

/**************************************************
*
* Name: CMain.cpp
*
* Description:
* Linker: no dependencies needed
*
* Change history:
*
* Author:
*
**************************************************/
 
//-----------------------------------------------------------------------------
// Specific Includes
//-----------------------------------------------------------------------------
#include <iostream>
 
 
//-----------------------------------------------------------------------------
// Function Declaration
//-----------------------------------------------------------------------------
int checkDigit(const int&, const int&);
 
 
//-----------------------------------------------------------------------------
// Namespaces define
//-----------------------------------------------------------------------------
using namespace std;
 
//-----------------------------------------------------------------------------
// Name : main()
// Desc : main function
//-----------------------------------------------------------------------------
int main()
{
 
              //stuff here
 
              return(0);
 
}
 
 
//-----------------------------------------------------------------------------
// Name : checkDigit
// Desc : computing checkDigit
//-----------------------------------------------------------------------------
int checkDigit(const int& numb, const int& index) {
 
              //implementation stuff here
 
              return result;
}

If you look at the Function Declaration, it has

Code:

//-----------------------------------------------------------------------------
// Function Declaration
//-----------------------------------------------------------------------------
int checkDigit(const int&, const int&);

and at the bottom, we have the implementation

Code:

//-----------------------------------------------------------------------------
// Name : checkDigit
// Desc : computing checkDigit
//-----------------------------------------------------------------------------
int checkDigit(const int& numb, const int& index) {
 
              //implementation stuff here
 
              return result;
}

If you look at both of them, there are no function parameters specified in the function declaration
int checkDigit(const int&, const int&);

and there is in the implementation part
int checkDigit(const int& numb, const int& index)

Is this the way we do it? Is it safe or any potential error might occur later? or should we just define in the first place? Like:

Code:

//-----------------------------------------------------------------------------
// Function Declaration
//-----------------------------------------------------------------------------
int checkDigit(const int& numb, const int& index);

Code:

//-----------------------------------------------------------------------------
// Name : checkDigit
// Desc : computing checkDigit
//-----------------------------------------------------------------------------
int checkDigit(const int& numb, const int& index) {
 
              //implementation stuff here
 
              return result;
}

If it is not safe, how would you fix the code above?

5) So, we have the following:

int * p1;

const int * p2;

int * const p3;

const int * const p4;

mutable int *p6;


Explain what each does.
Which one can you modify the pointer?
which one can you modify the data?
which one you can modify for both?
which you can't do anything?

6) So, if we have,

i++;

it translate to
i = i +1;

1) If we have *++p and *(++)p, what does it translate to? (translate to means how does it look in address processing, and how does it look when dereferencing it?)
2) What are about *p++ and *p(++)? what does it translate to? (translate to means how does it look in address processing, and how does it look when dereferencing it?)

Which one of the above applies for the following?
A) address processing applies before dereferencing, is it *++p or *(++)p or *p++ or *p(++)?
B) dereferencing applies before address processing, is it *++p or *(++)p or *p++ or *p(++)?

7) What's wrong with this line?
const int FOURTY = 40;

Discuss the do's and don't.

8) Let's say we have the following structures:

struct _myStruct1
{
char a;
char b;
char c;
} Struct1;

and you get the total memory size of this Struct1 by calling the function sizeof()
For example:
sizeof(Struct1);
The answer is 3 bytes;

Now, you have another structure
struct _myStruct2
{
int x;
int a;
} Struct2;

and you get the total memory size of this Struct2 by calling the function sizeof()
For example:
sizeof(Struct2);
The answer is 8 bytes;

Why? Because each data gives us:

char - 1 byte
bool - 1 byte
int - 4 bytes
float - 4 bytes
pointer - 4 p p
double - 8 bytes

Now, the question is how many bytes do I get for the following? and why?
struct _myStruct3
{
int x;
char a;
} Struct3;

how many bytes does Struct3 have ? why? how can you optimize this code by reducing more bytes?

9) Let's say somewhere in the class implementation file, you see the portion of the code
int CAlgoirthm::AABBCal()
{
.......
.......
calcBox = 1;
distanceBox = 2;
......
......
{

By looking just at those two variables, how can you tell which variable belongs to class data members and which belongs to local?

zackary13 28Mar2011 21:22

Re: C++ Questions
 
10) Let's see how many errors can you find in this class below? How can you fix it if there is an error?

Code:

class Complex
{
public:
Complex: (double real, double imaginary = 0) : _real(real), _imaginary(imaginary) { }
 
void operator+( Complex other)
{
_real = _real + other._real;
_imaginary = _imaginary + other._imaginary;
}
 
void operator <<( ostream os)
{
os << "(" << _real << "," << _imaginary << ")";
}
 
Complex operator++()
{
++_real;
return *this;
}
 
Complex operator++(int)
{
Complex temp = *this;
++_real;
return temp;
}
 
private:
double _real._imaginary;
 
};

You can copy the class and paste to your compiler for better view.

So, let's see how many errors you guys can find and how to fix each error.

11) In a class, when do you explicitly allow or not allow copying objects?
CEnemy( const CEnemy& );

12) What's the different between this '#include' vs 'forward declaration'?
#include "differentclass.h"

class otherClass {
...
};

vs this
class subclass;

class newClass {
...
};

When should you use one or the other?

zackary13 28Mar2011 21:24

Re: C++ Questions
 
13) There are several casting operators specific to the C++ language. These operators are intended to remove some of the ambiguity and danger inherent in old style C language casts. These operators are:


see http:
//msdn.microsoft.com/en-us/library/5f6c9f8h(v=VS.80).aspx


Let's say we have the following:




int x;


char a;




first normal cast




x = (int)a;




casting with C++ casts,




x = ????(a)




Replace ????? with a type of casting - what type of castings out of those C++ casts would you choose for the example above? (x = ????(a)) Explain your answer why you pick that cast type.

14) Let's say, we want to use namespace in our class like (eg: using namespace std;)


For the following code:


MyClass.h file has:


Code:


 
#ifndef _MYCLASS_H_
 
 
#define_MYCLASS_H_
 
 
using namespace std;
 
 
include<iostream>
 
 
class MyClass
{
 
 
//class stuff here
 
 
};
 
 
#endif


what's wrong with code above? anything that you can find? how would you fix it?

15) Let's say we have this code snippet

Code:

int TrackID(int id1, int id2);
......
.....
int trackID = 0;
,....
int TotalID = TrackID(++trackID, ++trackID);
...


if the TrackID function purpose is to add the two id together to give the total, what is the TotalID above after the TrackID(++trackID, ++trackID) is called?

and what is to the TotalID after this call below:

TotalID = TrackID(++trackID, trackID++);

Explain your both of your answer.

16) Consider the following class:

Code:

class Player {
private:
string email_, firstName_, lastName_;
 
public:
Player( const char* firstName, const char* lastName )
: firstName_(firstName), lastName_(lastName)
, email_(firstName_ + "." + lastName_ + "@acme.com")
{
Player::loadSkills();
}
 
virtual void loadSkills() = 0;
virtual ~Player();
 
};

What's wrong with the code? See if you can find the errors. If you do, why should you fix it that way? In other word, what's do and don't.
Assuming loadSkills() defined somewhere in the sub class/inherited class.

17) Let's look at the copy assignment operator in one of the classes

Code:


CEnemy& CEnemy::operator=(CEnemy gEnemy)
{
if (this != &gEnemy)
{
this->~CEnemy();
new (this) CEnemy(gEnemy);
}
return *this;
}

Observer that function carefully, what's wrong with the code? is it safe code (will it make memory leak?) ? why or why not?

See if you can spot error if there is any.

18) Take a look at the below partial code of the class:

Code:

class Player
{
 
public:
//constructor stuff here
//destructor stuff here
//other methods here ...
 
int GetPlayerID() {
 return m_PlayerID;
}
 
private:
//other class data members here...
int m_PlayerID;
 
};

What's the potential wrong with the code?

19) what is a class?

20) If a class has a virtual function, what does the class need to have?

xpi0t0s 29Mar2011 14:12

Re: C++ Questions
 
Homework? Try reviewing the course notes and/or textbook; the answers will be in there. Sorry, but this means you're going to have to take some time to do this.


All times are GMT +5.5. The time now is 05:10.