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.
Please complete the class survey� ( 10 minutes )��
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
This week we will cover
How do you call individual components of a Class?
A/ Members
Two identical functions different data types
Source: Zybook C++ Book
Source: Zybook C++ Book
Template Functions
-- Live Demo (Template Functions) --
11.1 Template Classes
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
Template Class Syntax
Form: template <class T>
class className
{
public:
. . .
private:
. . .
};
12
Object Declarations & Templates
class-name<type> object;
E.g.
indexList<int> intList;
dummy <int> numDepend;
dummy <string> spouseName;
13
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;
}
What are templates used for?
A/ Allows you to create generic functions or classes that work with multiple data types.
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
What is an advantage of the object-oriented design?
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.
11.5 Operator Overloading and Friends of a class
23
Operator Overloading
bool operator < (const entry&);
24
Class entry operator <
25
What is Operator Overloading used for?
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.
Friends
30
-- Live Demo of Friends of a class--
What are friends of classes used for?
A/ Allow functions that are not part of the class to have access to all the objects (public and private) in the class.
11.7 Common Programming Errors
35