/* CLASS NOTES MAR 9 */ #include using namespace std; /********************* * * C'tors & D-tors * *********************/ /* 1) Initialization with a default value */ /* class point{ int x, y; public: // a and b is assigned to 0 if not provided point( int a = 0, int b = 0 ): x(a), y(b) {} void print() { cout << "(" << x << "," << y << ")" << endl; } }; int main() { point p1, p2(3), p3(1,3); cout << "p1: " ; p1.print(); cout << "p2: " ; p2.print(); cout << "p3: " ; p3.print(); return 0; } */ /* 2) conversion for a single arg, explicit keyword */ /* class point { int x, y; public: // add 'explicit' keyword in the beginning of c'tor to avoid implicit conversion explicit point( int a ) : x(a), y(0) {} void print() { cout << "(" << x << "," << y << ")" << endl; } }; int main(){ point p(3); p.print(); // implicit conversion example // int i = 4; // double d; // d = i; p = 4; p.print(); } */ /* 3)copy c'tors */ /* class point { public: int x, y ; point( int a, int b ): x(a), y(b) {} point( const point& pt ) { // copy c'tor cout << "copying x and y values" << endl; x = pt.x; y = pt.y; } }; void printPoint( point pnt ) { cout << "(" << pnt.x << "," << pnt.y << ")" << endl; } int main() { point p(3,4); printPoint(p); return 0; } */ /* 4) d'tors - destructors */ // called when a variable of type point is out of scope. use to 'delete' objects created with // 'new' keyword /* class point { public: int x, y ; point( int a, int b ): x(a), y(b) {} ~point(){ cout << "destructing point with x: " << x << " and y: " << y << endl; } }; void printPoint( point pnt ) { cout << "(" << pnt.x << "," << pnt.y << ")" << endl; } int main() { point p(3,4); printPoint(p); cout << "I am back at main()" << endl; cout << "p.x: " << p.x << ", p.y: " << p.y << endl; return 0; } */ /***************************** * * Friend Functions & Classes * *******************************/ // Special functions and classes which can access private members of a class // function example /* class A{ friend void func( A& a ); private: int x; }; void func( A& a ) { a.x = 5; cout << "x=" << a.x << endl; } int main() { A aobj; func(aobj); //aobj.x = 5; // illegal return 0; } */ // class example /* class A{ friend class B; private: int x; }; class B { A a; public: void setA( int i ) { a.x = i; cout << "a.x = " << a.x << endl; } }; int main() { B b; b.setA(3); return 0; } */ /********************************************* * * Derivation (Inheritence) and Composition * *********************************************/ // Composition : If Class A has an object of Class B as a member, // A and B are said to be in a compositional relationship. // Keyword 'HAS A' // ex: /* class trunk { int capacity; public: // public members }; class car { trunk t; int speed; public: // public members }; */ // car object 'HAS A' trunk object // Derivation: If class A extends Class B: // A is inheriting from B // A is called derived class, B is called base class // Keyword 'IS A' // ex: // public derivation - inherited members are declared public for derived class objects /* class motorVehicle { int speed; public: motorVehicle(): speed(0) {} void speedUp() { speed += 10; cout << "speeding up... new speed = " << speed << endl ; } }; class car : public motorVehicle { public: void race() { cout << "racing... " << endl; for( int i = 0 ; i < 3 ; i++ ) speedUp(); } }; int main() { car c; c.speedUp(); c.race(); } */ // private derivation - inherited members are declared private for derived class objects /* class motorVehicle { int speed; public: motorVehicle(): speed(0) {} void speedUp() { speed += 10; cout << "speeding up... new speed = " << speed << endl ; } }; class car : public motorVehicle { public: // cannot access private member of motorVehicle regardless of derivation type //void initSpeed( int i ) { // speed = i; // } void race() { cout << "racing... " << endl; for( int i = 0 ; i < 3 ; i++ ) speedUp(); } }; int main() { car c; c.init(3); //c.speedUp(); // ILLEGAL c.race(); } */