c++ - 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 个电阻器?
解决方案
您只需要一个参数,因为类包含一个 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);
}
};
推荐阅读
- javascript - Safari 不会在重定向时保留 Authorization 标头
- c++ - "auto asd = (2,3,4,5);" 是什么意思 做?
- python - VTK 循环遍历图像中的像素
- r - 通过提取列名中的模式重命名数据框的所有列
- java - 如何修复 Spring 中的“Coach 无法解析为类型”错误
- ruby-on-rails - WkWebView 有时会丢失会话变量
- c++ - 执行 Sendmessage 函数后从 ComboBox 获取错误的字符串
- numpy - IndexError:numpy中数组的索引太多
- java - JavaFX:在迭代时删除特殊的日子
- postgresql - AWS RDS PostgreSQL 小更新