c++ 重载
函数重载
当函数基本上执行相同的任务, 但使用不同形式的数据时, 才应菜哦那个函数重载
#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 digits = 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(const char * 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!"; unsigned long 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 (); }; #endif
#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'; }
#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; }
操作符重载例子
#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; }; #endif
#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"; }
#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; }
注意:
操作符限制:
1, 有些操作符不能重载
懒得写了, 具体看书《c++ primer plus》 P387
2. 很多可以
Container operators
Syntax:
#include<list> container operator=(const container& c2);
bool operator==(const container& c1, const container& c2);
bool operator!=(const container& c1, const container& c2);
bool operator <(const container& c1, const container& c2);
bool operator >(const container& c1, const container& c2);
bool operator <=(const container& c1, const contianer& c2);
bool operator >=(const container& c1, const container& c2);
All of the C++ containers can be compared and assigned with the standard comparison operators: ==, !=, <=, >=, >, <, and =.
Performing a comparison or assigning one list to another takes liner time.
翻译: 执行一个比较或者分配一个list给另一个需要花费时间。
Two lists are equal if:
翻译: 两个list一样如果:
1. Their size is the same, and
翻译: 他们大小一样
2. Each member in location i in one list is equal to the member in location i in the other list.
翻译: 我在一个list的每个成员都等于在另一个list成员的位置
Comparisons among lists are done lexicographically.
翻译:列表之间的比较是按字母顺序进行的
作者:沐禹辰
出处:http://www.cnblogs.com/renfanzi/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
出处:http://www.cnblogs.com/renfanzi/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。