Print An Array-Object Oriented Programming-Lecture Slides, Slides of Object Oriented Programming

Main topics in this course are object-orientation, objects and classes, overloading, inheritance, polymorphism, generic programming, exception handling, introduction to design patterns. This lecture includes: Print, Array, Object, Program, Oriented, Genetic, Increment, Decrement, Return, Main, Operator, Find, Array

Typology: Slides

2011/2012

Uploaded on 08/08/2012

anchita
anchita 🇮🇳

4.4

(7)

113 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object
Object-
-Oriented Programming
Oriented Programming
(OOP)
(OOP)
Lecture No. 34
Lecture No. 34
Generic Algorithms
Generic Algorithms
A Case Study
A Case Study
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Print An Array-Object Oriented Programming-Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object

Object

Oriented Programming

Oriented Programming

(OOP)

(OOP)

Lecture No. 34

Lecture No. 34

Print an Array

Print an Array

template< typename T > template< typename T >

void printArray( T* array, int size ) void printArray( T* array, int size )

for ( int i = 0; i < size; i++ )

for ( int i = 0; i < size; i++ )

cout << array[ i ] <<

cout << array[ i ] << “

Generic Algorithms

Generic Algorithms

template< typename T >

template< typename T >

T* find( T* array, T* beyond,T* find( T* array, T* beyond,

const T& x ) {

const T& x ) {

T* p = array; T* p = array;

while ( p != beyond ) {

while ( p != beyond ) {

if ( *p == x )if ( *p == x )

return p;

return p;

p++;p++;

return return beyondbeyond;;

Generic Algorithms

Generic Algorithms

This algorithm works fine for arrays of any

This algorithm works fine for arrays of any

type

type

We can make it work for any generic

We can make it work for any generic

container that supports two operations

container that supports two operations

Increment operator (++)

Increment operator (++)

Dereference operator (*)

Dereference operator (*)

Example

Example

Class Template

Class Template

► ►

A

A

Vector

Vector

class template can store data

class template can store data

elements of different types

elements of different types

Without templates, we need a separate

Without templates, we need a separate

Vector

Vector

class for each data type

class for each data type

Example

Example

Class Template

Class Template

const Vector< T >& operator =( const Vector< T >& operator =(

const Vector< T >& ); const Vector< T >& );

T& operator ; T& operator ;

Example

Example

Class Template

Class Template

template< class T > template< class T >

const Vector& Vector::operator const Vector& Vector::operator

=( const Vector& right) {

=( const Vector& right) {

if ( this != &right ) {

if ( this != &right ) {

delete [] ptr;

delete [] ptr;

size = right.size; size = right.size;

Example

Example

Class Template

Class Template

template< class T >

template< class T >

T& Vector< T >::operator []( T& Vector< T >::operator [](

int index ) {

int index ) {

if ( index < 0 || index >= size ) { if ( index < 0 || index >= size ) {

cout <<

cout << “

Error: index out of

Error: index out of

range range\nn””;;

exit( 1 ); exit( 1 );

return ptr[index]; return ptr[index];

} } docsity.com