Some Tough C++ Questions from Ed Ji
1. Let us say you are writing a kind of C++ compiler, how to convert a C++ class code into C code? What if you want to keep inheritance?
2. What are four things that are generated by C++ compiler if you do not declare them?
3. Assume you already know the concepts of inheritance, virtual method, and polymorphism. Suppose you are designing a thread base class, which will launch a thread function once Run( ) method in this thread base class is called. You want to leave the implementation of the thread function to the user who is using your thread base class. Use inheritance, show the design of the thread base class. Hint: you can use static method, and pure virtual method.
4. You are given a requirement document. And you need to do class design. Show how to do object oriented analysis and modeling to convert the requirement into classes.
5. What must be initialized in the initialization list of a class constructor
6. Show with example 1) when copy constructor is called, 2) when an assignment operator is called?
7. When is it necessary to declare a destructor virtual?
8. Assume CComplex is a class, if we have:
CComplex c = 2 + CComplex(10, 10) ;
then what is needed for this class?
9. When do you have to define a default constructor?
10. Design a way to print out timestamp whenever you call cout, for example, when you call cout << 100 << endl ;
11. You know that with class template, compiler can write code for you. Template metaprogramming: use template, ask C++ compiler to automatically write template code for you to compute Factorial(5). Show how to do this.
12. What are two things that are not inherited into derived calss from base class?
13. List the differences between pointer and reference (as many as you can)
14. You know that a class constructor can not return anything to the client of the class. Design two ways to notify the client of your class when something bad (or fatal) happens in the constructor of your class.
Is there other way besides the two ways?
15. You know that a class destructor can not take any argument. So we can not pass a refrence to the destructor and we do not want to use global variable.
And we do not want to use throw (together with try {...} catch( Error &e) {...} either.
Let us say something really bad happening within destructor. How do you notify the client of the class of the bad thing. Hint: think about one term frequently used in GUI programming. [ Here is one solution with inheritance. Please come up with another solution with callback function. ]
16. STL collection classes: You have a state machine to implement, show what collection classes can be used to quickly construct a state machine and its transitions. Here is an example solution implemented with map and list.
17. Sometimes you need to use 'this' pointer, like, class A
{
public: A ( const int a ) { this->a = a ; }
private: int a ;
} ;
Give other examples where you will need to use 'this' pointer. [ The solution for question 3 will have 'this' pointer. Another example ]
18. You may hear about 'component' often. A component is usually a complicated software entity that provides some kinds of functionalities. It is usually designed in such a way that it can easily interact with other components or applications. What must we usually have in a component? Describe basic steps in component programming. [ quick answer ]
How Strong am I in C++ OOP? Please read a small story about Ed Ji. Thank you!
(Note: Answers to these questions can be traced in my Ed's Technical Note written in vim.)
Copyright by Ed Ji. All rights reserved.