You are on page 1of 21

Engineering H192 - Computer Programming

Inheritance and Overloading

Lecture 28

Winter Quarter

The Ohio State University Gateway Engineering Education Coalition

Lect 28

P. 1

Engineering H192 - Computer Programming

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:

Winter Quarter

The Ohio State University Gateway Engineering Education Coalition

Lect 28

P. 2

Engineering H192 - Computer Programming

Inheritance (continued)

This hierarchy could, of course, be continued for more levels. Each level inherits the attributes of the above level. Shape is the base class. 2-D and 3-D are derived from Shape and Circle, Square, and Triangle are derived from 2-D. Similarly, Sphere, Cube, and Tetrahedron are derived from 3-D.

Winter Quarter

The Ohio State University Gateway Engineering Education Coalition

Lect 28

P. 3

Engineering H192 - Computer Programming

Inheritance (continued)
class A : base class access specifier B { member access specifier(s): ... member data and member function(s); ... } Valid access specifiers include public, private, and protected
Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 28 P. 4

Engineering H192 - Computer Programming

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

public base class (B) public members protected members private members
Winter Quarter

derived class (A) public protected inherited but not accessible


Lect 28 P. 5

The Ohio State University Gateway Engineering Education Coalition

Engineering H192 - Computer Programming

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

protected base class (B) public members protected members private members
Winter Quarter

derived class (A) protected protected inherited but not accessible


Lect 28 P. 6

The Ohio State University Gateway Engineering Education Coalition

Engineering H192 - Computer Programming

Private Inheritance
class A : private B { // Class A now inherits the members of Class B // with public and protected members } // promoted to private

private base class (B) public members protected members private members
Winter Quarter

derived class (A) private private inherited but not accessible


Lect 28 P. 7

The Ohio State University Gateway Engineering Education Coalition

Engineering H192 - Computer Programming

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 Gateway Engineering Education Coalition Lect 28 P. 8

Inheritance (continued)

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; };
Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 28 P. 9

Engineering H192 - Computer Programming

Inheritance (continued)
int main ( ) { Square mySquare; Cube myCube; mySquare.getColor ( ); // Square inherits getColor() mySquare.getArea ( ); myCube.getColor ( ); // Cube inherits getColor() myCube.getVolume ( ); }
Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 28 P. 10

Engineering H192 - Computer Programming

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 The advantage is that the same apparent function can be called to perform similar but different tasks. The following will show an example of this.
Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 28 P. 11

Engineering H192 - Computer Programming

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) ; }
Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 28 P. 12

Engineering H192 - Computer Programming

Function Overloading
void swap (int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } void swap (float *c, float *d) { float temp; temp = *c; *c = *d; *d = temp; } void swap (char *p, char *q) { char temp; temp = *p; *p = *q; *q = temp; }

Winter Quarter

The Ohio State University Gateway Engineering Education Coalition

Lect 28

P. 13

Engineering H192 - Computer Programming

Function Templates
We have discussed overloaded functions as a way to perform similar operations on data of different types. The swap functions were an example. We wrote three functions with the same name but different data types to perform the swap operations. Then we could call swap (&a, &b), for example, and C++ would select which function to use by matching the data type of a and b to one of the functions.
Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 28 P. 14

Engineering H192 - Computer Programming

Function Templates
Another way to perform this task would be to create a function template definition. With a function template defined, when we call swap (&a, &b), C++ will generate the object code functions for us. The program on the following slides is an example.

Winter Quarter

The Ohio State University Gateway Engineering Education Coalition

Lect 28

P. 15

Engineering H192 - Computer Programming

Function Templates
template <typename T> void swap (T *a, T *b) { T is a dummy type that will be T temp; filled in by the compiler as temp = *a; needed *a = *b; a and b are of type T *b = temp; temp is of type T } swap is a function template, NOT a function
Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 28 P. 16

Engineering H192 - Computer Programming

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 Gateway Engineering Education Coalition Lect 28 P. 17

Function Templates

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 Gateway Engineering Education Coalition Lect 28 P. 18

Engineering H192 - Computer Programming

Operator Overloading (continued)


Operator overloading means that the operators: Have multiple definitions that are distinguished by the types of their parameters, and When the operator is used, the C++ compiler uses the types of the operands to determine which definition should be used.

Winter Quarter

The Ohio State University Gateway Engineering Education Coalition

Lect 28

P. 19

Engineering H192 - Computer Programming

Operator Overloading (continued)


A programmer has the ability to re-define or change how the operators (+, -, *, /, =, <<, >>, etc.) work on their own classes. Overloading operators usually consists of defining a class member function called operator+ (where + is any operator). Note that operator is a reserved word in C++. If anything usually follows that operator, it is passed to the function. That function acts exactly like any other member function; it has the same scope as other member functions and can return a value just like any other member function.
Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 28 P. 20

Engineering H192 - Computer Programming

Operator Overloading (continued)


Steps for defining an overloaded operator: 1. Name the operator being overloaded. 2. Specify the (new) types of parameters (operands) the operator is to receive. 3. Specify the type of value returned by the operator. 4. Specify what action the operator is to perform.
Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 28 P. 21

You might also like