-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
1/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
"nheritane and Over!oading
#eture 2$
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
2/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
"nheritane
• O%&ets are o'ten de'ined in terms o' hierarhia!!asses with a %ase !ass and one or more !eve!so' !asses that inherit 'rom the !asses that area%ove it in the hierarhy(
• )or instane* graphis o%&ets might %e de'ined as'o!!ows+
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
3/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
"nheritane ,ontinued
• This hierarhy ou!d* o' ourse* %e ontinued 'or more
!eve!s(
• Eah !eve! inherits the attri%utes o' the a%ove !eve!( Shape
is the %ase !ass( 2-. and /-. are derived 'rom Shape andCir!e* S0uare* and Triang!e are derived 'rom 2-.( Simi!ar!y*
Sphere* Cu%e* and Tetrahedron are derived 'rom /-.(
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
4/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
"nheritane ,ontinued
!ass A + %ase !ass aess spei'ier B
mem%er aess spei'ier,s+
(((mem%er data and mem%er 'untion,s
(((
3
4a!id aess spei'iers in!ude pu%!i* private* and
proteted
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
5/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
Pu%!i "nheritane
pu%!i %ase !ass ,5
public membersprotected members
private members
derived !ass ,6
publicprotected
inherited but not
accessible
!ass A + pu%!i B
// Class A now inherits the members of Class B
// with no change in the “access specifier” for
3 // the inherited members
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
6/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
Proteted "nheritane
proteted %ase !ass ,5
public membersprotected members
private members
derived !ass ,6
protectedprotected
inherited but not
accessible
!ass A + proteted B
// Class A now inherits the members of Class B
// with pu%!i members “promoted” to proteted
3 // but no other changes to the inherited members
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
7/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
Private "nheritane
private %ase !ass ,5
public membersprotected members
private members
derived !ass ,6
privateprivate
inherited but not
accessible
!ass A + private B
// Class A now inherits the members of Class B
// with pu%!i and proteted members
3 // “promoted” to private
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
8/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
"nheritane ,ontinued!ass Shape
pu%!i+
int GetColor ,
proteted+ // so derived classes can access it
int color 3
!ass Two! + pu%!i Shape
// put members specific to "! shapes here
3
!ass Three! + pu%!i Shape
// put members specific to #! shapes here
3
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
9/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
"nheritane ,ontinued
!ass S$uare + pu%!i Two!
pu%!i+
'!oat getArea ,
proteted+
'!oat edgelength
3
!ass Cube + pu%!i Three!
pu%!i+
'!oat get%olume ,
proteted+
'!oat edgelength
3
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
10/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
"nheritane ,ontinued
int main ,
S$uare m&S$uare
Cube m&Cube
m&S$uare(getColor , // S$uare inherits getColor'(
m&S$uare(getArea ,
m&Cube(getColor , // Cube inherits getColor'(
m&Cube(get%olume ,
3
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
11/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
)untion Over!oading
• C77 supports writing more than one 'untion with
the same name %ut di''erent argument !ists( This
ou!d in!ude+
) di''erent data types
) di''erent num%er o' arguments
• The advantage is that the same apparent 'untion
an %e a!!ed to per'orm simi!ar %ut di''erenttas8s( The 'o!!owing wi!! show an eamp!e o'
this(
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
12/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
)untion Over!oading
void swap ,int *a+ int *b
void swap ,'!oat *c+ '!oat *d
void swap ,har *p+ har *$
int main ,
int a , -+ b , .
'!oat c , .01+ d , 21034
har p , 565 + $ , 5n5
swap ,7a+ 7b
swap ,7c+ 7d
swap ,7p+ 7$
3
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
13/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
)untion Over!oading
void swap ,int *a+ int *b
int temp temp , *a *a , *b *b , temp 3
void swap ,'!oat *c+ '!oat *d
'!oat temp temp , *c *c , *d *d , temp 3
void swap ,har *p+ har *$ har temp temp , *p *p , *$ *$ , temp 3
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
14/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
)untion Temp!ates
• :e have disussed over!oaded 'untions as a way
to per'orm simi!ar operations on data o' di''erent
types( The swap 'untions were an eamp!e(
• :e wrote three 'untions with the same name %ut
di''erent data types to per'orm the swap
operations( Then we ou!d a!! swap ,;a* ;%* 'or
eamp!e* and C77 wou!d se!et whih 'untion touse %y mathing the data type o' a and % to one o'
the 'untions(
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
15/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
)untion Temp!ates
• 6nother way to per'orm this tas8 wou!d %e to
reate a function template de'inition(
• :ith a function template de'ined* when we a!!
swap ,;a* ;%* C77 wi!! generate the o%&et ode
'untions 'or us( The program on the 'o!!owings!ides is an eamp!e(
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
16/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
)untion Temp!ates
temp!ate
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
17/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
)untion Temp!ates
int main ,
int a , :+ b , .
'!oat c , 10.+ d , 403
har e , 565+ f , 5;5 swap ,7a+ 7b // compiler puts int in for T
swap ,7c+ 7d // compiler puts float in for T
swap ,7e+ 7f // compiler puts char in for T
out
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
18/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
Operator Over!oading
• C77 a!ready has a num%er o' types ,e(g(* int* '!oat*
har* et( that eah have a num%er o' %ui!t in
operators( )or eamp!e* a float an %e added to
another float and stored in yet another float with
use o' the 7 and > operators+'!oatC > '!oat6 7 '!oat5
• "n this statement* '!oat5 is passed to '!oat6 %y
way o' the 7 operator( The 7 operator 'rom '!oat6
then generates another float that is passed to'!oatC via the > operator( That new float is then
stored in '!oatC %y some method out!ined in the >
'untion(
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
19/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
Operator Over!oading ,ontinued
• Operator over!oading means that the operators+
) Have mu!tip!e de'initions that are
distinguished %y the types o' their parameters*and
) :hen the operator is used* the C77 ompi!er
uses the types o' the operands to determinewhih de'inition shou!d %e used(
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
20/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
Operator Over!oading ,ontinued
• 6 programmer has the a%i!ity to re-de'ine or hange how
the operators ,7* -* ?* @* >*
-
8/20/2019 Lecture 28 - C++ Inheritance&Overloading - 06
21/21
Lect 28
Engineering H192 - Computer Programming
Winter Quarter The Ohio State University
Gateway Engineering Eduation Coa!ition
Operator Over!oading ,ontinued
Steps 'or de'ining an over!oaded operator+
1( Aame the operator %eing over!oaded(
2( Spei'y the ,new types o' parameters ,operands
the operator is to reeive(
/( Spei'y the type o' va!ue returned %y theoperator(
B( Spei'y what ation the operator is to per'orm(