Motivation - Object Oriented Programming - Lecture Slides, Slides of Object Oriented Programming

Motivation, Prints an array, Integer elements, Array of characters, Array of doubles, Array of integers, Wraps arrays, Arrays of boolean variables, Generic Programming are the points you can learn in this object oriented programming subject.

Typology: Slides

2011/2012

Uploaded on 11/09/2012

bacha
bacha 🇮🇳

4.3

(41)

213 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object-Oriented Programming
(OOP)
Lecture No. 32
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Motivation - Object Oriented Programming - Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming

(OOP)

Lecture No. 32

Motivation

  • Following function prints an array of

integer elements:

void printArray(int* array, int size) { for ( int i = 0; i < size; i++ ) cout << array[ i ] << “, ”; }

...Motivation

  • What if we want to print an array of

doubles?

void printArray(double* array, int size) { for ( int i = 0; i < size; i++ ) cout << array[ i ] << “, ”; }

...Motivation

  • Now if we want to change the way function

prints the array. e.g. from

to

...Motivation

  • What if we want to use an Array class that

wraps arrays of double?

class Array { double* pArray; int size; public: … };

...Motivation

  • What if we want to use an Array class that

wraps arrays of boolean variables?

class Array { bool* pArray; int size; public: … };

Generic Programming

  • Generic programming refers to programs

containing generic abstractions

  • A generic program abstraction (function,

class) can be parameterized with a type

  • Such abstractions can work with many

different types of data

Advantages

  • Reusability
  • Writability
  • Maintainability

Function Templates

  • A function template can be parameterized

to operate on different types of data

Declaration

template< class T > void funName( T x ); // OR template< typename T > void funName( T x ); // OR template< class T, class U, … > void funName( T x, U y, … );

…Example – Function Templates

int main() { int iArray[5] = { 1, 2, 3, 4, 5 }; void printArray( iArray, 5 ); // Instantiated for int[] char cArray[3] = { ‘a’, ‘b’, ‘c’ }; void printArray( cArray, 3 ); // Instantiated for char[] return 0; }

Explicit Type Parameterization

  • A function template may not have any

parameter

template T getInput() { T x; cin >> x; return x; }

…Explicit Type

Parameterization

int main() { int x; x = getInput< int >(); double y; y = getInput< double >(); }

User-defined Specializations

  • A template may not handle all the types

successfully

  • Explicit specializations need to be

provided for specific type(s)