Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ Templates: Generic Programming and Data Structures, Study notes of Construction

The concept of templates in C++ and how they support generic programming. Templates provide a simple way to represent general concepts and combine them with various data types. They offer advantages such as more general code, less bugs, and no runtime overhead. the use of templates, template instantiation, and template constructors and member functions.

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

ekanaaa
ekanaaa 🇺🇸

4.3

(28)

268 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
OOD and C++
Section 5: Templates
Templates - Generic Programming
Decide which algorithms you want:
parameterize them so that they work for
a wide-range of types & data structures
Templates in C++ support generic progarmming
Templates provide:
•simple way to represent general concepts
•simple way to combine concepts
Use ‘data-types’ as parameters at compilation
Advantage:
• more general ‘generic code’
• reuse same code for different data types
• less bugs - only implement/debug one piece of
code
• no overhead on run-time efficiency compared to
data specific code
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download C++ Templates: Generic Programming and Data Structures and more Study notes Construction in PDF only on Docsity!

OOD and C++

Section 5: Templates

Templates - Generic Programming

a wide-range of types & data structuresparameterize them so that they work for Decide which algorithms you want:

Templates provide:Templates in C++ support generic progarmming

•simple way to combine concepts•simple way to represent general concepts

Advantage:Use ‘data-types’ as parameters at compilation

data specific code• no overhead on run-time efficiency compared tocode• less bugs - only implement/debug one piece of• reuse same code for different data types• more general ‘generic code’

Use of Templates

Widely used for container classes: Make the definition of your class as broad as possible!

  • C++ Standard Template Library (Arrays,Lists,…..)• Storage independent of data type

Encapsulation:

interfaceHide a sophisticated implementation behind a simple

Templates in C++

template class mytemplate

public:

void function();mytemplate();

protected:

int protected_function();

private:

double private_data;

template class declaration: mytemplate.hh

template :

  • DataC can be any type name:• type parameter ‘DataC’ (generic class) will be used• specifies a template is being declared

class, built in type, typedef

used in template implementation.• DataC must contain data/member functions which are

Template Instantiation

template class

class

class

class

class< >

Instantiations Object code does not exist until template is instantiated •template is included as a header file

Template is instantiated at compile time

  • ‘Specialisation’ ie define class of a specific typemember functions which are used • Instantiation generates code for constructor/destructor &• (^) No run-time overhead

Warning: Do not generate different templates for objects withErrors in template not detected until instantiation

Results in ‘code-bloat’ => big increase in executable size.only minor differences

Template Instantiation

template class mytemplate { private:public:

// define my template member functions here};

mytemplate.hh

void myprogram(){#include “mytemplate.hh” mytemplate dt; // instantiate double versionmytemplate it; // instantiate integer version

myprogram.cc

class myderivedclass: public mytemplate {#include “mytemplate.hh(2) Instantiation via inheritance(1) Instantiate by declaring a variable private:public:

myderivedclass.hh

Inheritance works as before: derived class sees public/protectedTemplate is instantiated when class is compiled => object code

Note: as before for classes. All constructors/destructors/member functions declared Template Constructors & Member Functions

or in a file .tcc included at end of header file^ Definition is in the template header file after declaration

{ : // setup member data from argstemplate mytemplate::mytemplate(args)// constructor// template declaration

// other initialisation

template int mytemplate::function(){// function} // perform some operation

int mytemplate::otherfunction(){inline template// inline function} // perform operation - no overhead of function call

mytemplate.hh

Only Constructors/Member Functions which are used are instantiated

Using Templates

void myprogram(){#include “String.hh”#include “mytemplate.hh”Member functions are used as before Constructors declare type parameter: class, typedef, struct

as = ts.function(); // member functionStringC as;mytemplate ts(100); // constructor

myprogram.cc

Object code isProgram instantiates construction and function()

(^) only (^) compiled for constructor and function()

Template code for otherfunction() is

(^) not (^) compiled

Function Templates

Specify a generic function •avoids run-time overheads of type checking or virtual function•works for a broad range of types

void swap(DataC& a, DataC& b){template// function definitiontemplate void swap(DataC& a, DataC& b);// function declaration b=temp;a=b;DataC temp(a);

swap.hh

void myprogram(){#include “swap.hh” swap(i,j);double u(5.5), v(9.8);int i(5), j(10);

// j=5 i=

swap(i,u);swap(u,v); // u=9.8 v=5.

(^) // invalid different types - implicit cast

myprogram.cc

More Templates...

(1) Template with multiple parameters

•example associative array

(2) Templates of Templates };template<class A, class B> class mytemplate {

•example: array of arrays (2d array)

};template<template > class mytemplate {

Designing & Implementing Templates

(2) Use templates to define common code for different (1) Design your classes to be general ‘generic’

data types ie container classes

(3) Use templates with inheritance to define specific classes

with common representations

(4) Develop template code as a specific type

=> for debugging

(5) Remember: template code for a member function

instantiated as a specific typeis only compiled when a template is

(6) Use a separate header file template declaration and

implementation .hh & .tcc

Using separate files .hh & .tcc

template declaration & implementation is a

header file

…..} template mytemplate::mytemplate(){// definition};template class mytemplate{//declaration - template is included & compiled when instantiated

mytemplate.hh

single header file

OR

header file for declaration

mytemplate.hh

#include “mytemplate.tcc”};template class mytemplate{

// include file for definition

header file for definition

mytemplate.tcc

  • used in examples• simplifies structure (similar to .hh +.cc)• separates ‘user-interface’ from implementation} template mytemplate::mytemplate(){