C++ Questions

Discussion in 'C++' started by zackary13, Mar 28, 2011.

  1. zackary13

    zackary13 New Member

    Joined:
    Mar 28, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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:
    [FONT=Calibri][SIZE=3]/**************************************************[/SIZE][/FONT]
    [SIZE=3][FONT=Calibri]*[/FONT][/SIZE]
    [SIZE=3][FONT=Calibri]* Name: CMain.cpp[/FONT][/SIZE]
    [SIZE=3][FONT=Calibri]*[/FONT][/SIZE]
    [SIZE=3][FONT=Calibri]* Description: [/FONT][/SIZE]
    [SIZE=3][FONT=Calibri]* Linker: no dependencies needed[/FONT][/SIZE]
    [SIZE=3][FONT=Calibri]*[/FONT][/SIZE]
    [SIZE=3][FONT=Calibri]* Change history:[/FONT][/SIZE]
    [SIZE=3][FONT=Calibri]*[/FONT][/SIZE]
    [SIZE=3][FONT=Calibri]* Author: [/FONT][/SIZE]
    [SIZE=3][FONT=Calibri]*[/FONT][/SIZE]
    [SIZE=3][FONT=Calibri]**************************************************/[/FONT][/SIZE]
     
    [FONT=Calibri][SIZE=3]//-----------------------------------------------------------------------------[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]// Specific Includes[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]//-----------------------------------------------------------------------------[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]#include <iostream>[/SIZE][/FONT]
     
     
    [FONT=Calibri][SIZE=3]//-----------------------------------------------------------------------------[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]// Function Declaration[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]//-----------------------------------------------------------------------------[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]int checkDigit(const int&, const int&);[/SIZE][/FONT]
     
     
    [FONT=Calibri][SIZE=3]//-----------------------------------------------------------------------------[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]// Namespaces define[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]//-----------------------------------------------------------------------------[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]using namespace std;[/SIZE][/FONT]
     
    [FONT=Calibri][SIZE=3]//-----------------------------------------------------------------------------[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]// Name : main()[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]// Desc : main function[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]//-----------------------------------------------------------------------------[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]int main()[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]{[/SIZE][/FONT]
     
    [SIZE=3][FONT=Calibri]               //stuff here[/FONT][/SIZE]
     
    [SIZE=3][FONT=Calibri]               return(0);[/FONT][/SIZE]
     
    [FONT=Calibri][SIZE=3]}[/SIZE][/FONT]
     
     
    [FONT=Calibri][SIZE=3]//-----------------------------------------------------------------------------[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]// Name : checkDigit[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]// Desc : computing checkDigit[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]//-----------------------------------------------------------------------------[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]int checkDigit(const int& numb, const int& index) {[/SIZE][/FONT]
     
    [SIZE=3][FONT=Calibri]               //implementation stuff here[/FONT][/SIZE]
     
    [SIZE=3][FONT=Calibri]               return result;[/FONT][/SIZE]
    [FONT=Calibri][SIZE=3]}[/SIZE][/FONT]
     
    
    If you look at the Function Declaration, it has

    Code:
    [FONT=Calibri][SIZE=3]//-----------------------------------------------------------------------------[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]// Function Declaration[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]//-----------------------------------------------------------------------------[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]int checkDigit(const int&, const int&);[/SIZE][/FONT]
    
    and at the bottom, we have the implementation

    Code:
    [FONT=Calibri][SIZE=3]//-----------------------------------------------------------------------------[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]// Name : checkDigit[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]// Desc : computing checkDigit[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]//-----------------------------------------------------------------------------[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]int checkDigit(const int& numb, const int& index) {[/SIZE][/FONT]
     
    [SIZE=3][FONT=Calibri]               //implementation stuff here[/FONT][/SIZE]
     
    [SIZE=3][FONT=Calibri]               return result;[/FONT][/SIZE]
    [FONT=Calibri][SIZE=3]}[/SIZE][/FONT]
    
    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:
    [FONT=Calibri][SIZE=3]//-----------------------------------------------------------------------------[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]// Function Declaration[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]//-----------------------------------------------------------------------------[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]int checkDigit(const int& numb, const int& index);[/SIZE][/FONT]
    
    Code:
    [FONT=Calibri][SIZE=3]//-----------------------------------------------------------------------------[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]// Name : checkDigit[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]// Desc : computing checkDigit[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]//-----------------------------------------------------------------------------[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]int checkDigit(const int& numb, const int& index) {[/SIZE][/FONT]
     
    [SIZE=3][FONT=Calibri]               //implementation stuff here[/FONT][/SIZE]
     
    [SIZE=3][FONT=Calibri]               return result;[/FONT][/SIZE]
    [FONT=Calibri][SIZE=3]}[/SIZE][/FONT]
    
    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?
     
  2. zackary13

    zackary13 New Member

    Joined:
    Mar 28, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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:
    [FONT=Times New Roman]class Complex[/FONT]
    [FONT=Times New Roman]{[/FONT]
    [FONT=Times New Roman]public:[/FONT]
    [FONT=Times New Roman]Complex: (double real, double imaginary = 0) : _real(real), _imaginary(imaginary) { }[/FONT]
     
    [FONT=Times New Roman]void operator+( Complex other)[/FONT]
    [FONT=Times New Roman]{[/FONT]
    [FONT=Times New Roman]_real = _real + other._real;[/FONT]
    [FONT=Times New Roman]_imaginary = _imaginary + other._imaginary;[/FONT]
    [FONT=Times New Roman]}[/FONT]
     
    [FONT=Times New Roman]void operator <<( ostream os)[/FONT]
    [FONT=Times New Roman]{[/FONT]
    [FONT=Times New Roman]os << "(" << _real << "," << _imaginary << ")";[/FONT]
    [FONT=Times New Roman]}[/FONT]
     
    [FONT=Times New Roman]Complex operator++()[/FONT]
    [FONT=Times New Roman]{[/FONT]
    [FONT=Times New Roman]++_real;[/FONT]
    [FONT=Times New Roman]return *this;[/FONT]
    [FONT=Times New Roman]}[/FONT]
     
    [FONT=Times New Roman]Complex operator++(int)[/FONT]
    [FONT=Times New Roman]{[/FONT]
    [FONT=Times New Roman]Complex temp = *this;[/FONT]
    [FONT=Times New Roman]++_real;[/FONT]
    [FONT=Times New Roman]return temp;[/FONT]
    [FONT=Times New Roman]}[/FONT]
     
    [FONT=Times New Roman]private:[/FONT]
    [FONT=Times New Roman]double _real._imaginary;[/FONT]
     
    [FONT=Times New Roman]};[/FONT]
     
    
    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?
     
  3. zackary13

    zackary13 New Member

    Joined:
    Mar 28, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice