190 likes | 304 Vues
This lecture covers core concepts of arrays of objects and inheritance in C++. It discusses the creation of arrays, emphasizing static and dynamic arrays, initializers, and constructors. The use of catch blocks for exception handling is explained along with potential allocation failures. Additionally, multiple inheritance is covered, illustrating how derived classes interact with base class functionality. The importance of constructors, parameter passing, and access control (public, private, protected) is highlighted, providing a comprehensive understanding of object-oriented programming principles in C++.
E N D
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. • 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
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
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
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
dynamic object arrays • same method. • Can’t use initializers • Classes must have an empty constructor or no contructor • same deletion method.
exception throwing • Exception is thrown if error occurs. • catching exception handles them.
catching • Potential bad code is surrounded by a try { //potential exception here } catch (type_of_exception name) { //code to handle exception }
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; }
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
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.
derived classes • doesn’t get to access base’s private functions • There is a workaround
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.
multiple inheritence class base {…} class derived1 : public base { …} class derived2: public derived1 { …} • derived2 gets all of base and all of derived1
multiple inheritence class base1{ ..} class base2{ …} class derived: public base1, public base2{…} //derived gets public fields and functions from both
Order of creation • base class constructors called first • derived class deconstructor called first.
passing base parameters • derived can pass parameters to base’s constructor. • Must tell derived constructor to call base constructor and pass parameters
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;} };
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