Beruflich Dokumente
Kultur Dokumente
1. What is virtual constructors/destructors? Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the base-class destructor function (matching the pointer type) is called on the object. There is a simple solution to this problem declare a virtual base-class destructor. This makes all derived-class destructors virtual even though they dont have the same name as the baseclass destructor. Now, if the object in the hierarchy is destroyed explicitly by applying the delete operator to a base-class pointer to a derived-class object, the destructor for the appropriate class is called. Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error. Does c++ support multilevel and multiple inheritance? Yes. 2. Why Garbage collection? Since C++ does not provide automatic garbage collection like some other languages, smart pointers can be used for that purpose. The simplest garbage collection scheme is reference counting or reference linking, but it is quite possible to implement more sophisticated garbage collection schemes with smart pointers. 3. How to write a swap( ) function which swaps the values of the variables using bitwise operators.? Ans: Here is the swap( ) function. swap ( int *x, int *y ) { *x ^= *y ; *y ^= *x ; *x ^= *y ; } The swap( ) function uses the bitwise XOR operator and does not require any temporary variable for swapping. 4. What are the advantages of inheritance? It permits code reusability. Reusability saves time in program development. It encourages the reuse of proven and debugged high-quality software, thus reducing problem after a system becomes functional. 5. What is the difference between declaration and definition? The declaration tells the compiler that at some later point we plan to present the definition of this declaration. E.g.: void stars () //function declaration The definition contains the actual implementation. E.g.: void stars () // declarator { for(int j=10; j>=0; j--) //function body cout<<*; cout< function_declaration; template function_declaration;
Q. When linking C or Assembly language modules with C++ modules I get undefined symbol errors at link time. It appears that none of the C or Assembly public symbols can be found. A. C++ is a strongly typed language. In order to support the language to its fullest, Turbo C++ must attach information to the symbols generated for function names and variables. When this is done, the symbol will no longer match the standard C style function name. In order to link correctly, the compiler must be notified that the symbol is declared in an external module without type information tacked on to the symbol. This is done by prototyping the function as type extern "C". Here is a quick example: extern "C" int normal_c_func( float, int, char ); // name not altered void cplusplus_function( int ); // name altered See related comments under Linker Errors and in the Paradox Engine question in this section. Q. Classes with static data members are getting linker errors ("undefined"). A. This code is built into Turbo C++ 1.0 but not in version 3.0. In the 1.0 compiler, static members without definitions were given a default value of 0. This default definition will no longer be made in the compiler. The programmer must now give an explicit definition for each static member. Here is a quick example: class A { static int i; }; A linker error saying that A::i is not defined will result unless the source also contains a line such as: int A::i = 1; Q. What potential problems can arise from typecasting a base class pointer into a derived class pointer so that the derived class's member functions can be called? A. Syntactically this is allowable. There is always the possibility of a base pointer actually pointing to a base class. If this is typecast to a derived type, the method being called may not exist in the base class. Therefore, you would be grabbing the address of a function that does not exist. Q: What's the difference between the keywords STRUCT and CLASS? A: The members of a STRUCT are PUBLIC by default, while in CLASS, they default to PRIVATE. They are otherwise functionally equivalent. Q: I have declared a derived class from a base class, but I can't access any of the base
functions such as printf() and scanf() will continue to be available. However, using them in conjunction with stream oriented functions can lead to unpredictable behaviour. Q. In C++, given two variables of the same name, one local and one global, how do I access the global instance within the local scope? A. Use the scope (::) operator. int x = 10; for(int x=0; x < ::x; x++) {
Q. Can I statically allocate more than 64K of data in a single module? A. Yes. Far data items are now supported: ... char far array1[60000L]; char far array2[60000L]; ... For arrays larger than 64k use: char huge array3[100000L]; Q. What is a friend member function? A. Declaring a friend gives non-members of a class access to the non-public members of a class. Q. Why do I get a "Type name expected" error on my definition of a friend class in my new class? A You need to let the compiler know that the label you use for your friend class is another class. If you do not want to define your entire class, you can simply have "class xxx", where xxx is your label. Q: How can I output hex values in upper case using the iostream libraries? A: You need to set the state of the stream using setf(). For example, #include int main(void) { cout << hex; cout << "\nNot upper-case : " << 255;