Sams Teach Yourself C++ in 21 Days

(singke) #1

  1. You can’t assign a value to a constant object, and you can’t reassign a constant
    pointer. This means that lines 6 and 8 are problems.

  2. The following is one possible answer. Note that this is a dangerous program to run
    because of the stray pointer.
    1: int main()
    2: {
    3: int pVar;
    4:
    pVar = 9;
    5: return 0;
    6: }

  3. The following is one possible answer:
    1: int main()
    2: {
    3: int VarOne;
    4: int pVar = &varOne;
    5:
    pVar = 9;
    6: return 0;
    7: }

  4. The following is one possible answer. Note that you should avoid memory leaks in
    your programs.
    1: #include
    2: int FuncOne();
    3: int main()
    4: {
    5: int localVar = FunOne();
    6: std::cout << “The value of localVar is: “ << localVar;
    7: return 0;
    8: }
    9:
    10: int FuncOne()
    11: {
    12: int pVar = new int (5);
    13: return
    pVar;
    14: }

  5. The following is one possible answer:
    1: #include
    2: void FuncOne();
    3: int main()
    4: {
    5: FuncOne();
    6: return 0;
    7: }
    8:
    9: void FuncOne()
    10: {


836 Appendix D

32 0672327112_app_d.qxd 11/19/04 12:30 PM Page 836

Free download pdf