contents · 03/03/2018  · • a generic unit is a package or a subprogram that takes one or more...

Post on 07-Jul-2020

1 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Contents

• C Language features • C++ language features • Generic units • Generic data structures • Generic Algorithms • Generic modules • Higher levels of genericity

C Language Features

Encapsulation : • Unit of encapsulation here is a file. • extern • Local variables and Global variables provide

encapsulation

C Language Features

Interface and Implementation • C does not support separation of module from

its implementation, however an include file or header file declares all the symbols exported by modules to the caller thus providing an interface and implementation

• The header files enable the compiler to compile the module separately from other implementation files.

C Language Features

Interface and Implementation • The object module generated includes

unresolved references to those variables and functions imported via the header files.

• The linker combines several separately compiled modules into an executable module, resolving the references.

C Language Features Program Organization :

C++ Language Features

Encapsulation : • Unit of encapsulation here is a class. • Public, private Visibility modes provide

encapsulation • : : Scope- resolution operator

C++ Language Features

Interface and Implementation • C++ provides way of interface through the

concept of abstract class and pure virtual functions.

• Another way is through friend functions. • Namespaces also provide encapsulation and

thus concept of interface and implementation.

C++ Language Features

Program Organization :

Generic Units

• A generic unit is a package or a subprogram that

takes one or more generic formal parameters.

• A generic formal parameter is a value, a variable, a

constant, a type, a subprogram, or even an instance

of another generic unit.

• Some formal parameters can have default values.

• Ex: Templates in C++, Generics in Ada

Generic Data Structures : Class Templates in C++

Can also ‘generalize’ classes

template<class T>

• Can also apply to class definition

• All instances of ‘T’ in class definition replaced

by type parameter

• Just like for function templates!

Once template defined, can declare objects of

the class

Class Templates Syntax

Class Template Definition

template<class T> class Pair { public: Pair() { }; Pair(T firstVal, T secondVal); void setFirst(T newVal); void setSecond(T newVal); T getFirst() const; T getSecond() const; private: T first; T second; };

Class Template Definition

template<class T> Pair<T>::Pair(T firstVal, T secondVal) { first = firstVal; second = secondVal; }

Class Template Definition

template<class T> void Pair<T>::setFirst(T newVal) { first = newVal; }

template<class T>

void Pair<T>::setSecond(T newVal) { second = newVal; }

Class Template Definition

template<class T> void Pair<T>::getFirst() const { return first; }

template<class T>

void Pair<T>::getSecond() const { return second; }

Class Template Definition

void main() {

Pair<int> score; Pair<char> seats;

score.setFirst(3); score.setSecond(0); seats.setFirst(‘a’); seats.setSecond(‘b’); int r1 = score.getFirst( ); char r2 = seats.getFirst();

}

Generic Modules : Function

Templates in C++ A C++ language construct that allows the compiler to generate multiple versions of a function by allowing parameterized data types. Template < TemplateParamList >

FunctionDefinition

FunctionTemplate

TemplateParamDeclaration: placeholder

class typeIdentifier typename variableIdentifier

Function Template Syntax

• Allow ‘swap values’ of any type variables: template<class T> void swapValues(T& var1, T& var2) { T temp; temp = var1; var1 = var2; var2 = temp; }

• First line called ‘template prefix’ • Tells compiler what’s coming is ‘template’

• And that T is a type parameter

Function Template Prefix

• Recall: template<class T>

• In this usage, ‘class’ means ‘type’, or

‘classification’

• Can be confused with other ‘known’ use

of word ‘class’! • C++ allows keyword ‘typename’ in place of keyword ‘class’ here

• But most use ‘class’ anyway

Function Template Prefix

• Again:

template<class T>

T can be replaced by any type

• Predefined or user-defined (like a C++ class

type)

• In function definition body: T used like any

other type

• Note: can use other than ‘T’, but T is

‘traditional’ usage

Calling a Function

Template Consider following call:

swapValues(int1, int2);

• C++ compiler ‘generates’ function definition for two int parameters using

template

Likewise for all other types needn’t do anything

‘special’ in call

• Required definition automatically generated

Example of a Function

Template

template<class SomeType> void Print( SomeType val ) { cout << "***Debug" << endl; cout << "Value is " << val << endl; }

Print<int>(sum);

Print<char>(initial);

Print<float>(angle);

To output the traced values, we insert:

Template parameter

(class, user defined type, built-in types)

Template argument

Another Function

Template • Declaration/prototype:

template<class T> void showStuff(int stuff1, T stuff2, T stuff3);

• Definition: template<class T> void showStuff(int stuff1, T stuff2, T stuff3) { cout << stuff1 << endl << stuff2 << endl << stuff3 << endl; }

showStuff Call

• Consider function call: showStuff(2, 3.3, 4.4);

• Compiler generates function definition

• Replaces T with double

• Since second parameter is type double

• Displays: 2 3.3 4.4

Multiple Type Parameters

• Can have:

template<class T1, class T2>

• Not typical

• Usually only need one ‘replaceable’ type

• Cannot have ‘unused’ template parameters

• Each must be ‘used’ in definition

• Error otherwise!

Another Template Example: passing two parameters

template <class T, int size>

class Stack {...

T buf[size];

};

Stack<int,128> mystack;

non-type parameter

Higher level of genericity

• Suppose we want to write an algorithm that works on different types of data collections, and not just different data types…This is known as higher level of genericity.

• For eg: We want to write an algorithm to do linear search in any linear datastructure, regardless of whether the collection is an array or list.

• This is usually seen in functional languages like ML. • C++ templates can be combined with operator

overloading gives us a high degree of generic programming.

Thank You!

Angelin

top related