ECE 462Object-Oriented Programmingj g g
using C++ and Java
Inheritance (2)
Yung-Hsiang Lul @ d [email protected]
YHL Inheritance (2) 1
public or private inheritance in C++
YHL Inheritance (2) 2
public inheritance in C++public inheritance in C
• interfaceclass Person {protected:
string name;g ;public:
string getName() { return name; }};};class Student: public Person ...Person * p = new Student ... // allowedSt d t * P // t ll dStudent * s = new Person ... // not allowed
• implementation (attributes and methods)Student * s = new Student ...
YHL Inheritance (2) 3
s -> getName(); // allowed
calling the method in base classcalling the method in base class
class Person {public:
void print(...) { ... } // Person::print};class Student: public Person {public:
void print(...) {p ( ) {Person::print(); // call print in base class// print additional attributes
}}};Person * p = new Student ...p-> print(); // call Student::print, polymorphism
YHL Inheritance (2) 4
p print(); // call Student::print, polymorphism
calling base'st tconstructor
YHL Inheritance (2) 5
calling base'smethodmethod
YHL Inheritance (2) 6
Derived Class is "Bigger"Derived Class is Bigger
• A derived class has everything (all attributes + all public y g ( pand protected methods) in the base class.
• A derived class probably has additional attributes and th dmethods.
⇒ A derived class is “bigger”.
• base class: general• derived class: specific,
base class
p ,more requirements
• inheritance = specializationderived class
YHL Inheritance (2) 7
YHL Inheritance (2) 8
Copy Constructor in Derived ClassCopy Constructor in Derived Class
YHL Inheritance (2) 9
YHL Inheritance (2) 10
YHL Inheritance (2) 11
Operator = and Derived ClassOperator = and Derived Class
YHL Inheritance (2) 12
YHL Inheritance (2) 13
YHL Inheritance (2) 14
YHL Inheritance (2) 15
Operator Overloading and Derived Class
YHL Inheritance (2) 16
ostream& operator<<(ostream& os, const Person& p) { os << p.name;
return os;}
YHL Inheritance (2) 17
YHL Inheritance (2) 18
YHL Inheritance (2) 19
YHL Inheritance (2) 20
upcastingp g
YHL Inheritance (2) 21
YHL Inheritance (2) 22
Operator and Polymorphism?Operator and Polymorphism?
YHL Inheritance (2) 23
Person * p1 = new Manager;p1 -> print();cout << (* p1) << endl;
YHL Inheritance (2) 24
YHL Inheritance (2) 25
YHL Inheritance (2) 26
YHL Inheritance (2) 27
ECE 462Object-Oriented Programming
using C++ and Java
Virtual Function (2)
YHL Virtual Function (2) 1
Virtual Function (2)
Yung-Hsiang [email protected]
YHL Virtual Function (2) 2
address instruction
YHL Virtual Function (2) 3
How Do Function Call Work?
Function call: change the address of the program counter.
1. push the current address at the call stack
address instruction
0X40096a0
0X40096a4
0X40096a8
YHL Virtual Function (2) 4
the call stack2. change program counter3. execute the instruction in
the called function4. pop the call stack and
restore the program counter
0X40096ac
0X40096b0
0X40096b4
0X40096b8 call
0X40096bc
virtual in C++
• allow polymorphism: a derived class to change the behavior (“override”, “new implementation”). a virtual method is virtual for all derived classes.
• If a derived class can use the same method, do not override the method.
YHL Virtual Function (2) 5
• A virtual method must have the same prototype (i.e. return type and argument types).
• virtual ⇒ derived class may (not have to) override• not virtual ⇒ should not override, compiler will allow, but
don’t ask for trouble• why virtual in C++? improve performance ... but ... cause
too much confusion
YHL Virtual Function (2) 6
YHL Virtual Function (2) 7
YHL Virtual Function (2) 8
How Does virtual Work?
class Base {virtual void xxx() { /* xxx1 */ }void yyy() { /* yyy1 */ }
};class Derived: public Base {
Base * bobj ⇒ NVF ← yyy1VF ← unknownbobj = new Base(...)VF ← xxx1;bobj = new Derived(...);VF ← xxx2;
YHL Virtual Function (2) 9
object’s storageattributes
non-virtual functions: locations of functions (NVF)
virtual functions: pointers to be assigned at run time (VF)
void xxx() { /* xxx2 */ }};
VF ← xxx2;
In general, all C++ methodsshould be virtual, unless you have strong reasonsto make them not virtual.
All Java methods are virtual.
Calling Virtual Functions inside Overloaded Functions
YHL Virtual Function (2) 10
YHL Virtual Function (2) 11
YHL Virtual Function (2) 12
ECE 462Object-Oriented Programmingj g g
using C++ and Java
Private and Protected Inheritance
Yung-Hsiang Lul @ d [email protected]
YHL Private Inheritance 1
class Derived: class Derived: class Derived:class Derived: public Base {
class Derived: protected Base {
class Derived: private Base {
interface + implementation implementationimplementationBase * bptr;bptr = new Derived ...
not allowed not allowedp
public member →public
public member →protected
public member →private
t t d b t t d b t t d bprotected member →protected
protected member →protected
protected member →private
private member → same samepinaccessible
override virtual function same same
YHL Private Inheritance 2
Private Inheritance as Composition
YHL Private Inheritance 3
YHL Private Inheritance 4
YHL Private Inheritance 5
YHL Private Inheritance 6
Protected Inheritance as RoleProtected Inheritance as Role
YHL Private Inheritance 7
YHL Private Inheritance 8
YHL Private Inheritance 9
YHL Private Inheritance 10
YHL Private Inheritance 11
ECE 462Object-Oriented Programmingj g g
using C++ and Java
Java Final and Finally
Yung-Hsiang Lul @ d [email protected]
YHL Final and Finally 1
Java "final"Java final
create a constant: (15.18)create a constant: (15.18)
interface A { public static final double PI = 3.14159; }interface A { public static final double PI 3.14159; }class X {
double x = A.PI}
YHL Final and Finally 2
YHL Final and Finally 3
YHL Final and Finally 4
constant parameter: (9.8)
void foo (final int x) {x = 100; // error
}
block inheritance (class): (3.6)
final class X {}}class Y extends X { // error}
YHL Final and Finally 5
block overriding (method): (3.6)block overriding (method): (3.6)
class X {{final public void foo () { }
}
class Y extends X {public void foo () { } // error
}
YHL Final and Finally 6
Java "finally"Java finally
exception handling: (10.5)exception handling: (10.5)
try { ...try { ... } catch (exception_type1 ide1) { ...} catch (exception_type2 ide2) { ... } finally { ...
// always execute, regardless of exceptions}
YHL Final and Finally 7
Java “finalize”Java finalize
Called before an object’s memory is reclaimed by the garbage collector: (11.9)
//GC.javaclass X {class X {
int id;static int nextId = 1;public X() { id = nextId++; }protected void finalize() throws Throwable {
if ( id%1000 == 0 ) { System.out.println("Finalization of X object, id = " + id); }
super.finalize(); }
YHL Final and Finally 8
}}
class Test {public static void main( String[] args ) {
X[] xarray = new X[ 10000 ];X[] xarray = new X[ 10000 ]; for (int i = 0; i < 10000; i++ )
xarray[i] = new X(); llxarray = null;
System.gc(); }
}
YHL Final and Finally 9