Passing Pointer to a Constant Object : pointer « Data Type « C++






Passing Pointer to a Constant Object

  
#include <iostream>

using namespace std;
class SimpleCat
{
  public:
    SimpleCat();
    SimpleCat(SimpleCat&);
    ~SimpleCat();

    int GetAge() const { return itsAge; }
    void SetAge(int age) { itsAge = age; }

  private:
    int itsAge;
};

SimpleCat::SimpleCat()
{
   cout << "Simple Cat Constructor..." << endl;
   itsAge = 1;
}

SimpleCat::SimpleCat(SimpleCat&)
{
   cout << "Simple Cat Copy Constructor..." << endl;
}

SimpleCat::~SimpleCat()
{
   cout << "Simple Cat Destructor..." << endl;
}

const SimpleCat * const FunctionTwo
   (const SimpleCat * const theCat);

int main()
{
   cout << "Making a cat..." << endl;
   SimpleCat Frisky;
   cout << "Frisky is " ;
   cout << Frisky.GetAge();
   cout << " years old" << endl;
   int age = 5;
   Frisky.SetAge(age);
   cout << "Frisky is " ;
   cout << Frisky.GetAge();
   cout << " years old" << endl;
   cout << "Calling FunctionTwo..." << endl;
   FunctionTwo(&Frisky);
   cout << "Frisky is " ;
   cout << Frisky.GetAge();
   cout << " years old" << endl;
   return 0;
}

// functionTwo, passes a const pointer
const SimpleCat * const FunctionTwo(const SimpleCat * const theCat)
{
   cout << "Frisky is now " << theCat->GetAge();
   cout << " years old " << endl;
   return theCat;
}
  
    
  








Related examples in the same category

1.Returning Values with Pointers
2.Creating a Stray Pointer
3.Demonstrating Passing by Value