// Lesson 3 files // Andrew Fountain http://quadruple.ca // // ex34.h #include using namespace std; class person { private: int age; long ssn; public: long getssn(void) { return ssn; } void setssn(void) { cin >> ssn; } }; // ex34.cpp #include "ex34.h" int main(int argc, char *argv[]) { person p; cout << "Enter your SSN:"; p.setssn(); cout << "P's SSN is " << p.getssn() << endl; } //--------------------------------------------MODIFIED // ex34.cpp #include "ex34.h" int main(int argc, char *argv[]) { person p; cout << "Enter your SSN:"; p.setssn(); cout << "P's SSN is " << p.getssn() << endl; person * pptr = new person; cout << "Enter the SSN for the pointed-to person:"; pptr -> setssn(); cout << "The SSN of the pointed to person is " << pptr->getssn() << endl; //or (*pptr).getssn() delete pptr; } /////////////////////////////////////////////////////////////////////////// // ex35.h #include using namespace std; class ShortClass { private: int age; public: ShortClass() { age = 75 ; doit(); } void doit(void) { cout << "The ShortClass' age is " << age << endl; } }; class LongClass { private: int age; public: LongClass(); void doit(void) ; }; // ex35.cpp #include "ex35.h" int main(int argc, char *argv[]) { ShortClass theShort; LongClass theLong; } //--------------------------------------------MODIFIED // ex35.cpp #include "ex35.h" LongClass::LongClass() { age = 75; doit(); } void LongClass::doit(void) { cout << "The LongClass' age is " << age << endl; } int main(int argc, char *argv[]) { ShortClass theShort; LongClass theLong; } /////////////////////////////////////////////////////////////////////////// // ex36.h #include using namespace std; class person { protected: int age; long ssn; public: person(int a): age(a) { cout << "One-arg person with age of " << age << endl; } person(int a, long s): age(a), ssn(s) { cout << "Two-arg person" << endl; } }; // ex36.cpp #include "ex36.h" int main(int argc, char *argv[]) { person one(30); person two(28,719045712); } //--------------------------------------------MODIFIED // ex36.h #include using namespace std; class person { protected: int age; long ssn; public: person() { cout << "Age is " << age << ", SSN is " << ssn << endl; } person(int a): age(a) { cout << "One-arg person with age of " << age << endl; } person(int a, long s): age(a), ssn(s) { cout << "Two-arg person" << endl; } }; // ex36.cpp #include "ex36.h" int main(int argc, char *argv[]) { person none; person one(30); person two(28,719045712); } /////////////////////////////////////////////////////////////////////////// // ex37.cpp #include using namespace std; int * GlobalIPTR; class Foo { private: int * iptr; public: Foo() { iptr = new int; GlobalIPTR = iptr; cout << "Enter an integer: " << endl; cin >> *iptr; } void localptr() { cout << "Local pointer points to " << *iptr << endl; } }; int main(int argc, char *argv[]) { Foo * f = new Foo; f->localptr(); delete f; cout << "Object 'f' has been deleted" << endl; f->localptr(); } //--------------------------------------------MODIFIED cout << "Global Pointer points to " << *GlobalIPTR << endl; ~Foo() { delete iptr; } /////////////////////////////////////////////////////////////////////////// // ex38.cpp #include using namespace std; int * GlobalIPTR; class Worker { private: float salary; public: Worker() : salary(25.00) { showSalary(); } void showSalary() { cout << "The worker's salary is: " << salary << endl; } }; class Manager { public: void changeSalary(Worker& w) { w.salary *= 1.1f; } Manager() {} }; int main(int argc, char *argv[]) { Worker theWorker; Manager theManager; theManager.changeSalary(theWorker); theWorker.showSalary(); } //--------------------------------------------MODIFIED friend class Manager;