// FIG7_5.CPP // Friends can access private members of a class. #include // Modified Count class class Count { friend void setX(Count &, int); // friend declaration public: Count() { x = 0; } // constructor void print() const { cout << x << endl; } // output private: int x; // data member }; // Can modify private data of Count because // setX is declared as a friend function of Count void setX(Count &c, int val) { c.x = val; // legal: setX is a friend of Count } main() { Count object; cout << "object.x after instantiation: "; object.print(); cout << "object.x after call to setX friend function: "; setX(object, 8); // set x with a friend object.print(); return 0; } --------------------------------------------------------- // FIG7_7.CPP // Using the this pointer to refer to object members. #include class Test { public: Test(int = 0); // default constructor void print() const; private: int x; }; Test::Test(int a) { x = a; } // constructor void Test::print() const { cout << " x = " << x << endl << " this->x = " << this->x << endl << "(*this).x = " << (*this).x << endl; } main() { Test a(12); a.print(); return 0; } ---------------------------------------------------- // TIME6.H // Declaration of class Time. // Member functions defined in TIME6.CPP #ifndef TIME6_H #define TIME6_H class Time { public: Time(int = 0, int = 0, int = 0); // default constructor // set functions Time &setTime(int, int, int); // set hour, minute, second Time &setHour(int); // set hour Time &setMinute(int); // set minute Time &setSecond(int); // set second // get functions (normally declared const) int getHour() const; // return hour int getMinute() const; // return minute int getSecond() const; // return second // print functions (normally declared const) void printMilitary() const; // print military time void printStandard() const; // print standard time private: int hour; // 0 - 23 int minute; // 0 - 59 int second; // 0 - 59 }; #endif ------ // TIME6.CPP // Member function definitions for Time class. #include "time6.h" #include // Constructor function to initialize private data. // Calls member function setTime to set variables. // Default values are 0 (see class definition). Time::Time(int hr, int min, int sec) { setTime(hr, min, sec); } // Set the values of hour, minute, and second. Time &Time::setTime(int h, int m, int s) { hour = (h >= 0 && h < 24) ? h : 0; minute = (m >= 0 && m < 60) ? m : 0; second = (s >= 0 && s < 60) ? s : 0; return *this; // enables chaining } // Set the hour value Time &Time::setHour(int h) { hour = (h >= 0 && h < 24) ? h : 0; return *this; // enables chaining } // Set the minute value Time &Time::setMinute(int m) { minute = (m >= 0 && m < 60) ? m : 0; return *this; // enables chaining } // Set the second value Time &Time::setSecond(int s) { second = (s >= 0 && s < 60) ? s : 0; return *this; // enables chaining } // Get the hour value int Time::getHour() const { return hour; } // Get the minute value int Time::getMinute() const { return minute; } // Get the second value int Time::getSecond() const { return second; } // Display military format time: HH:MM:SS void Time::printMilitary() const { cout << (hour < 10 ? "0" : "") << hour << ":" << (minute < 10 ? "0" : "") << minute << ":" << (second < 10 ? "0" : "") << second; } // Display standard format time: HH:MM:SS AM (or PM) void Time::printStandard() const { cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":" << (minute < 10 ? "0" : "") << minute << ":" << (second < 10 ? "0" : "") << second << (hour < 12 ? " AM" : " PM"); } ----------------- // FIG7_8.CPP // Chaining member function calls together // with the this pointer #include #include "time6.h" main() { Time t; t.setHour(18).setMinute(30).setSecond(22); cout << "Military time: "; t.printMilitary(); cout << endl << "Standard time: "; t.printStandard(); cout << endl << endl << "New standard time: "; t.setTime(20, 20, 20).printStandard(); cout << endl; return 0; } --------------------------------------------------- // EMPLOY1.H // An employee class #ifndef EMPLOY1_H #define EMPLOY1_H class Employee { public: Employee(const char*, const char*); // constructor ~Employee(); // destructor char *getFirstName() const; // return first name char *getLastName() const; // return last name // static member function static int getCount(); // return # objects instantiated private: char *firstName; char *lastName; // static data member static int count; // number of objects instantiated }; #endif -------- // EMPLOY1.CPP // Member functions definitions for class Employee #include #include #include #include "employ1.h" // Initialize the static data member int Employee::count = 0; // Define the static member function that // returns the number of employee objects instantiated. int Employee::getCount() { return count; } // Constructor dynamically allocates space for the // first and last name and uses strcpy to copy // the first and last names into the object Employee::Employee(const char *first, const char *last) { firstName = new char[ strlen(first) + 1 ]; assert(firstName != 0); // ensure memory allocated strcpy(firstName, first); lastName = new char[ strlen(last) + 1 ]; assert(lastName != 0); // ensure memory allocated strcpy(lastName, last); ++count; // increment static count of employees cout << "Employee constructor for " << firstName << ' ' << lastName << " called." << endl; } // Destructor deallocates dynamically allocated memory Employee::~Employee() { cout << "~Employee() called for " << firstName << ' ' << lastName << endl; delete firstName; // recapture memory delete lastName; // recapture memory --count; // decrement static count of employees } // Return first name of employee char *Employee::getFirstName() const { char *tempPtr = new char[strlen(firstName) + 1]; assert(tempPtr != 0); // ensure memory allocated strcpy(tempPtr, firstName); return tempPtr; } // Return last name of employee char *Employee::getLastName() const { char *tempPtr = new char[strlen(lastName) + 1]; assert(tempPtr != 0); // ensure memory allocated strcpy(tempPtr, lastName); return tempPtr; } ---------------------- // FIG7_9.CPP // Driver to test the employee class #include #include "employ1.h" main() { cout << "Number of employees before instantiation is " << Employee::getCount() << endl; // use class name Employee *e1Ptr = new Employee("Susan", "Baker"); Employee *e2Ptr = new Employee("Robert", "Jones"); cout << "Number of employees after instantiation is " << e1Ptr->getCount() << endl; cout << endl << "Employee 1: " << e1Ptr->getFirstName() << " " << e1Ptr->getLastName() << endl << "Employee 2: " << e2Ptr->getFirstName() << " " << e2Ptr->getLastName() << endl << endl; delete e1Ptr; // recapture memory delete e2Ptr; // recapture memory cout << "Number of employees after deletion is " << Employee::getCount() << endl; return 0; } -------------------------------------------------------