Search code examples
c++classobjectconstantspass-by-reference

What does classname functionname(const classname & objectname) means?


I have an assigment where we should do resistor calculations (series and parallel) using the following class :

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);



};

My question is regarding the functions serie and parallel . How am i supposed to add 2 resistors using a function that only takes one single object as an argument?


Solution

  • You only need one parameter because the class contains the information for one R, and the parameter contains the information for the other 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);
            }
    
        };