DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Closed Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2004
    Location
    Hungary
    Posts
    5

    General C++ TEST

    Who think is goog in c++ rezolve this.
    Any solution ,send me: istynet@yahoo.com
    General C++ questions

    1. What is the difference between const, inline and #define?
    a. What is the problem with #define directive?
    Please explain the disadvantages of using a macro as follows:

    #define MAX(a,b) ((a) > (b) ? (a) : (b))

    b. How can you re-write the above macro using inline and templates?
    c. How can you use the preprocessor to simulate templates?

    2. How to avoid hiding global new?
    a. Consider the example below. What’s wrong with it?

    typedef void (*PEHF)();

    class X {
    public:
    void f();


    // operator new allowing specification of
    // error-handling function
    void *operator new(size_t size, PEHF pehf);
    };

    void specialErrorHandler(); //definition omitted

    X *xp1 = new (specialErrorHandler) X;

    X *xp2 = new X;

    How to avoid the problem?

    3. Is the order of variables in the initialization list important?
    a. Consider the following class. What’s the problem with it?

    class Array {
    private:
    int *data;
    unsigned size;
    int lBound, hBound;

    public:
    Array(int lowBound, int highBound);
    };

    Array::Array(int lowBound, int highBound)
    : size(highBound – lowBound + 1), lBound(lowBound),
    hBound(highBound), data(new int[size])
    {}

    What about static data member initialization? When does it happen?


    4. What should be the return vales of operator= and why?
    a. Consider the following class:

    class String {
    private:
    char *data;

    public:
    String(const char *value = 0);
    };

    b. Please write operator= for this class. One that accepts String objects, the second that takes char*.
    c. What is the signature of the default version?
    d. Which one of the following snippets is right? Why?
    What happens in the second case?

    String& String::operator=(const String& rhs)
    {
    ...
    return *this;
    }

    String& String::operator=(const String& rhs)
    {
    ...
    return rhs;
    }

    5. Why it is a good choice to avoid data members in the public interface?
    a. Please explain it with ‘consistency’ and accessibility!
    b. What is the third reason (probably the biggest advantage) not to have data members in the public interface?
    (Hint: functional abstraction)


    6. Why to use const if possible?
    a. Please consider the following declarations. Please explain the meaning of the const keyword.

    char *p = “Hello”;
    const char *p = “Hello”;
    char * const p = “Hello”;
    const char * const p = “Hello”;

    b. Look at the following example:

    class String {
    private:
    char *data;

    public:
    String(const char *value = 0);

    operator const char *() const { return data; }
    };

    c. What is the purpose of const member functions?
    d. Can member functions be overloaded if they only differ in their ‘constness’?

    char& operator[](int position) { return data[position]; }
    const char& operator[](int position) const
    { return data[position]; }

    e. What does it mean for a member function to be const?
    (Hint: bit wise, conceptual)
    f. Consider the following example. What is the problem and how would you resolve it?
    (Hint: cast away ‘constness’)

    class String {
    private:
    char *data;
    unsigned dataLength;
    unsigned lengthIsValid:1;

    public:
    String(const char *value = 0): lengthIsValid(0) {...}
    unsigned length() const;
    };

    unsigned String::length() const
    {
    if (!lenghtIsValid) {
    dataLength = strlen(data);
    lengthIsValid = 1;
    }

    return dataLength;
    }


    7. Overloading on pointer and numerical type.
    a. What is zero? I.e. what will happen here:

    void f(int x);
    void f(char *p);

    f(0); // calls f(int) or f(char*) ???

    b. How would you resolve the problem? Is it possible?
    c. What do you think about this?

    class Null {
    public:
    template<class T> operator *T() const { return 0; }
    };

    const Null NULL;

    f(NULL);


    8. “Handles” to internal data.
    a. Consider the following class and its usage. Explain the consequences.

    class String {
    private:
    char *data;

    public:
    String(const char *value = 0);
    operator char *() const; // Convert String ->char*
    ...
    };

    const String B(“Hello World”);
    String& alsoB = (String&) B;

    char *str = B;
    strcpy( str, “Hi Mom”);

    b. Does B still have the value “Hello world” or has it changed into a mother’s greeting? What is the correct implementation of String::operator char*?


    9. What are inline functions good for?
    a. Does inline functions have any influence on the size of the code?
    b. Is ‘inline’ a hint or a command to the compiler, i.e. can the compiler ignore inline directive?
    c. What happens (if possible at all) if the compiler chooses not to inline a function in the following situation?

    // This is file example.h
    inline void f() { . . . };
    . . .

    // This is file source1.cc
    #include “example.h” //includes definition of f

    // This is file source2.cc
    #include “example.h” // also includes definition
    // of f

    d. What happens in the following situation with an inline function? How does the compiler resolve the problem?

    inline f() { . . . };

    void (*pf)() = f; // pf points to f

    main()
    {
    f(); // an inline call to f
    pf(); // a non-inline call to f
    }

    e. Can you tell a couple examples why inline functions can be hard to manage?
    (Hint: debugging)


    10. What is the purpose of a multithreaded program?
    a. Where is it appropriate to use multithreaded applications?
    b. How would you make a thread waiting?
    Why it is a bad idea to write endless loops in a multithreaded environment
    Attached Files

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Sorry dearie, but you must be new on this forum and that's why you probably don't know that we don't do other peoples' homework, let alone solve their exams. If you have a specific question about a program you're writing by yourslef, you're welcome to ask for our advice. If it's plain cheating, then I'm sorry but this is the wrong place. This thread is closed, with reason.
    Last edited by Danny; 05-10-2004 at 11:38 AM.
    Danny Kalev

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links