Thursday, 26 May 2011

C++ program part7

  1. When is a template a better solution than a base class?
    When you are designing a generic class to contain or otherwise manage objects of other types, when the format and behavior of those other types are unimportant to their containment or management, and particularly when those other types are unknown (thus the genericity) to the designer of the container or manager class.
    Prior to templates, you had to use inheritance; your design might include a generic List container class and an application-specific Employee class. To put employees in a list, a ListedEmployee class is multiply derived (contrived) from the Employee and List classes. These solutions were unwieldy and error-prone. Templates solved that problem.

  2. What is the result of compiling this program in a C compiler? in a C++ compiler?
    int __ = 0;
    main() {
    int ___ = 2;
    printf("%d\n", ___ + __);
    }
    

    On GNU compiler both C and C++ give output as 2. I believe other C++ compiler will complain "syntax error", whereas C compiler will give 2.

  3. What is the difference between C and C++ ? Would you prefer to use one over the other ?
    C is based on structured programming whereas C++ supports the object-oriented programming paradigm.Due to the advantages inherent in object-oriented programs such as modularity and reuse, C++ is preferred. However almost anything that can be built using C++ can also be built using C.


  4. What are the access privileges in C++ ? What is the default access level ?
    The access privileges in C++ are private, public and protected. The default access level assigned to members of a class is private. Private members of a class are accessible only within the class and by friends of the class. Protected members are accessible by the class itself and it's sub-classes. Public members of a class can be accessed by anyone.

No comments:

Post a Comment