c++ 重載
阿新 • • 發佈:2018-06-22
while pla 例子 its ring ostream lan IE more
函數重載
當函數基本上執行相同的任務, 但使用不同形式的數據時, 才應菜哦那個函數重載
#include <iostream> #include <string> using namespace std; // 函數重載 unsigned long left(unsigned long sum, unsigned ct); char * left(const char * str, int n = 1); unsigned long left(unsigned long num, unsigned ct){ unsigned digitsView Code= 1; unsigned long n = num; if (ct == 0 || num == 0 ) return 0; while (n /= 10) { digits++; } if (digits > ct) { ct = digits - ct; while (ct--) num /= 10; return num; }else return num; } char * left(constchar * str, int n){ if (n < 0) n = 0; char * p = new char[n+1]; int i; for ( i = 0;i < n && str[i]; i++){ p[i] = str[i]; } while (i <= n) p[i++] = ‘\0‘; return p; } int main(int argc, char const *argv []){ char * trip = "Hawaii!"; unsignedlong n = 12345678; int i; char * temp; for (i = 1; i < 10; i++){ std::cout << left(n, i) << ‘\n‘; temp = left(trip, i); std::cout << temp << ‘\n‘; delete [] temp; } return 0; }
操作符重載
普通類函數操作
#ifndef MYTIME0_H_ #define MYTIME0_H_ class Time { private: int hours; int minutes; public: Time (); Time (int h, int m = 0); void AddMin(int m); void AddHr(int h); void Reset(int h = 0, int m = 0); Time Sum(const Time & t) const; void Show() const; // virtual ~Time (); }; #endifmytime.h
#include <iostream> #include "mytime.h" Time::Time() { hours = minutes = 0; } Time::Time(int h, int m) { hours = h; minutes = m; } void Time::AddMin(int m) { minutes += m; hours += minutes / 60; minutes %= 60; } void Time::AddHr(int h) { hours += h; } void Time::Reset(int h, int m) { hours = h; minutes = m; } Time Time::Sum(const Time & t) const { Time sum; sum.minutes = minutes + t.minutes; sum.hours = hours + t.hours + sum.minutes / 60; sum.minutes %= 60; return sum; } void Time::Show() const { std::cout << hours << " hours, " << minutes << " minutes" << ‘\n‘; }mytime0.cpp
#include <iostream> #include "mytime.h" using namespace std; int main(int arg, char const * argv []) { // Time a; // a.Show(); Time planing; Time coding(2, 40); Time fixing(5, 55); Time total; std::cout << "planing time = "; planing.Show(); std::cout << endl << ‘\n‘; std::cout << "fixing time = " ; fixing.Show(); std::cout << endl; total = coding.Sum(fixing); std::cout << "coding.Sum(fixing) = " ; total.Show(); std::cout << endl; return 0; }usertime0.cpp
操作符重載例子
#ifndef MYTIME1_H_ #define MYTIME1_H_ class Time { private: int hours; int minutes; public: Time(); Time(int h, int m = 0); void AddMin(int m); void AddHr(int h); void Reset(int h = 0, int m = 0); Time operator+(const Time & t) const; void Show() const; }; #endifmytime1.h
#include <iostream> #include "mytime1.h" Time::Time() { hours = minutes = 0; } Time::Time(int h, int m) { hours = h; minutes = m; } void Time::AddMin(int m) { minutes += m; hours += minutes / 60; minutes %= 60; } void Time::AddHr(int h) { hours += h; } void Time::Reset(int h, int m) { hours = h; minutes = m; } Time Time::operator+(const Time & t)const { Time sum; sum.minutes = minutes + t.minutes; sum.hours = hours + t.hours + sum.minutes / 60; sum.minutes %= 60; return sum; } void Time::Show() const { std::cout << hours << " hours, " << minutes << " minutes"; }mytime1.cpp
#include <iostream> #include "mytime1.h" using namespace std; int main(int argc, char const *argv[]) { Time planning; Time coding(2, 40); Time fixing(5, 55); Time total; std::cout << "planning time ="; planning.Show(); std::cout << ‘\n‘; std::cout << "coding time = "; coding.Show(); std::cout << ‘\n‘; std::cout << "fixing time = "; fixing.Show(); std::cout << ‘\n‘; total = coding + fixing; // 轉化以後就是coding.operator+(fixing); std::cout << "coding + fixing = "; total.Show(); std::cout << ‘\n‘; Time morefixing(3, 28); std::cout << "more fixing time = "; morefixing.Show(); std::cout << ‘\n‘; total = morefixing.operator+(total); std::cout << "morefixing.operator+(total) = "; total.Show(); std::cout << ‘\n‘; // 那多個對象相加呢? //舉個例子 // t4 = t3 + t2 + t1; // 轉換以後 // t4 = t1.operator+(t2 + t3); // t4 = t1.operator+(t2.operator(t3)); return 0; }usertime1.cpp
c++ 重載