正在加载图片...
To swap variables of different types, write another function Overloading allows functions to have same name Signature(types and numbers of parameters) keep them unique to the compiler This could lead to a library of swap functions One function for each standard/primitive type Compiler chooses which to use from signature But . what about swapping user defined types such as an object? We cannot cover the swap function for ALL possible class objects void swap (int& first, int& second) int temp firsti first second void swap (double& first, double& second) secon d temp double temp firsti first second void swap (char& first, char& second) second temp; first second second temp; COMP 152▪ To swap variables of different types, write another function • Overloading allows functions to have same name • Signature (types and numbers of parameters) keep them unique to the compiler ▪ This could lead to a library of swap functions • One function for each standard/primitive type • Compiler chooses which to use from signature ▪ But … what about swapping user defined types such as an object? • We cannot cover the swap function for ALL possible class objects COMP152 5 void swap (int& first, int& second) { int temp = first; first = second; second = temp; } void swap (double& first, double& second) { double temp = first; first = second; second = temp; } void swap (char& first, char& second) { char temp = first; first = second; second = temp; }
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有