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

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

Upload: others

Post on 07-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Contents · 03/03/2018  · • 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,

Contents

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

Page 2: Contents · 03/03/2018  · • 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,

C Language Features

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

encapsulation

Page 3: Contents · 03/03/2018  · • 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,

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.

Page 4: Contents · 03/03/2018  · • 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,

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.

Page 5: Contents · 03/03/2018  · • 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,

C Language Features Program Organization :

Page 6: Contents · 03/03/2018  · • 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,

C++ Language Features

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

encapsulation • : : Scope- resolution operator

Page 7: Contents · 03/03/2018  · • 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,

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.

Page 8: Contents · 03/03/2018  · • 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,

C++ Language Features

Program Organization :

Page 9: Contents · 03/03/2018  · • 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,

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

Page 10: Contents · 03/03/2018  · • 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,

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

Page 11: Contents · 03/03/2018  · • 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,

Class Templates Syntax

Page 12: Contents · 03/03/2018  · • 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,

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; };

Page 13: Contents · 03/03/2018  · • 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,

Class Template Definition

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

Page 14: Contents · 03/03/2018  · • 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,

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; }

Page 15: Contents · 03/03/2018  · • 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,

Class Template Definition

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

template<class T>

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

Page 16: Contents · 03/03/2018  · • 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,

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();

}

Page 17: Contents · 03/03/2018  · • 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,

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

Page 18: Contents · 03/03/2018  · • 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,

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

Page 19: Contents · 03/03/2018  · • 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,

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

Page 20: Contents · 03/03/2018  · • 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,

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

Page 21: Contents · 03/03/2018  · • 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,

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

Page 22: Contents · 03/03/2018  · • 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,

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

Page 23: Contents · 03/03/2018  · • 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,

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; }

Page 24: Contents · 03/03/2018  · • 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,

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

Page 25: Contents · 03/03/2018  · • 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,

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!

Page 26: Contents · 03/03/2018  · • 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,

Another Template Example: passing two parameters

template <class T, int size>

class Stack {...

T buf[size];

};

Stack<int,128> mystack;

non-type parameter

Page 27: Contents · 03/03/2018  · • 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,

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.

Page 28: Contents · 03/03/2018  · • 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,

Thank You!

Angelin