Inheritance and Function Overloading in C++: Lecture 28, Slides of Computer Engineering and Programming

An introduction to inheritance and function overloading in c++. Inheritance is a way to define objects in terms of hierarchical classes, where each level inherits the attributes of the level above it. Function overloading allows writing multiple functions with the same name but different argument lists, enabling the same function to perform similar tasks on different data types. The document also covers function templates and operator overloading.

Typology: Slides

2012/2013

Uploaded on 05/06/2013

apsara
apsara 🇮🇳

4.5

(2)

86 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Inheritance and Overloading
Lecture 28
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Inheritance and Function Overloading in C++: Lecture 28 and more Slides Computer Engineering and Programming in PDF only on Docsity!

Inheritance and Overloading

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)

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

Public Inheritance

public base class (B)

public members protected members private members

derived class (A) public protected inherited but not accessible

class A : public B { // Class A now inherits the members of Class B // with no change in the “access specifier” for } // the inherited members

Private Inheritance

private base class (B)

public members protected members private members

derived class (A) private private inherited but not accessible

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 };

Inheritance (continued)

int main ( )

{

Square mySquare; Cube myCube;

mySquare.getColor ( ); // Square inherits getColor() mySquare.getArea ( ); myCube.getColor ( ); // Cube inherits Docsity.com

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. Docsity.com

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; }

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 aDocsity.com

Function Templates

template void swap (T *a, T *b)

{

T temp; temp = *a; *a = *b; *b = temp;

}

T is a “dummy” type that will be filled in by the compiler as needed

a and b are of “type” T temp is of “type” T swap is a function template, NOT a function

int main ( )Function Templates

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 Docsity.com

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.

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 Docsity.com