Mega Code Archive

 
Categories / C++ / Data Type
 

Passing References to Objects

#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 & FunctionTwo (const SimpleCat & theCat); int main() {    cout << "Making a cat..." << endl;    SimpleCat Frisky;    cout << "Frisky is " << Frisky.GetAge() << " years old" << endl;    int age = 5;    Frisky.SetAge(age);    cout << "Frisky is " << Frisky.GetAge() << " years old" << endl;    cout << "Calling FunctionTwo..." << endl;    FunctionTwo(Frisky);    cout << "Frisky is " << Frisky.GetAge() << " years old" << endl;    return 0; } // functionTwo, passes a ref to a const object const SimpleCat & FunctionTwo (const SimpleCat & theCat) {    cout << "Frisky is now " << theCat.GetAge();    return theCat; }