+ All Categories
Home > Documents > CE00314-2 Further Programming Concepts in C++

CE00314-2 Further Programming Concepts in C++

Date post: 31-Jan-2016
Category:
Upload: lindley
View: 23 times
Download: 0 times
Share this document with a friend
Description:
CE00314-2 Further Programming Concepts in C++. Lecture 6 Part 1 - Templates Part 2 - Further Inheritance. Introduction. Part 1 Template definition Function templates Class templates Part 2 Further inheritance. Template Definition. Cambridge Dictionary - PowerPoint PPT Presentation
21
CE00314-2 Further Programming Concepts in C++ Lecture 6 Part 1 - Templates Part 2 - Further Inheritance
Transcript
Page 1: CE00314-2 Further Programming Concepts in C++

CE00314-2Further Programming Concepts in C++

Lecture 6

Part 1 - Templates

Part 2 - Further Inheritance

Page 2: CE00314-2 Further Programming Concepts in C++

Introduction

Part 1 Template definition Function templates Class templates

Part 2 Further inheritance

Page 3: CE00314-2 Further Programming Concepts in C++

Template Definition

Cambridge Dictionary template  1 a pattern made of metal, plastic or paper, which is used for making many copies of a shape or to help cut material accurately

2 a system that helps you arrange information on a computer screen

C++ Pattern for handling generic data

Page 4: CE00314-2 Further Programming Concepts in C++

Function overloading

Swap revisited To handle integers

void swap(int& rnA, int& rnB){ int nTemp;

nTemp = rnA; rnA = rnB;

rnB = nTemp}

Page 5: CE00314-2 Further Programming Concepts in C++

Function overloading

Swap revisited To handle characters

void swap(char& rnA, char& rnB){ char nTemp;

nTemp = rnA; rnA = rnB;

rnB = nTemp}

Page 6: CE00314-2 Further Programming Concepts in C++

Function overloading

Swap revisited float, double ……. Separate functions for each ??

Generic function Generic functionality Generic data – automatically typed when used Template function

Page 7: CE00314-2 Further Programming Concepts in C++

Template function

Conceptually

void swap(Type_of_Var& Var1, Type_of_Var& Var2){ Type_of_Var Temp; Temp = Var1; Var1 = Var2; Var2 = Temp;}

Page 8: CE00314-2 Further Programming Concepts in C++

Template Function - syntax

template<class T> // template prefixvoid swapValues(T& Var1, T& Var2){

T Temp;Temp = Var1;Var1 = Var2;Var2 = Temp;

}

For demonstration see – Template1.cpp

Page 9: CE00314-2 Further Programming Concepts in C++

Template functions - array print Handling unspecified arrays Array length?

template<class T>void printArray(const T *array, const int nSize){

for(int nCount = 0; nCount < nSize; nCount++)

cout << array[nCount] << “ “;

cout << endl;}

For usage example see – Template2.cpp

Page 10: CE00314-2 Further Programming Concepts in C++

Template Functions - returningAlso possible to have generic return

template<class T>

T addValues(T Val1, T Val2)

{

return Val1 + Val2;

}

See Template3.cpp for usage example

Page 11: CE00314-2 Further Programming Concepts in C++

Template Functions – multiple types Possible to have more than one type

template<class T1, class T2> Must use all parameters All must be utilised within function See Template4.cpp

Possible to use Tx for casting and return type See Template5.cpp

Page 12: CE00314-2 Further Programming Concepts in C++

Template functions - Plenary

Produce generic solution Data types provided at run time Operators must be able to handle types Overloaded operators included Use of keyword “class” can be replaced with

“typename” ANSI/ISO standard allows Programmers tend to use “class”

Page 13: CE00314-2 Further Programming Concepts in C++

Class Templates

Syntax basically the same as when using function templates must use keyword “class”

template<class T>class Pair{private:

T first;T second;

private:Pair();Pair(T Val1, T Val2);

etc….. See Template6.cpp for further details

Page 14: CE00314-2 Further Programming Concepts in C++

Class templates

So far….. Class within main file Could place within .h file

and include See Template7.cpp

Shows use of Array of pointers ? – mixed types?

Page 15: CE00314-2 Further Programming Concepts in C++

Plenary – Part 1

Concept of templates Template functions

Use of “class” or “typename” keywords Template classes

Must use “class” keyword

Tutorial Work Experiment with given code Is it possible to mix types when using arrays etc?

Page 16: CE00314-2 Further Programming Concepts in C++

Part 2 – Further Inheritance

Base Class e.g. Shape Not viable as object Also consider “Dog” May prevent object instantiation Class becomes “Abstract Base Class”

Page 17: CE00314-2 Further Programming Concepts in C++

Abstract Base Class

Dummy definitions Overridden within derived class

Virtual methods Without any definition “Pure Virtual Functions” Only within ABC

Class containing ABCs Can not be used to instantiate objects

Page 18: CE00314-2 Further Programming Concepts in C++

Abstract Base Class

Shape

Circle Square Triangle

This is meaninglessin a real sense

Page 19: CE00314-2 Further Programming Concepts in C++

Abstract Base Class

May not be able to instantiate But …… ….. still vital to hierarchy

See Abstract1.cpp & Abstract1.h

Page 20: CE00314-2 Further Programming Concepts in C++

Within “Shape” class

Could have empty methods virtual void Draw( ) { }

Or make it pure virtual virtual void Draw ( ) = 0;

Within Shape – no functionality for Draw( )

Page 21: CE00314-2 Further Programming Concepts in C++

Tutorial Work

As for templates Plus

Experiment with ABCs – using Abstract1.ccp & .h Research for assignment

Next Week Bob Hobbs


Recommended