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;
}
View Code

操作符重载

普通类函数操作

#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
mytime.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;
};

#endif
mytime1.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

猜你喜欢

转载自www.cnblogs.com/renfanzi/p/9172014.html