Professional Documents
Culture Documents
C++ Inheritance1
C++ Inheritance1
C++ Inheritance I
CSE 333 Fall 2022
Teaching Assistants:
Nour Ayad Frank Chen Nick Durand
Dylan Hartono Humza Lala Kenzie Mihardja
Bennedict Soesanto Chanh Truong Justin Tysdal
Tanay Vakharia Timmy Yang
L18: C++ Inheritance I CSE333, Fall 2022
Administrivia
v New exercise (ex 12) out today., due Wed. morning
§ wordcount! (and stl map makes it very short!!)
2
L18: C++ Inheritance I CSE333, Fall 2022
Lecture Outline
v C++ Inheritance
§ Review of basic idea
§ Dynamic Dispatch
§ vtables and vptr
3
L18: C++ Inheritance I CSE333, Fall 2022
4
L18: C++ Inheritance I CSE333, Fall 2022
§ Redundant!
§ Cannot treat multiple investments together
• e.g. can’t have an array or vector of different assets
Inheritance
v A parent-child “is-a” relationship between classes
§ A child (derived class) extends a parent (base class)
v Benefits:
§ Code reuse
• Children can automatically inherit code from parents
§ Polymorphism
• Ability to redefine existing behavior but preserve the interface
• Children can override the behavior of the parent
• Others can make calls on objects without knowing which part of the
inheritance tree it is in
§ Extensibility
• Children can add behavior
7
L18: C++ Inheritance I CSE333, Fall 2022
Terminology
Java C++
Superclass Base Class
Subclass Derived Class
8
L18: C++ Inheritance I CSE333, Fall 2022
GetMarketValue()
GetProfit()
GetCost()
Stock
symbol_ Cash
total_shares_
amount_
total_cost_
current_price_ GetMarketValue()
GetMarketValue() DividendStock
GetProfit() symbol_
GetCost() total_shares_
total_cost_
current_price_
dividends_
GetMarketValue()
GetProfit()
GetCost()
9
L18: C++ Inheritance I CSE333, Fall 2022
10
L18: C++ Inheritance I CSE333, Fall 2022
Back to Stocks
DividendStock
Stock
symbol_
symbol_
total_shares_
total_shares_
total_cost_
total_cost_
current_price_
current_price_
dividends_
GetMarketValue()
GetMarketValue()
GetProfit()
GetProfit()
GetCost()
GetCost()
BASE DERIVED
12
L18: C++ Inheritance I CSE333, Fall 2022
Back to Stocks
Stock DividendStock
Stock
symbol_ dividends_
symbol_
total_shares_ total_shares_
total_cost_ total_cost_
current_price_
GetMarketValue()
current_price_
GetProfit()
GetMarketValue()
GetMarketValue() GetProfit() GetCost()
GetProfit() GetCost() PayDividend()
GetCost()
v A derived class:
§ Inherits the behavior and state (specification) of the base class
§ Overrides some of the base class’ member functions (opt.)
§ Extends the base class with new member functions, variables
(opt.)
13
L18: C++ Inheritance I CSE333, Fall 2022
DividendStock dividend();
DividendStock* ds = ÷nd;
Stock* s = ÷nd; // why is this allowed?
// Invokes DividendStock::GetMarketValue()
ds->GetMarketValue();
// Invokes DividendStock::GetMarketValue()
s->GetMarketValue();
17
L18: C++ Inheritance I CSE333, Fall 2022
Most-Derived
class A {
public:
// Foo will use dynamic dispatch
virtual void Foo(); void Bar() {
}; A* a_ptr;
C c;
class B : public A {
public: a_ptr = &c;
// B::Foo overrides A::Foo
virtual void Foo(); // Whose Foo() is called?
}; a_ptr->Foo();
}
class C : public B {
// C inherits B::Foo()
};
18
L18: C++ Inheritance I CSE333, Fall 2022
Your Turn!
class A {
v Which Foo() is called? public:
virtual void Foo();
};
Q1 Q2 void Bar() { class B : public A {
A A A* a_ptr;
C c;
public:
virtual void Foo();
B B E e; };
22
L18: C++ Inheritance I CSE333, Fall 2022
vtable/vptr Example
class Base { Base b;
public: Der1 d1;
virtual void f1(); Der2 d2;
virtual void f2();
}; Base* b0ptr = &b;
Base* b1ptr = &d1;
class Der1 : public Base { Base* b2ptr = &d2;
public:
virtual void f1(); b0ptr->f1(); // Base::f1()
}; b0ptr->f2(); // Base::f2()
24
L18: C++ Inheritance I CSE333, Fall 2022
vtable/vptr Example
Base b;
object class compiled
instances vtables code Der1 d1;
Der2 d2;
Base Base::f1()
f1() push %rbp Base* bptr = &d1;
b vptr
f2() ...
bptr->f1();
// bptr -->
Base::f2()
// d1.vptr -->
push %rbp
Der1 // Der1.vtable.f1 -->
...
f1() // Der1::f1()
d1 vptr
f2() Der1::f1() bptr = &d2;
push %rbp
...
bptr->f1();
Der2 // bptr -->
Der2::f2() // d2.vptr -->
d2 vptr f1() push %rbp
f2() // Der2.vtable.f1 -->
... // Base::f1()
25
L18: C++ Inheritance I CSE333, Fall 2022
More to Come…
Next time…
27