Lecture 28 - C++ Inheritance& Overloading - 06
Lecture 28 - C++ Inheritance& Overloading - 06
Lecture 28
Inheritance
• Objects are often defined in terms of hierarchical
classes with a base class and one or more levels
of classes that inherit from the classes that are
above it in the hierarchy.
• For instance, graphics objects might be defined
as follows:
Inheritance (continued)
Inheritance (continued)
class A : base class access specifier B
{
member access specifier(s):
...
member data and member function(s);
...
}
Public Inheritance
class A : public B
{ // Class A now inherits the members of Class B
// with no change in the “access specifier” for
} // the inherited members
Protected Inheritance
class A : protected B
{ // Class A now inherits the members of Class B
// with public members “promoted” to protected
} // but no other changes to the inherited members
Private Inheritance
class A : private B
{ // Class A now inherits the members of Class B
// with public and protected members
} // “promoted” to private
Inheritance (continued)
class Shape
{
public:
int GetColor ( ) ;
protected: // so derived classes can access it
int color;
};
class Two_D : public Shape
{
// put members specific to 2D shapes here
};
class Three_D : public Shape
{
// put members specific to 3D shapes here
};
Winter Quarter The Ohio State University Lect 28 P. 8
Gateway Engineering Education Coalition
Engineering H192 - Computer Programming
Inheritance (continued)
class Square : public Two_D
{
public:
float getArea ( ) ;
protected:
float edge_length;
};
class Cube : public Three_D
{
public:
float getVolume ( ) ;
protected:
float edge_length;
};
Inheritance (continued)
int main ( )
{
Square mySquare;
Cube myCube;
Function Overloading
• C++ supports writing more than one function with
the same name but different argument lists. This
could include:
– different data types
– different number of arguments
Function Overloading
void swap (int *a, int *b) ;
void swap (float *c, float *d) ;
void swap (char *p, char *q) ;
int main ( )
{
int a = 4, b = 6 ;
float c = 16.7, d = -7.89 ;
char p = 'M' , q = 'n' ;
swap (&a, &b) ;
swap (&c, &d) ;
swap (&p, &q) ;
}
Function Overloading
Function Templates
Function Templates
Function Templates
Function Templates
int main ( )
{
int a = 5, b = 6;
float c = 7.6, d = 9.8;
char e = 'M', f = 'Z';
swap (&a, &b); // compiler puts int in for T
swap (&c, &d); // compiler puts float in for T
swap (&e, &f); // compiler puts char in for T
cout << "a=" << a << " and b=" << b << endl;
cout << "c=" << c << " and d=" << d << endl;
cout << "e=" << e << " and f=” << f << endl;
}
Winter Quarter The Ohio State University Lect 28 P. 17
Gateway Engineering Education Coalition
Engineering H192 - Computer Programming
Operator Overloading
• C++ already has a number of types (e.g., int, float,
char, etc.) that each have a number of built in
operators. For example, a float can be added to
another float and stored in yet another float with
use of the + and = operators:
floatC = floatA + floatB;
• In this statement, floatB is passed to floatA by
way of the + operator. The + operator from floatA
then generates another float that is passed to
floatC via the = operator. That new float is then
stored in floatC by some method outlined in the
= function.
Winter Quarter The Ohio State University Lect 28 P. 18
Gateway Engineering Education Coalition
Engineering H192 - Computer Programming