Sams Teach Yourself C++ in 21 Days

(singke) #1
Answers 837

D


11: int * pVar = new int (5);
12: std::cout << “The value of *pVar is: “ << *pVar ;
13: delete pVar;
14: }
8.MakeCatreturns a reference to the CATcreated on the free store. There is no way to
free that memory, and this produces a memory leak.


  1. The following is one possible answer:
    1: #include
    2: using namespace std;
    3: class CAT
    4: {
    5: public:
    6: CAT(int age) { itsAge = age; }
    7: ~CAT(){}
    8: int GetAge() const { return itsAge;}
    9: private:
    10: int itsAge;
    11: };
    12:
    13: CAT MakeCat(int age);
    14: int main()
    15: {
    16: int age = 7;
    17: CAT
    Boots = MakeCat(age);
    18: cout << “Boots is “ << Boots->GetAge() << “ years old”;
    19: delete Boots;
    20: return 0;
    21: }
    22:
    23: CAT * MakeCat(int age)
    24: {
    25: return new CAT(age);
    26: }


Day 10


Quiz


  1. Overloaded member functions are functions in a class that share a name but differ
    in the number or type of their parameters.

  2. A definition sets aside memory; a declaration does not. Almost all declarations are
    definitions; the major exceptions are class declarations, function prototypes, and
    typedefstatements.

  3. Whenever a temporary copy of an object is created. This also happens every time
    an object is passed by value.


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

Free download pdf