Logistics • Job related news C++ Classes – Co-op orientation • Friday, Sept 16 th 12:00 – 1:30pm 70-1400 • Friday, Oct 14 th 1:00 – 2:30pm Eastman Aud. – Career Fair • Wednesday, Sept 28 th • 11am – 4pm • Gordon Field House Project C++ == (C with Classes) • Classes are extensions of struct • Any questions? – Access rules on data members – Constructor / Destructor functions • Coding style – Overloaded Operators – Commenting – Member functions – File organization – Use of RCS Access rules File organization • Like in Java…data members can be • Header file (.h) – Public – all functions have access to data – Declaration of class member – Included in all source that use the class. – Private – only member functions have access to • Source file (.cpp) data member – Code implementing all class functions – Protected – only class and devired classes have access to the data member • Let’s look at a simple class 1
Box.h Box.cpp #ifndef BOX_DEFINED #include “Box.h” #define BOX_DEFINED Box::Box (int h, int w, int d) class Box { { height=h; width=w; depth=d; private: int height, width, depth; } public: Box (int h, int w, int d); int Box::volume () int volume (); { }; return height * width * height; #endif } Classes and memory Constructor • Box: • Special member function used to create an height object of a class. – Has same name as the class. width – Does not return anything. depth – Used to initialize data members. Member functions Box.cpp Object initialization #include “Box.h” • Objects are initialized using constructors made available by the class. Box::Box (int h, int w, int d) { Box mybox (3,4,5); height=h; width=w; depth=d; Box *boxOnHeap = new Box(3,4,5); } // Note we can do this with intristic data int Box::volume () // types as well { int a (7); return height * width * height; char b (‘j’); } float f (7.8); 2
Default constructor Sidebar: initialization v. assignment • This • Constructor that takes no argument int a (7); //initialization • Used to construct a “default” object. • Is the same as this • Generally a good idea to provide a default int a=7; //initialization constructor. • But not this int a; a=7; //assignment Default Constructor Constructor overloading class Box // This is okay • A class can have multiple constructors. { Box mybox (3,4,5); private: • Which gets called depends upon arguments int height, width, // This is not listed when instantiated. depth; Box defaultBox; public: Box (int h, int w, int d); int volume (); }; Default Constructor Default constructor class Box include “Box.h” // This is okay { Box mybox (3,4,5); private: Box::Box (int h, int w, int d) int height, width, // Now,this is okay { depth; Box defaultBox; public: height=h; width=w; depth=d; Box(); } Box (int h, int w, int Box::Box () d); { int volume (); }; height=1; width=1; depth=1; } 3
Default constructor arguments C++ Rules for constructors • You can provide default args for your constructor • If a class does not define any constructors, a class Box default constructor is automatically created { for it private: int height, width, depth; • If a class defines at least one constructor, a public: Box (int h=1, int w=1, int d=1); default constructor must be explicitly define int volume (); }; Box thisBox0; • WARNING: Never trust the compiler to do Box thisBox1(3); Box thisBox2(3,4); it for you!!! Box thisBox3(5,6,9); Default Constructor Default Constructor class Box // This is okay class Box { { Box mybox (3,4,5); private: private: // Now, this is okay int height, width, int height, width, // This is not Box defaultBox; depth; depth; Box defaultBox; public: public: Box (int h, int w, int int volume (); d); }; int volume (); }; Objects and arrays Parameter initialization list • Objects placed in arrays must have a default • Initializes data members upon construction constructor. rather than in body of constructor Box::Box (int h, int w, int d): //default constructor required height(h), width(w), depth (d) Box boxArray[20]; { Box boxArrayOnHeap = new Box[20]; } // but not here Box *boxPtrArray[20]; 4
Parameter initialization list Parameter initialization list class PairOfBoxes • Copy vs. Assignment { Box::Box (int h, int w, int d) private: Box b1; { Box b2; height=h; width=w; depth=d; public: } PairOfBoxes (); }; Can also be written using parameter initialization list (this way is more efficient). class Box { Box::Box (int h, int w, int d): private: int height, width, depth; height(h), width(w), depth (d) public: {} // no assignment is made. Box (int h, int w, int d); }; Parameter initialization list Copy constructor • Must use initialization list • Used to construct an object from another object of the same class PairOfBoxes::PairOfBoxes(): • Object has access to non-public members of b1 (1,2,3), b2(4,5,6) objects of the same class {} Copy constructor Copy constructor class Box Box B (1,2,3); { Box C(B); private: int height, width, depth; Or public: Box B (1,2,3); Box (const Box& B); Box C = B; // initialization not assignment Box (int h, int w, int d); }; Box::Box (const Box& B) : height (B.height), width (B.width), height (B.height) {} 5
Constructor Destructor • Copy Constructor • A destructor for a class is called when the memory of an object of that class is reclaimed: – If no copy constructor is defined for a class, the – A global (static) object is reclaimed when the program default copy constructor is used. terminates. • Member by member copy of data from one object to – A local (automatic) object is reclaimed when the another. function terminates (stack is popped). • Can be troublesome if class have pointers as data – A dynamically allocated object is reclaimed when members. someone invokes the delete operator on it. • WARNING: Never trust the compiler to do it for you!!! – Like Java finalize Destructor Destructor void aFunction (Foo f) • Destructors have the same name as the class but preceded with a ~. { • Destructors do not return a value. Foo f2; Foo *fooptr = new Foo(); • Destructors take no arguments and cannot be overloaded. ... delete fooptr; // destructor called • Destructors are used for cleaning up object data / state } – Allocated memory // after function is complete, destructor – Close files called on f and f2 – Close network connections, etc. Destructors Destructors class Foo Foo::Foo (int size) : asize (size), array_member (new int[size]) { { private: for (int i=0; i<size; i++) int *array_member; array_member[i] = 0; int asize; } ... Foo::~Foo () public: { Foo (int size); // cleanup what was allocated by ~Foo (); // constructor } if (array_member != 0) delete array_member; } 6
Summary For next time • Classes • Continue with classes… – Member access – Conversions/Conversion Constructors – Constructors – Operators • Default constructors – When and where object are created • Copy constructors • Questions? – Destructors • Have a good weekend 7
Recommend
More recommend