Member Templates-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: Member, Template, Float, Return, Class, Vector, Virtual, Const, Private, Public, Int

Typology: Slides

2011/2012

Uploaded on 08/08/2012

anchita
anchita 🇮🇳

4.4

(7)

113 documents

1 / 13

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. 35
Lecture No. 35
Member Templates
Member Templates
A class or class template can have member
A class or class template can have member
functions that are themselves templates
functions that are themselves templates
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Member Templates-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. 35

Lecture No. 35

Member Templates

Member Templates

template class Complex {

template class Complex {

T real, imag; T real, imag;

public: public:

// Complex( T r, T im )

// Complex( T r, T im )

Complex( T r, T im ) :

Complex( T r, T im ) :

real(r), imag(im) {}

real(r), imag(im) {}

// Complex(const Complex& c) // Complex(const Complex& c)

Complex(const Complex& c) :

Complex(const Complex& c) :

real( c.real ), imag( c.imag ) {}

real( c.real ), imag( c.imag ) {}

}; }; docsity.com

Member Templates

Member Templates

int main() { int main() {

Complex< float > fc( 0, 0 ); Complex< float > fc( 0, 0 );

Complex< double > dc = fc; // OK

Complex< double > dc = fc; // OK

return 0;

return 0;

Instantiation

Instantiation

class Complex class Complex {{

float float real, imag;real, imag;

public:

public:

Complex(

Complex( float

float r,

r, float

float im ) :

im ) :

real(r), imag(im) {}

real(r), imag(im) {}

// No Copy Constructor // No Copy Constructor

Class Template Specialization

Class Template Specialization

template<>

template<>

class Vector< char* > { class Vector< char* > {

private:

private:

int size; int size;

char** ptr;

char** ptr;

public: public:

// Vector< char* >( int = 10 );

// Vector< char* >( int = 10 );

Vector( int = 10 ); Vector( int = 10 );

Vector( const Vector< char* >& );

Vector( const Vector< char* >& );

virtual ~Vector(); virtual ~Vector();

docsity.com

Class Template Specialization

Class Template Specialization

template<>

template<>

Vector<char>::Vector(int s) {Vector<char>::Vector(int s) {

size = s;

size = s;

if ( size != 0 ) { if ( size != 0 ) {

ptr = new char*[size];

ptr = new char*[size];

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

ptr[i] = 0;ptr[i] = 0;

else else

ptr = 0;ptr = 0;

Class Template Specialization

Class Template Specialization

template<> template<>

int Vector<char>::getSize() const { int Vector<char>::getSize() const {

return size;

return size;

Class Template Specialization

Class Template Specialization

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

if ( size == 0 ) { if ( size == 0 ) {

ptr = 0;

ptr = 0;

return *this;

return *this;

ptr = new char[size]; ptr = new char[size];

Class Template Specialization

Class Template Specialization

int main() {

int main() {

Vector< char* > sv1( 2 ); Vector< char* > sv1( 2 );

sv1[0] =

sv1[0] = “

Aamir

Aamir ”

; // Error

; // Error

sv1.insert( "Aamir", 0 ); sv1.insert( "Aamir", 0 );

sv1.insert( "Nasir", 1 );

sv1.insert( "Nasir", 1 );

Vector< char* > sv2( sv1 ); Vector< char* > sv2( sv1 );

Vector< char* > sv3( 2 );

Vector< char* > sv3( 2 );

sv3 = sv1; sv3 = sv1;

return 0;

return 0;

docsity.com