Making Algorithm Generic - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Making Algorithm Generic

Description:

Making Algorithm Generic – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 12
Provided by: yxie
Category:

less

Transcript and Presenter's Notes

Title: Making Algorithm Generic


1
Making Algorithm Generic
  • Dr. Ying Xie

2
  • void selectionSort(int arr, int n)
  • for (int startP 0 startP lt n-2 startP)
  • //1. find the smallest number in the
    sublist
  • int subscriptForSmallest startP
  • For (int jstartP jltn-1 j)
  • if (arrj lt
    arrsubscriptForSmallest)
  • subscriptForSmallest j
  • //2. swap the smallest number and the
    first number of this sublist
  • int temp arrstartP
  • arrstarP arrsubscriptForSmallest
  • arrsubscriptForSmallest temp
  • How about If I want to sort a double array?

3
Function Templates
  • C function templates are those functions which
    can handle different data type without separate
    code for each of them.
  • Example template version of selectionSort.
  • template lttypename Tgt
  • void selectionSort(T , int n)
  • template lttypename Tgt
  • void selectionSort(T arr, int n)
  • for (int startP 0 startP lt n-2 startP)
  • //1. find the smallest number in the
    sublist
  • int subscriptForSmallest startP
  • For (int jstartP jltn-1 j)
  • if (arrj lt
    arrsubscriptForSmallest)
  • subscriptForSmallest j
  • //2. swap the smallest number and the
    first number of this sublist
  • T temp arrstartP
  • arrstarP arrsubscriptForSmallest

4
double arr15 2.0, 5.0, 1.0, 10.0,
2.3 selectionSort(arr1, 5) int arr26
1,0,3,4,5,2 selectionSort(arr2, 6) Account
accs100 ifstream accInfo("accInfo.dat") larges
tAccIndex -1 while(accInfogtgtidgtgtpingtgtamount)
largestAccIndex Account
acc(id, pin, amount) accountslargestAccI
ndex acc selectionSort(accs,
largestAccIndex1) ?
5
  • Operator Overload
  • - Operator Overload With Friend Functions
  • - Operator Overload with Member Functions

6
Operator Overload With Friend Functions The
syntax to overload a binary operator for a class
with a friend function. Class myClass
public friend returnType
operator op (const myClass left, const myClass
right) private int mem1
double mem2 //implementing the operator op
as a friend function returnType operator op(const
myClass left, const myClass right)
7
  • Operator Overload lt with Friend Function
  • class Account
  • public
  • Account()
  • Account(string initID, string initPIN,
    double initBalance)
  • void Deposit(double amount)
  • bool Withdraw(double amount)
  • double GetBalance()
  • string GetID()
  • string GetPIN()
  • friend bool operator lt (const Account
    left, const Account right)
  • private
  • double balance
  • string id
  • string pin
  • //implementing the operator lt as a friend
    function

8
Operator Overload with Member Functions The
syntax to overload a binary operator for a class
with a member function. Class myClass
public returnType operator op
(const myClass mc) private int
mem1 double mem2 //implementing the
operator op as a friend function returnType
myClassoperator op(const myClass mc)
9
  • Operator Overload lt with Member Function
  • class Account
  • public
  • Account()
  • Account(string initID, string initPIN,
    double initBalance)
  • void Deposit(double amount)
  • bool Withdraw(double amount)
  • double GetBalance()
  • string GetID()
  • string GetPIN()
  • bool operatorlt (const Account)
  • private
  • double balance
  • string id
  • string pin
  • //implementing the operator lt as a friend
    function

10
How to make decisions overload with friend
function or overload with member function
acc1 lt acc2 ? view 1 operatorlt (acc1, acc2)
then implement it as a friend
function view 2 acc1.operatorlt(acc2)
then implement it as a member function
How about cout ltlt acc1? The only view is
operatorltlt(cout, acc1) then
you can only load ltlt as a friend function.
11
  • Operator Overload ltlt with Friend Function
  • class Account
  • public
  • Account()
  • Account(string initID, string initPIN,
    double initBalance)
  • void Deposit(double amount)
  • bool Withdraw(double amount)
  • double GetBalance()
  • string GetID()
  • string GetPIN()
  • friend ostream operator ltlt (ostream
    ostr, const Account acc)
  • private
  • double balance
  • string id
  • string pin
  • //implementing the operator lt as a friend
    function
Write a Comment
User Comments (0)
About PowerShow.com