首页 > 解决方案 > classname functionname(const classname & objectname) 是什么意思?

问题描述

我有一个分配,我们应该使用以下类进行电阻器计算(串联和并联):

class R1 {

protected:
   double R , cost_in_euro ;
      string unit ;
public :

   R1(double R , const string & unit , double cost_in_euro);
   R1(double R , double cost_in_euro);
   R1(double R);
   R1(const R1 & R);


   R1 serie(const R1 & R1);
   R1 parallel(const R1 & R1);



};

我的问题是关于函数 serie 和 parallel 。我应该如何使用仅将一个对象作为参数的函数添加 2 个电阻器?

标签: c++classobjectconstantspass-by-reference

解决方案


您只需要一个参数,因为类包含一个 R 的信息,而参数包含另一个 R 的信息。

    class R1 {
        protected:
        double R , cost_in_euro ;
            string unit ;
        public :

        R1(double R , const string & unit , double cost_in_euro);
        R1(double R , double cost_in_euro);
        R1(double R);
        R1(const R1 & R);


        R1 serie(const R1 & other)
        {
            double total = other.R + R;
            return R1(total);
        }

        R1 parallel(const R1 & other)
        {
            double r1 = other.R;
            double r2 = R;
            double total = (r1*r2)/(r1 + r2);

            return R1(total);
        }

    };

推荐阅读