zoukankan      html  css  js  c++  java
  • C++网易云课堂开发工程师--转换函数

    class Fraction{

      public:

        Fraction(int num, int den = 1) : m_numerator(num), m_denominator(den) { }

        Fraction operator + (const Fraction& f){

        return Fraction(.....);

        }

      private:

        int m_numerator;

        int m_denominator;

    }

    Fraction f(3, 5);

    Fraction d2 = f + 4;  //调用non-explicit ctor 将4转为Fraction,这部分内容挺有趣的!

    explicit-one-argument ctor

      explicit不允许自动类型转换,通常用在构造函数前面(90%)。

    class Fraction{

      public:

        explict Fraction(int num, int den = 1) : m_numberator(num), m_denominator(den) {  }

        operator double( ) const {return (double)(m_numerator / m_denominator)}

        Fraction operator (const Fraction& f)  { return Fraction(...) };

      private:

        int m_numerator;

        int m_denominator;

    };

    Fraction f(3, 5);

    Fraction d2 = f + 4;  //不允许自动类型转换

  • 相关阅读:
    CCF NOI1062 计算曼哈顿距离
    CCF NOI1061 Wifi密码
    CCF NOI1073 放苹果
    CCF NOI1072 爬楼梯
    CCF NOI1071 Pell数列
    CCF NOI1064 计算斐波那契第n项
    NOI训练行动路线图
    CCF NOI1060 雷同检测
    CCF NOI1059 表达式求值
    CCF NOI1057 石头剪刀布
  • 原文地址:https://www.cnblogs.com/sky-z/p/9575364.html
Copyright © 2011-2022 走看看