1 / 19

Lecture 22

Lecture 22. CIS 208 Friday, April 22th, 2005. Arrays of Objects (static). class cl { int j; public: cl(int k) { j = k;} int getJ() {return j;} }; two ways of creating arrays.

sun
Télécharger la présentation

Lecture 22

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 22 CIS 208 Friday, April 22th, 2005

  2. Arrays of Objects (static) class cl { int j; public: cl(int k) { j = k;} int getJ() {return j;} }; • two ways of creating arrays. • cl ob[3] = {1,2,3}; //creates 3 element array, and sends 1,2,3 to the constructors. Can only be used if has single parameter constructor. • cl ob[3] = {cl(1), cl(2), cl(3)}; // works on any number of parameters

  3. Arrays of objects • Classes without constructors can’t use initializers. • GD ob[3]; // assuming this class has no parameter’d constructors. • if class has only a parameterized constructor, then you must give initializers. • Make classes with empty constructor

  4. empty constructor class cl{ int j; public : cl(int k) { j = k;} cl() {j = 0;} //empty constructor int getJ(){return j;} }; now can use either initialized or uninitialized declarations cl ob[3]; //uninitialized cl ob[3] = {1,2,3}; // initialized

  5. dynamic arrays • p_var = new array_type[size]; • creates array that holds size amout of items • deletion delete [] p_var; • must use [] to delete arrays. • Can’t use initializers on dynamic arrays

  6. dynamic object arrays • same method. • Can’t use initializers • Classes must have an empty constructor or no contructor • same deletion method.

  7. exception throwing • Exception is thrown if error occurs. • catching exception handles them.

  8. catching • Potential bad code is surrounded by a try { //potential exception here } catch (type_of_exception name) { //code to handle exception }

  9. bad allocation exception #include <new> // must be included to test exception int *p; try { p = new int[10]; } catch(bad_alloc xa) { cout << “Allocation Failure\n”; return 1; }

  10. Inheritence • Make a class that is derived from a base class. • Gets all functionality of base class • may or may not have access to base’s private fields/functions

  11. inheritence • class derived_class_name : access base_class name{ //body of class }; • access can be public, private or protected. • if public, then all public function/fields of base class can be called outside class • if private, then base functions can’t be called from outside.

  12. derived classes • doesn’t get to access base’s private functions • There is a workaround

  13. protected • replaces private or public when describing fields/functions of base class. • derived classes get access, but no one else does. • in other words, private to all except the children.

  14. multiple inheritence class base {…} class derived1 : public base { …} class derived2: public derived1 { …} • derived2 gets all of base and all of derived1

  15. multiple inheritence class base1{ ..} class base2{ …} class derived: public base1, public base2{…} //derived gets public fields and functions from both

  16. Order of creation • base class constructors called first • derived class deconstructor called first.

  17. passing base parameters • derived can pass parameters to base’s constructor. • Must tell derived constructor to call base constructor and pass parameters

  18. base constructors class base { int k public: base(int l) { k = I; }; class derived: public base { int j; public : derived(int x,y) : base(x) {j = y;} };

  19. Derived Pointers • Classes B & D; • B* can point to either B or D objects • can only access B’s functions. • fixed by casting to D* • D* can’t point to B objects

More Related