1 of 36

CNSP - Lecture 11� Chapter 11�Advanced Techniques

BY PROF. RAFAEL ORTA

Disclosure: this presentation includes slides that were provided by the textbook publisher and Dr. Hnatyshin, some are adaptation, and some are an exact replica.

2 of 36

3 of 36

Please complete the class survey� ( 10 minutes )��

4 of 36

Last week we covered

10.1 Pointers and the Address Operator

10.2 Pointer Variables

10.3 The Relationship Between Arrays

and Pointers

10.4 Pointer Arithmetic

10.5 Initializing Pointers

10.6 Comparing Pointers

10.7 Pointers as Function Parameters

10.8 Pointers to Constants and Constant Pointers

10.9 Dynamic Memory Allocation

10.10 Returning Pointers from Functions

5 of 36

This week we will cover

  • Template functions.
  • How to use a template of classes in C++ and how to define it.
  • Review of object-oriented design.
  • How to overload C++ operators.
  • Understand friends of a class.

6 of 36

How do you call individual components of a Class?

7 of 36

A/ Members

8 of 36

Two identical functions different data types

Source: Zybook C++ Book

9 of 36

Source: Zybook C++ Book

Template Functions

10 of 36

-- Live Demo (Template Functions) --

11 of 36

11.1 Template Classes

  • Very similar to the use of template functions
  • Enables class to be used with any data type

before class definition, add

template <class T>

where T is a placeholder for the actual type of the class used when a class object is declared

11

12 of 36

Template Class Syntax

Form: template <class T>

class className

{

public:

. . .

private:

. . .

};

12

13 of 36

Object Declarations & Templates

  • Form:

class-name<type> object;

E.g.

indexList<int> intList;

  • type can be any defined data type
  • class-name is name of template class
  • object is what is created of type <type>

dummy <int> numDepend;

dummy <string> spouseName;

13

14 of 36

Template Classes example

#include <iostream>

using namespace std;

template <class T>

class Calculator

{

private:

T num1, num2;

public:

Calculator(T n1, T n2)

{

num1 = n1;

num2 = n2;

}

void displayResult()

{

cout << "Numbers are: " << num1 << " and " << num2 << "." << endl;

cout << "Addition is: " << add() << endl;

cout << "Subtraction is: " << subtract() << endl;

cout << "Product is: " << multiply() << endl;

cout << "Division is: " << divide() << endl;

}

T add() { return num1 + num2; }

T subtract() { return num1 - num2; }

T multiply() { return num1 * num2; }

T divide() { return num1 / num2; }

};

int main()

{

Calculator<int> intCalc(2, 1);

Calculator<float> floatCalc(2.4, 1.2);

cout << "Int results:" << endl;

intCalc.displayResult();

cout << endl << "Float results:" << endl;

floatCalc.displayResult();

return 0;

}

15 of 36

16 of 36

What are templates used for?

17 of 36

A/ Allows you to create generic functions or classes that work with multiple data types.

18 of 36

11.4 Illustrating Object-Oriented Design

1. Identify data and define services provided by each object

2. Identify interactions among objects in terms of services required or

provided

3. Determine the specification of each object

4. Implement each object

18

19 of 36

20 of 36

What is an advantage of the object-oriented design?

21 of 36

A/The main advantage is the abstraction it provides, the ability to be able to re-use & share code. Also, its encapsulation provides a good platform to divide work among groups.

22 of 36

23 of 36

11.5 Operator Overloading and Friends of a class

  • More than one way to provide operator functions for class objects
    • overloading existing operators, such as <, >, <<
      • allows relation of the form e1 < e2

23

24 of 36

Operator Overloading

  • E.g. in entry class definition file

bool operator < (const entry&);

  • < is an operator that can be applied to a type entry object
  • The parameter list indicates that the right operand of < must also be of type entry

24

25 of 36

Class entry operator <

25

26 of 36

What is Operator Overloading used for?

27 of 36

A/ Allows classes the use similar operators to the ones we use with other data types. It also allows the use of an operator for more than one function.

28 of 36

29 of 36

30 of 36

Friends

  • A friend of a class is a member function or operator that has two or more operands from the class being defined or an operand from another class
    • has all privileges of member function
    • can’t be const function

30

31 of 36

-- Live Demo of Friends of a class--

32 of 36

What are friends of classes used for?

33 of 36

A/ Allow functions that are not part of the class to have access to all the objects (public and private) in the class.

34 of 36

35 of 36

11.7 Common Programming Errors

  • Misuse of subscript notation or dot notation (Arrays of Classes), use both the array subscript and function name ex. X[i].getCom().
  • Errors in defining template classes ( remember to use :: (scope resolution operator) , and the word Template as prefix)
  • Errors in using friend functions (remember to begin the function prototype with the word friend).

35

36 of 36