# OOAD Lab 1.5, 2 (03/02) ###### tags: `OOAD` --- [TOC] --- ## 1.5. Pointer and References - *"C 的 pointer 是 "indirect addressing mode" (間接定址)的高階語言實作"* ^[CYP]^ - *"Activation record"*: - An activation record (AR) is a private block of memory associated with an invocation of a procedure. [^[LINK]^](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&ved=2ahUKEwjKovPd2br9AhWbfd4KHU5UA1cQFnoECBgQAw&url=https%3A%2F%2Fwww.rose-hulman.edu%2Fclass%2Fcsse%2Fcsse404%2Fschedule%2Fday17%2FProcedures.pdf&usg=AOvVaw2q3DaYur9oAsF3a2thFJFy) - 一個 activation record (stack frame) 儲存程式的:[^[LINK]^](https://liquid0118.pixnet.net/blog/post/48494862) - 參數與本地變數 - 執行結果(回傳值) - 返回位址(return address) - 其他環境資訊 - *"How to swap two variables in C? (pass by address)"* ```c= void swap(char* a, char* b) { char t; t = (*a); (*a) = (*b); (*b) = t; } int main() { char x, y; swap_c(&x, &y); } ``` - "C++'s pass by reference?" ```cpp= void swap(char& a, char& b) { char t; t = a; a = b; b = t; } int main() { char x, y; swap(x, y); } ``` - ```cpp= int main() { int& a; } ``` ```= $ g++ a.cpp a.cpp: In function ‘int main(int, char**)’: a.cpp:3:10: error: ‘a’ declared as reference but not initialized int& a; ^ ``` - "Java's pass by reference?" ```java= class T { int v; } char c; // A char-type variable do(c); // pass by value T a; // A reference variable to T a = new T(); do(a); // pass by reference ``` - "How to swap two (primitive) variables in pass-by-reference in Java?" - The default wrapper classes in Java is **immutable**, so assigning will init new objects [^[LINK]^](https://stackoverflow.com/q/5560176) - That is, the passed `Integer` object originally in the ref var in the swapping function will be replaced with newly inited `Integer` object - ---> one way: manually declare mutable wrapper classes [^[LINK]^](https://stackoverflow.com/a/4319581) ```java= class IntRef { public int v; } void swap(IntRef a, IntRef b) { int t; t = a.v; a.v = b.v; b.v = t; } main() { IntRef x = new IntRef(); IntRef y = new IntRef(); x.v = 10; y.v = 25; swap(x, y); } ``` ## 2. Inheritance - **All the following is C++!!!** - `public`: - publicly derived subtypes of the base class will be able to directly access these variables. - `protected`: - only the base class could directly access them - `private`: - any class or part of code could access them - these would defeat a key goal of object-oriented design: **encapsulation** data within a class and exposing the data only through the public interface - All constructors, including copy constructors, destructors aer not inherited - inheritance: extending additional behaviors in OO system development - **Basic principle of inheritance**: - The base class: - contains common members and methods used by the subclasses - The subtypes: - are more specialized (特異化) then the base class - **(Cons/des call)**: - the base class part of objects is always constructed first, destroyed last - the subclass part of objects is always constructed last, destroyed first - **每一個 subclass 要負責建構與清除自己的特異化的的部分** - in default, subclasses use the default constructor of the base class (take no argument) - Add the constructor of the base class into the **member initialization list**: ```cpp= class Pet { public: Pet() : weight(1), food("Pet Chow") {} Pet(int w) : weight(w), food("Pet Chow") {} Pet(int w, string f) : weight(w), food(f) {} protected: int weight; string food; }; class Rat : public Pet { public: Rat() {} Rat(int w) : Pet(w) {} Rat(int w, string f) : Pet(w, f) {} }; ``` - **Override**: - A derived class can override the methods of its base class(es) - The method in the derived class must have the same same **signature** as the base class method to override - **Overloading** - many methods of the same name with different signatures are created - $\ne$ polymorphism - **Overriding a single one of the overloads will hide the rest!** - it is better to override all the overloads - **Composition** (contain other objects as members) $\leftrightarrow$ **Inheritance** - **The Principle of Inheritance** 1. A *subclass* is a *base class* (recursively) A *base class* is not necessary a subclass (recursively) 2. *subclass*'s behaviors must be specialized and distinguished from *base class*'s - **Inheritance without polymorphism is not object-oriented** - **When you use a subclass to override a base class's method. C++ will use the current type to determine the method**: ```cpp= class pet { void speak() {cout << "Growl" << endl;} } class pet : public pet { void speak() {cout << "meow" << endl;} } main() { pet insect; cat pussy; pet* nose = (pet*) new cat(); insect.speak(); pussy.speak(); ((pet) pussy).speak(); nose->speak(); } ``` ```= pet constructor // `insect` pet constructor // `pussy` cat constructor // ... pet constructor // `nose` cat constructor // ... Growl // `insect.speak()` meow // `pussy.speak()` Growl // `((pet) pussy).speak()` => call pet's copy cons first pet destructor // ... call pet's des Growl // `nose->speak()` cat destructor // `pussy` pet destructor // ... pet destructor // `insect` ```