Introduction To C++ Dynamic Binding and User Defined Type Conversions
Introduction To C++ Dynamic Binding and User Defined Type Conversions
Dynamic Binding
and User Defined Type
Conversions
Topic #4
CS202 4- 1
Lectures #7, 8
Dynamic Binding
Virtual Functions
Abstract Base Classes
Pointers to Base Class Objects
How this relates to Pointers to Functions
User Defined Type Conversions
Type Conversion with User Defined Types
Casting, Functional Notation, Static Cast, Dynamic
Cast
RTTI (Run Time Type Identification)3
CS202 4- 2
Static Binding
CS202 4- 3
Static Binding
account
savings::statement()
interest
student object
checking
student
CS202 4- 4
savings
account::statement()
name[]
balance
checking::statement()
charges
student::statement()
school[]
Static Binding
CS202 4- 5
Static Binding
student smith("Joe Smith", 5000, "UT");
student s(smith); s.statement();
checking c;
c = s;
account a;
a = s;
CS202 4- 6
c.statement();
a.statement();
Static Binding
checking
c = s;
c;
student
s;
account::statement()
name[]
balance
account::statement()
name[]
balance
checking::statement()
charges
checking::statement()
charges
student::statement()
school[]
account
a = s;
a;
account::statement()
name[]
balance
student
s;
account::statement()
name[]
balance
checking::statement()
charges
student::statement()
school[]
CS202 4- 7
Static Binding
Static binding guarantees that we will never associate a
member function of a derived class with an object of a
direct or indirect base class.
If that were to happen, the member function would
attempt to access data members that do not exist in the
object. That is because a base class object does not have
an "is a" relationship with a derived class object.
Of course, we can go the other way around. That is, we
can associate a member function of a direct or indirect
base class with an object of a derived class as long as
that member function is accessible (i.e., public).
That is what inheritance is all about and it works
4- 8 because we have an "is a" relationship.
CS202
Upcasting
CS202 4- 9
Upcasting
void print_account(account* p) { //pointer
p->statement();
}
void print_account(account &r) { //reference
r.statement();
}
int main() {
student smith("Joe Smith", 5000, "UT");
student* ps = &smith;
ps->statement();
checking* pc = &smith;
pc->statement();
account* pa = &smith;
pa->statement();
Upcasting
CS202 4- 11
CS202 4- 12
Dynamic Binding
CS202 4- 13
Overriding...What is it?
CS202 4- 14
Overriding...What is it?
CS202 4- 15
Overriding...What is it?
Overriding:
Defining
CS202 4- 16
Overriding...4 Requirements
CS202 4- 17
CS202 4- 20
CS202 4- 21
CS202 4- 22
CS202 4- 23
pc->statement();
account* pa = &smith;
pa->statement();
CS202 4- 24
CS202 4- 25
CS202 4- 26
CS202 4- 27
CS202 4- 28
CS202 4- 29
a c c o u n t * b a n k [ 4 ]s;a v i n g s i ; s a v i n g s : : v t b l
bank[0] = &i;
bank[1] = &a;
vptr
savings::statement()
bank[2] = &s;
bank[3] = &c;
name[]
. . .
balance
interest
account
a ; account::vtbl
vptr
account::statement()
name[]
balance
student
s ; student::vtbl
vptr
student::statement()
name[]
balance
charges
school[]
checking
c ;c h e c k i n g : : v t b l
vptr
checking::statement()
name[]
balance
charges
CS202 4- 31
CS202
CS202 4- 33
Use of Protected...
class account {
public:
account(const char* ="none", float=0);
virtual void statement();
private:
...
};
class checking : public account {
...
protected:
void statement();
};
class student : public checking {
...
protected:
void statement();
};
app:
student smith("Kyle smith", 5000, "UT");
account* pa = &smith;
pa->statement(); //okay
CS202 4- 34
CS202 4- 35
Virtual Friends
CS202 4- 36
Virtual Friends
class account {
friend std::ostream &operator<<(std::ostream &,account&);
protected:
virtual void statement(std::ostream &);
...
};
class checking : public account {
...
protected:
void statement(std::ostream &); //helper fnct
};
class student : public checking {
...
protected:
void statement(std::ostream &); //helper fnct
};
CS202 4- 37
Virtual Friends
void account::statement(ostream &o) {
o <<"Account Statement" <<endl;
o <<" name = " <<name <<endl;
o <<" balance = " <<balance <<endl;
}
void checking::statement(ostream &o) {
o <<"Checking "; account::statement(o);
o <<" charges = " <<charges <<endl;
}
void student::statement(ostream &o) {
o <<"Student "; checking::statement(o);
o <<" school = " <<school <<endl;
}
ostream &operator<<(ostream &o, account &a) {
a.statement(o); return (o);
}
app:
student smith("Kyle smith", 5000, "UT");
account &ra(smith); //reference
account* pa(&smith); //pointer
cout <<ra <<*pa; //both use the student
CS202 4- 38
Virtual Destructors
CS202 4- 39
CS202 4- 40
CS202 4- 41
Introduction to C++
Run Time
Type Ident.
CS202 4- 42
RTTI
CS202 4- 43
CS202 4- 44
CS202 4- 45
CS202 4- 46
CS202 4- 47
CS202 4- 48
Recommendations w/ RTTI
CS202 4- 49
Introduction to C++
Abstract
Base Classes
CS202 4- 50
CS202 4- 51
CS202 4- 52
Introduction to C++
User
Defined
Conversions
CS202 4- 53
Implicit Conversions
CS202 4- 54
Implicit Conversions
CS202 4- 55
Explicit Conversions
CS202 4- 56
CS202 4- 58
CS202 4- 59
CS202 4- 60
CS202 4- 61
CS202 4- 63
CS202 4- 64
CS202 4- 65